What is console-table-printer?
The console-table-printer npm package is a utility for printing tables to the console in a visually appealing way. It is useful for developers who need to display tabular data in a readable format directly in the terminal.
What are console-table-printer's main functionalities?
Basic Table Printing
This feature allows you to print a basic table with rows of data. The code sample demonstrates how to create a table, add rows to it, and print it to the console.
const { Table } = require('console-table-printer');
const p = new Table();
p.addRow({ index: 1, name: 'John', age: 25 });
p.addRow({ index: 2, name: 'Jane', age: 30 });
p.printTable();
Custom Column Formatting
This feature allows you to customize the alignment of columns in the table. The code sample shows how to set different alignments for each column.
const { Table } = require('console-table-printer');
const p = new Table({
columns: [
{ name: 'index', alignment: 'left' },
{ name: 'name', alignment: 'center' },
{ name: 'age', alignment: 'right' }
]
});
p.addRow({ index: 1, name: 'John', age: 25 });
p.addRow({ index: 2, name: 'Jane', age: 30 });
p.printTable();
Colorful Table Output
This feature allows you to add colors to the table output for better visual distinction. The code sample demonstrates how to set different colors for each column.
const { Table } = require('console-table-printer');
const p = new Table({
columns: [
{ name: 'index', color: 'red' },
{ name: 'name', color: 'green' },
{ name: 'age', color: 'blue' }
]
});
p.addRow({ index: 1, name: 'John', age: 25 });
p.addRow({ index: 2, name: 'Jane', age: 30 });
p.printTable();
Other packages similar to console-table-printer
cli-table
cli-table is another package for printing tables to the console. It offers similar functionality but with a different API. It is slightly less feature-rich compared to console-table-printer but is still a solid choice for basic table printing needs.
table
The table package provides a more flexible and customizable way to print tables to the console. It offers advanced features like custom border styles and text wrapping, making it a good alternative for users who need more control over the table's appearance.
easy-table
easy-table is a lightweight package for printing tables to the console. It focuses on simplicity and ease of use, making it a good choice for users who need to quickly display tabular data without much configuration.
Print Pretty tables on Your Console
Synopsis
Printing Simple Table with Coloring rows on your console. Its useful when you want to present some tables on console. Using as less code possible you can print the table on console.
Installation
npm install console-table-printer --save
Basic Example
const {Table} = require('console-table-printer');
const p = new Table();
p.addRow({ index: 1, text: 'red wine please', value: 10.212 });
p.addRow({ index: 2, text: 'green gemuse please', value: 20.00 });
p.addRows([
{ index: 3, text: 'gelb bananen bitte', value: 100 },
{ index: 4, text: 'update is working', value: 300}
]);
p.printAll();
Output:
You can also put some color to your table like this:
const p = new Table();
p.addRow({ index: 1, text: 'red wine', value: 10.212 }, {color: 'red'});
p.addRow({ index: 2, text: 'green gemuse', value: 20.00 }, {color: 'green'});
p.addRow({ index: 3, text: 'gelb bananen', value: 100 }, {color: 'yellow'});
p.printAll();
Output:
License
MIT