JSPM

  • Created
  • Published
  • Downloads 398386
  • Score
    100M100P100Q171603F
  • License GPL3

Command line table generator.

Package Exports

  • tty-table

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (tty-table) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

tty-table

A terminal table widget for nodejs and the browser.

Installation

  • Nodejs
npm install tty-table
  • Browser (via browserify)
<script src="tty-table.bundle.min.js"></script>
<script>
    var Table = require('tty-table');
    ...
</script>

Why

  • Automatic text wrapping
  • Colors
  • Optional callbacks on column values
  • Header, body column alignment
  • Padding
  • Works in the browser as well as nodejs

Example Output

Terminal

Terminal Example

Browser & Browser Console

Browser Console Example

Working example

Note that neither ASCI colors nor default borders are rendered in the browser. An alternative border style, as shown below, can be used by setting the option:

borderStyle : 2

Example Usage

var Table = require('tty-table');
var chalk = require('chalk');

var header = [
    {
        value : "item",
        headerColor : "cyan",
        color: "yellow",
        alignment : "left",
        paddingLeft : 1,
        width : 30
    },
    {
        value : "price",
        color : "red", 
        formatter : function(value){
            var str = "$" + value.toFixed(2);
            if(value > 5){
                str = chalk.underline.green(str);
            }
            return str;
        }
    },
    {
        value : "organic",
        formatter : function(value){
            if(value === 'yes'){
                value = chalk.stripColor(value);
                value = chalk.green(value);
            }
            else{
                value = chalk.white.bgRed(value);
            }
            return value;
        }
    }
];

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"]
];

//Example 1
var t1 = Table(header,rows,{
    borderStyle : 1,
    paddingBottom : 0,
    headerAlignment : "center",
    alignment : "center",
    color : "white"
});
var str1 = t1.render();
console.log(str1);

API Reference

Table

Kind: global class

Table(header, rows, options)

Param Type Description
header array
header.column object
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.alignment string default: "center"
header.column.color string default: terminal default color
header.column.headerAlignment string default: "center"
header.column.headerColor string default: terminal default color
rows array
options object Table options
options.borderStyle number default: 1 (0 = no border) Refers to the index of the desired character set.
options.borderCharacters array

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

License

GPLv3 License

Copyright 2015, Tecfu.