automin
Version:
"Create a minified mirror version of your js, css, html, json files"
71 lines (70 loc) • 2.48 kB
JavaScript
;
/**
* Stylish Console
* */
Object.defineProperty(exports, "__esModule", { value: true });
exports.print_stats = exports.print = void 0;
;
const print = (str, options) => {
const { overwrite, returnOutput, hasEllipsis } = Object.assign({
overwrite: false,
returnOutput: false,
hasEllipsis: true // Add ellipsis if string is longer than CL width
}, options);
// Get command line width
const cl_width = (process.stdout.columns || defaultColumns);
// Trim string
let result = String(str).normalize('NFKC').substring(0, cl_width);
if (hasEllipsis && result.length >= cl_width)
result = result.substring(0, result.length - 3) + "...";
else
result += " ".repeat(Math.abs(cl_width - result.length));
if (overwrite) // Overwrite last command line
result = `\r${result}`;
if (returnOutput) // return output?
return result;
process.stdout.write(result);
};
exports.print = print;
;
// Center a string between given width of spaces
const center = (str, width) => {
const len = str.length;
const reduce = Math.abs(width - len);
let div = Math.floor(reduce / 2);
let sample = div;
while ((div + sample + len) > width)
sample -= 1;
sample = Math.abs(sample);
div = Math.abs(div);
return " ".repeat(div) + str + " ".repeat(sample);
};
const print_stats = (str, stats) => {
const cl_width = (process.stdout.columns || defaultColumns);
const diff_width = 10;
const stat_width = 10;
const sb = []; // String Builder. By Java Hahaha
const reduce_width = cl_width - stat_width - diff_width;
const stat = stats.stat;
str = String(str).normalize("NFD");
str = str.substring(0, reduce_width);
if (str.length == reduce_width) {
str = str.substring(0, str.length - 4) + "..."; // Add ellipsi
str += " "; // Add Margin After String
}
else {
// Fill with some extra chararacters to fill some
// empty cells and make a perfect thing...???
const ma = reduce_width - str.length - 2;
str += " "; // Add Margin After String
if (ma > 0)
str += "~".repeat(ma);
if (ma > -1)
str += " "; // Add margin after generated cell(s)
}
sb.push(str);
sb.push(center(stat, stat_width - 1));
sb.push(center(stats.diff, diff_width - 1));
process.stdout.write(sb.join("|") + "\n");
};
exports.print_stats = print_stats;