Socket
Socket
Sign inDemoInstall

tty-table

Package Overview
Dependencies
15
Maintainers
1
Versions
89
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    tty-table

Command line table generator.


Version published
Maintainers
1
Install size
1.84 MB
Created

Readme

Source

tty-table

Build Status Dependency Status NPM version Built with Grunt

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

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, should be used by setting the following option:

borderStyle : 2

Example Usage

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

var header = [
	{
		value : "item",
		headerColor : "cyan",
		color: "white",
		align : "left",
		paddingLeft : 5,
		width : 30
	},
	{
		value : "price",
		color : "red", 
		width : 10,
		formatter : function(value){
			var str = "$" + value.toFixed(2);
			if(value > 5){
				str = chalk.underline.green(str);
			}
			return str;
		}
	},
	{
		alias : "Is organic?",	
		value : "organic",
		width : 15,
		formatter : function(value){
			
			//will convert an empty string to 0	
			//value = value * 1;
			
			if(value === 'yes'){
				value = chalk.black.bgGreen(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,
	borderColor : "blue",
	paddingBottom : 0,
	headerAlign : "center",
	align : "center",
	color : "white"
});

str1 = t1.render();
console.log(str1);


//Example with 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"
	}
];

var t2 = Table(header,rows,{
	borderStyle : 1,
	paddingBottom : 0,
	headerAlign : "center",
	align : "center",
	color : "white"
});

var str2 = t2.render();
console.log(str2);

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: "+"}
		]
	]

Table(header, rows, options)

ParamTypeDescription
headerarraySee example
header.columnobjectColumn options
header.column.aliasstringAlernate header column name
header.column.alignstringdefault: "center"
header.column.colorstringdefault: terminal default color
header.column.footerAlignstringdefault: "center"
header.column.footerColorstringdefault: terminal default color
header.column.formatterfunctionRuns a callback on each cell value in the parent column
header.column.headerAlignstringdefault: "center"
header.column.headerColorstringdefault: terminal's default color
header.column.marginLeftnumberdefault: 0
header.column.marginTopnumberdefault: 0
header.column.maxWidthnumberdefault: 20
header.column.paddingBottomnumberdefault: 0
header.column.paddingLeftnumberdefault: 0
header.column.paddingRightnumberdefault: 0
header.column.paddingTopnumberdefault: 0
rowsarraySee example
optionsobjectTable options
options.borderStylenumberdefault: 1 (0 = no border) Refers to the index of the desired character set.
options.borderCharactersarraySee @note
options.borderColorarraydefault: terminal's default color

Example

var Table = require('tty-table');
var t1 = Table(header,rows,options);
console.log(t1.render()); 

Table.Cls.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.

Keywords

FAQs

Last updated on 31 May 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc