tty-table
A terminal table widget for nodejs and the browser.
Installation
npm install tty-table
<script src="tty-table.bundle.min.js"></script>
<script>
var Table = require('tty-table');
...
</script>
Why
Example Output
Terminal
Browser & Browser Console
Working example
Note that neither ASCI colors nor default borders are rendered in the browser.
An alternative border style, as shown below, should be used by setting the following option:
borderStyle : 2
Example Usage
var Table = require('../');
var chalk = require('chalk');
var header = [
{
value : "item",
headerColor : "cyan",
color: "yellow",
align : "left",
// paddingRight : 5,
// width : 30
},
{
value : "price",
color : "red",
formatter : function(value){
var str = "$" + value.toFixed(2);
if(value > 5){
str = chalk.underline.green(str);
}
return str;
}
},
{
alias : "Is organic?",
value : "organic",
formatter : function(value){
if(value === 'yes'){
value = chalk.stripColor(value);
value = chalk.green(value);
}
else{
value = chalk.white.bgRed(value);
}
return value;
}
}
];
//Example with arrays as rows
var rows = [
["hamburger",2.50,"no"],
["el jefe's special cream sauce",0.10,"yes"],
["two tacos, rice and beans topped with cheddar cheese",9.80,"no"],
["apple slices",1.00,"yes"],
["ham sandwich",1.50,"no"],
["macaroni, ham and peruvian mozzarella",3.75,"no"]
];
var footer = [
"TOTAL",
(function(){
return rows.reduce(function(prev,curr){
return prev+curr[1]
},0)
}()),
(function(){
var total = rows.reduce(function(prev,curr){
return prev+((curr[2]==='yes') ? 1 : 0);
},0);
return (total/rows.length*100).toFixed(2) + "%";
}())];
var t1 = Table(header,rows,footer,{
borderStyle : 1,
paddingBottom : 0,
headerAlign : "center",
align : "center",
color : "white"
});
str1 = t1.render();
console.log(str1);
//You can also pass an array of objects as rows
var rows = [
{
item : "hamburger",
price : 2.50,
organic : "no"
},
{
item : "el jefe's special cream sauce",
price : 0.10,
organic : "yes"
},
{
item : "two tacos, rice and beans topped with cheddar cheese",
price : 9.80,
organic : "no"
},
{
item : "apple slices",
price : 1.00,
organic : "yes"
},
{
item : "ham sandwich",
price : 1.50,
organic : "no"
},
{
item : "macaroni, ham and peruvian mozzarella",
price : 3.75,
organic : "no"
}
];
...
API Reference
Table
Kind: global class
Note:
Default border character sets:
[
[
{v: " ", l: " ", j: " ", h: " ", r: " "},
{v: " ", l: " ", j: " ", h: " ", r: " "},
{v: " ", l: " ", j: " ", h: " ", r: " "}
],
[
{v: "│", l: "┌", j: "┬", h: "─", r: "┐"},
{v: "│", l: "├", j: "┼", h: "─", r: "┤"},
{v: "│", l: "└", j: "┴", h: "─", r: "┘"}
],
[
{v: "|", l: "+", j: "+", h: "-", r: "+"},
{v: "|", l: "+", j: "+", h: "-", r: "+"},
{v: "|", l: "+", j: "+", h: "-", r: "+"}
]
]
Param | Type | Description |
---|
header | array | See example |
header.column | object | Column options |
header.column.formatter | function | Runs a callback on each cell value in the parent column |
header.column.marginLeft | number | default: 0 |
header.column.marginTop | number | default: 0 |
header.column.maxWidth | number | default: 20 |
header.column.paddingBottom | number | default: 0 |
header.column.paddingLeft | number | default: 0 |
header.column.paddingRight | number | default: 0 |
header.column.paddingTop | number | default: 0 |
header.column.alias | string | Alernate header column name |
header.column.align | string | default: "center" |
header.column.color | string | default: terminal default color |
header.column.headerAlign | string | default: "center" |
header.column.headerColor | string | default: terminal default color |
header.column.footerAlign | string | default: "center" |
header.column.footerColor | string | default: terminal default color |
rows | array | See example |
options | object | Table options |
options.borderStyle | number | default: 1 (0 = no border) Refers to the index of the desired character set. |
options.borderCharacters | array | See @note |
Example
var Table = require('tty-table');
Table(header,rows,options);
Table.render() ⇒ String
Renders a table to a string
Kind: static method of Table
Example
var str = t1.render();
console.log(str); //outputs table
Running tests
grunt test
Saving the output of new tests
grunt st
License
GPLv3 License
Copyright 2015, Tecfu.