termtable
v1.0.2
Published
Aligns data by column in a terminal
Downloads
6
Readme
Termtable
Termtable allows you to align data in columns for your terminal applications. I am open for some contributions so feel free to open some issues or make some pull requests!
Install
The package is currently on the node package manager. It can be installed with the npm client:
npm install termtable
Examples
Here is a simple example to display a simple array of objects.
var termtable = require('termtable');
var cats = [
{name: 'mittens', color: 'orange', age: 2},
{name: 'mr.bubbles', color: 'black', age: 1},
{name: 'ginger', color: 'white', age: 6}
];
var config = {
columns: [
{key: 'name', label: 'Name'},
{key: 'color', label: 'Color'},
{key: 'age', label: 'Age (in years)'}
]
};
var table = termtable(cats, config);
console.log(table);
Ordering
To order by a specific column, just add the orderBy
property to the config.
var termtable = require('termtable');
var cats = [
{name: 'mittens', color: 'orange', age: 2},
{name: 'mr.bubbles', color: 'black', age: 1},
{name: 'ginger', color: 'white', age: 6}
];
var config = {
columns: [
{key: 'name', label: 'Name'},
{key: 'color', label: 'Color'},
{key: 'age', label: 'Age (in years)'}
],
orderBy: 'age'
};
var table = termtable(cats, config);
console.log(table);
Conditionnal styling
It is possible to format the content of a specific cell with the colors
package. The next example will format in red the ages that are greater than 2:
var termtable = require('termtable');
var colors = require('colors');
var cats = [
{name: 'mittens', color: 'orange', age: 2},
{name: 'mr.bubbles', color: 'black', age: 1},
{name: 'ginger', color: 'white', age: 6}
];
var config = {
columns: [
{key: 'name', label: 'Name', style: formatByAge},
{key: 'color', label: 'Color'},
{key: 'age', label: 'Age (in years)'}
]
};
function formatByAge(cat) {
if(cat.age>=2) {
return colors.red;
}
return colors.blue;
}
var table = termtable(cats, config);
console.log(table);
Tests
It is possible to test the package with npm:
npm test