Skip to content

Instantly share code, notes, and snippets.

@quimcalpe
Last active May 28, 2023 16:41
Show Gist options
  • Save quimcalpe/7626075 to your computer and use it in GitHub Desktop.
Save quimcalpe/7626075 to your computer and use it in GitHub Desktop.
Ansi Tables on console
var ansi = require("ansi");
var cursor = ansi(process.stdout);
var colors = [
"white", "black", "blue", "cyan", "green", "magenta", "red", "yellow", "grey",
"brightBlack", "brightRed", "brightGreen", "brightYellow", "brightBlue", "brightMagenta", "brightCyan", "brightWhite"
];
var styles = [ "bold", "italic", "underline", "inverse" ];
colors.forEach( function (color) {
String.prototype[color] = function () {
this._color = color;
return this;
};
});
styles.forEach( function (style) {
String.prototype[style] = function () {
if ( !this._styles ) this._styles = [];
this._styles.push(style);
return this;
};
});
function printTable (table, opts) {
opts = opts || { align : [] };
// compute row length
if ( table.length === 0 ) return;
var numCols = table.reduce(function (maxCols, col) {
return ( col.length > maxCols ) ? col.length : maxCols;
}, 0);
var maxColumnLengths = Array(numCols);
for ( var i = 0; i < numCols; i++ ) maxColumnLengths[i] = 0;
table.map(function (row) {
var i = 0;
for ( ; i < numCols; i++ ) {
if ( row[i] !== undefined && row[i].length > maxColumnLengths[i] ) {
maxColumnLengths[i] = row[i].length;
}
}
});
// print table
var output = [];
table.forEach(function (row) {
row.forEach(function (col, index) {
if ( col._color ) {
cursor[col._color]();
}
if ( col._styles ) {
col._styles.forEach( function (style) {
cursor[style]();
});
}
process.stdout.write(padString(col.toString(), maxColumnLengths[index], opts.align[index]));
process.stdout.write(" ");
cursor.reset();
});
process.stdout.write("\n");
});
}
function padString(str, padLen, align, padChar) {
if ( str.length >= padLen ) return str;
if ( padChar === undefined ) padChar = " ";
switch ( align ) {
case "center":
var availableSlots = padLen - str.length;
var right = Math.ceil(availableSlots / 2);
var left = availableSlots - right;
return Array(left+1).join(padChar) + str + Array(right+1).join(padChar);
break;
case "right":
return Array(padLen + 1 - str.length).join(padChar) + str;
break;
default:
return str + Array(padLen + 1 - str.length).join(padChar);
break;
}
}
var table = [
[ "Package".blue().underline(), "Current".blue().underline(), "Wanted".blue().underline(), "Latest".blue().underline(), "Path".blue().underline() ],
[ "fstream".red(), "0.1.24", "0.1.25".green(), "0.1.25".magenta(), "fstream".brightBlack() ],
[ "lru-cache".yellow(), "2.3.1", "2.3.1".green(), "2.5.0".magenta(), "lru-cache".brightBlack() ],
[ "node-gyp".red(), "0.11.0", "0.11.0".green(), "0.12.1".magenta(), "node-gyp".brightBlack() ],
[ "npm-registry-client".red(), "0.2.29", "0.2.30".green(), "0.2.30".magenta(), "npm-registry-client".brightBlack() ]
];
printTable( table, { align : [ "left", "right", "right", "right", "left" ] } );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment