Display yout data in a table using a terminal, browser, or browser console.
- To install as a terminal application:
$ sudo apt-get install nodejs
$ npm install tty-table -g
- As a Nodejs module:
$ npm install tty-table
- Browser (via browserify)
<script src="tty-table.bundle.min.js"></script>
<script>
var Table = require('tty-table');
...
</script>
- Can be used as a bugfixed, drop-in replacement for the unmaintained, but popular Automattic/cli-table:
var Table = require('tty-table')('automattic-cli-table');
//now runs with same syntax as Automattic/cli-table
...
- Fixes these open issues with Automattic/cli-table:
- Optional callbacks on column values
- Header, body column alignment
- Padding
- Passing of rows as either arrays or objects
- Colors (not supported in browser)
- Footer
- Works in the browser as well as nodejs
- Can be run as a standalone terminal application
examples/styles-and-formatting.js
$ node examples/data/fake-stream.js | tty-table --format=json
- *See the built-in help for the terminal version of tty-table with:
$ tty-table -h
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
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.alias | string |
Alernate header column name |
header.column.align | string |
default: "center" |
header.column.color | string |
default: terminal default color |
header.column.footerAlign | string |
default: "center" |
header.column.footerColor | string |
default: terminal default color |
header.column.formatter | function |
Runs a callback on each cell value in the parent column |
header.column.headerAlign | string |
default: "center" |
header.column.headerColor | string |
default: terminal's default color |
header.column.marginLeft | number |
default: 0 |
header.column.marginTop | number |
default: 0 |
header.column.width | string | number |
default: "auto" |
header.column.paddingBottom | number |
default: 0 |
header.column.paddingLeft | number |
default: 1 |
header.column.paddingRight | number |
default: 1 |
header.column.paddingTop | number |
default: 0 |
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 |
options.borderColor | string |
default: terminal's default color |
options.compact | boolean |
default: false Removes horizontal lines when true. |
options.defaultErrorValue | mixed |
default: 'ERROR!' |
options.defaultValue | mixed |
default: '?' |
options.errorOnNull | boolean |
default: false |
options.truncate | mixed |
default: false When this property is set to a string, cell contents will be truncated by that string instead of wrapped when they extend beyond of the width of the cell. For example if: "truncate":"..." the cell will be truncated with "..." |
Example
let Table = require('tty-table');
let t1 = Table(header,rows,options);
console.log(t1.render());
Renders a table to a string
Kind: static method of Table
Example
let str = t1.render();
console.log(str); //outputs table
$ grunt test
$ grunt st
- Because:
node script.js --color=always
- To generate vim tags (make sure jsctags is installed globally)
$ grunt tags
- To generate vim tags on file save
$ grunt watch
Copyright 2015-2017, Tecfu.