tiny-bin
Version:
A library for building tiny and beautiful command line apps.
66 lines (65 loc) • 2.49 kB
JavaScript
/* IMPORT */
import stringWidth from 'fast-string-width';
import colors from 'tiny-colors';
import Addon from './addon.js';
import { identity, stripAnsi } from '../utils.js';
/* MAIN */
class Logger extends Addon {
/* CONSTRUCTOR */
constructor(bin, handler) {
super(bin);
this.indentationLevel = 0;
this.indentation = ' ';
this.handler = handler;
}
/* API */
indent() {
this.indentationLevel += 1;
}
dedent() {
this.indentationLevel -= 1;
}
group(name, fn) {
this.print(colors.bold(name.toUpperCase()));
this.indent();
this.print();
fn();
this.print();
this.dedent();
}
print(message = '') {
const colorize = this.bin.metadata.colors ? identity : stripAnsi;
this.handler(colorize(`${this.indentation.repeat(this.indentationLevel)}${message}`));
}
table(rows, mode = 'line') {
const rowsLengths = rows.map(row => row.map(cell => stringWidth(cell)));
const maxLengths = rowsLengths[0].map((_, j) => Math.max(...rowsLengths.map((_, i) => rowsLengths[i][j])));
if (mode === 'lines' && maxLengths.length === 2) { //TODO: Generalize this, even though it's not needed yet
const COLUMN = 30; //TODO: Make this customizable
const PADDING = 4;
rows.forEach(([left, right], i) => {
const leftNedded = stringWidth(left) + PADDING;
const leftAvailable = COLUMN - leftNedded;
const leftShortEnough = (leftAvailable >= 2);
const rightLines = right.trim().split(/\r?\n|\r/g);
const line = [left, rightLines.map((line, i) => (leftShortEnough && !i) ? `${' '.repeat(leftAvailable)}${line}` : `${i ? '' : '\n'}${' '.repeat(COLUMN)}${line}`).join('\n')].join('');
this.print(line);
});
}
else if (mode === 'line') {
rows.forEach((row, i) => {
const line = row.map((value, j) => {
const paddingLength = (j === row.length - 1) ? 0 : Math.max(0, 1 + maxLengths[j] - rowsLengths[i][j]);
const padding = ' '.repeat(paddingLength);
return `${value}${padding}`;
}).join(' ');
this.print(line);
});
}
else {
throw new Error('Unsupported printing mode');
}
}
}
/* EXPORT */
export default Logger;