Yet another library for printing tables to the terminal. In this case though, there is no expectation that you will have all the data ahead of time. Instead, you can set up your column definitions ahead of time, and data will be printed to the terminal as it is provided, allowing to provide feedback immediately as you fetch more and more data.
If you do have all you data already, consider using fancy-text-table
, which will be able to automatically size the columns for you.
Initializes a new table. All options are optional, with the defaults resulting in printing directory to console similar to console.log(param1, param2, param3, ...)
. The following options are available:
columns
{Array[{Object}]}: definitions for each column, in order. Each column has the following properties:size
{Integer}: the number of characters in each cellalign
{String}: how to align the colums, eitherleft
,right
, orcenter
char
{String}: the character to use when padding the string for alignment
separator
{String}: the text to use to separate columns
const table = require('live-text-table')({
columns: [
{ size: 2, align: 'left' },
{ size: 45, align: 'left', char: chalk.gray('.') },
{ size: 10, align: 'right' }
],
separator: ' | '
});
Each instance of a table has the following methods:
Creates a title. This string will span across cells of a table, and not factor in cell alignment.
table.title('This Is My Table');
Creates a row of items as defined in the table options. Note that if a value is larger than the defined size, it will push the rest of the content and misalign that row in the table.
table.row('1', 'value 1', 1234);
table.row('2', 'value 2', 5678);
table.row('3', 'value 3', 90);
Creates an empty line in teh table. This is useful if you want to break up chunkcs of cells.
table.line();
Full table example. Feel free to use colors anywhere you'd like to make your tables even prettier!
const chalk = require('chalk');
const table = require('live-text-table')({
columns: [
{ size: 2, align: 'left' },
{ size: 15, align: 'left', char: chalk.gray('.') },
{ size: 6, align: 'right' }
],
separator: ' | '
});
table.title('My Fruits');
table.line();
table.row(chalk.dim('#1'), chalk.cyan('pineapples'), 1234);
table.row(chalk.dim('#2'), chalk.cyan('oranges'), 5678);
table.row(chalk.dim('#3'), chalk.cyan('bananas'), 90);
// My Fruits
//
// #1 | pineapples..... | 1234
// #2 | oranges........ | 5678
// #3 | bananas........ | 90