Socket
Socket
Sign inDemoInstall

tty-table

Package Overview
Dependencies
8
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
Created

Readme

Source

tty-table

A table widget for CLI applications in nodejs.

Installation

npm install tty-table

Why

  • Automatic text wrapping
  • Colors
  • Optional callbacks on column values

Example Output

Example

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)

ParamTypeDescription
headerarray
rowsarray
optionsobjectTable options
options.marginTopnumberdefault: 0
options.marginLeftnumberdefault: 0
options.maxWidthnumberdefault: 20
options.callbackfunctiondefault: null
options.headerAlignmentstringdefault: "center"
options.alignmentstringdefault: "center"
options.paddingRightnumberdefault: 0
options.paddingLeftnumberdefault: 0
options.paddingBottomnumberdefault: 0
options.paddingTopnumberdefault: 0
options.colorstringdefault: terminal default color
options.headerColorstringdefault: terminal default color
options.borderStylesnumberdefault: 1 (0 = no border)

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.

Keywords

FAQs

Last updated on 07 Jul 2015

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