What is tty-table?
The tty-table npm package is used to create and display tables in the terminal. It provides a simple and flexible way to format and present tabular data in a visually appealing manner.
What are tty-table's main functionalities?
Basic Table Creation
This feature allows you to create a basic table with specified headers and rows. The table is then rendered and displayed in the terminal.
const ttyTable = require('tty-table');
const header = [
{ value: 'Name', width: 30, headerColor: 'cyan' },
{ value: 'Age', width: 10, headerColor: 'cyan' }
];
const rows = [
['Alice', 30],
['Bob', 25]
];
const table = ttyTable(header, rows);
console.log(table.render());
Customizing Table Appearance
This feature allows you to customize the appearance of the table, including border style, border color, padding, alignment, and text color.
const ttyTable = require('tty-table');
const header = [
{ value: 'Name', width: 30, headerColor: 'cyan', color: 'white', align: 'left' },
{ value: 'Age', width: 10, headerColor: 'cyan', color: 'white', align: 'right' }
];
const rows = [
['Alice', 30],
['Bob', 25]
];
const options = {
borderStyle: 'solid',
borderColor: 'blue',
paddingBottom: 0,
headerAlign: 'center',
align: 'center',
color: 'white'
};
const table = ttyTable(header, rows, options);
console.log(table.render());
Adding Footers
This feature allows you to add footers to the table, which can be used to display summary information or totals.
const ttyTable = require('tty-table');
const header = [
{ value: 'Name', width: 30, headerColor: 'cyan' },
{ value: 'Age', width: 10, headerColor: 'cyan' }
];
const rows = [
['Alice', 30],
['Bob', 25]
];
const footer = [
{ value: 'Total', colspan: 1, align: 'right' },
{ value: '2', align: 'right' }
];
const table = ttyTable(header, rows, { footer });
console.log(table.render());
Other packages similar to tty-table
cli-table
cli-table is another npm package for creating tables in the terminal. It offers similar functionality to tty-table but with a different API. cli-table is known for its simplicity and ease of use, making it a popular choice for basic table creation.
table
The table package provides a more advanced and flexible way to create tables in the terminal. It supports a wide range of customization options and is suitable for more complex table layouts. Compared to tty-table, the table package offers more features but may have a steeper learning curve.
easy-table
easy-table is a lightweight package for creating tables in the terminal. It focuses on simplicity and ease of use, making it a good choice for quick and straightforward table creation. While it may not offer as many customization options as tty-table, it is very easy to get started with.
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
- Automatic text wrapping
- Colors (nodejs)
- Optional callbacks on column values
- Header, body column alignment
- Padding
- Pass rows as either arrays or objects
- Works in the browser as well as nodejs
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, 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",
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 t1 = Table(header,rows,{
borderStyle : 1,
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
Running tests
grunt test
License
GPLv3 License
Copyright 2015, Tecfu.