
Research
/Security News
Toptal’s GitHub Organization Hijacked: 10 Malicious Packages Published
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
console-table-printer
Advanced tools
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.
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();
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.
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 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.
🖥️🍭Printing Pretty Tables on your console
Printing Simple Table with Coloring rows on your console. Its useful when you want to present some tables on console using js.
npm install console-table-printer --save
const { printTable } = require('console-table-printer');
// Create a simple task list
const tasks = [
{ id: 1, task: 'Fix login bug', priority: 'High', status: 'In Progress' },
{ id: 2, task: 'Update documentation', priority: 'Medium', status: 'Done' },
{ id: 3, task: 'Add unit tests', priority: 'High', status: 'Todo' },
];
// Print the table
printTable(tasks);
You can also create a Table instance and print it:
const { Table } = require('console-table-printer');
// Create a game leaderboard
const leaderboard = new Table();
// Add players with their scores
leaderboard.addRow({ rank: 1, player: 'Alice', score: 1250, level: 'Master' });
leaderboard.addRow({ rank: 2, player: 'Bob', score: 1180, level: 'Expert' });
leaderboard.addRows([
{ rank: 3, player: 'Charlie', score: 1050, level: 'Advanced' },
{ rank: 4, player: 'Diana', score: 920, level: 'Intermediate' },
]);
// Print the leaderboard
leaderboard.printTable();
You can also put some color to your table like this:
const p = new Table();
p.addRow({ item: 'Pizza', price: 12.99, rating: '5/5' }, { color: 'red' });
p.addRow({ item: 'Burger', price: 8.99, rating: '4/5' }, { color: 'green' });
p.addRow({ item: 'Ramen', price: 15.99, rating: '5/5' }, { color: 'yellow' });
p.addRow({ item: 'Salad', price: 6.99, rating: '3/5' }, { color: 'cyan' });
p.printTable();
You can also put properties based on columns (color/alignment/title)
const p = new Table({
title: 'Project Status',
columns: [
{ name: 'id', alignment: 'left', color: 'blue' },
{ name: 'project', alignment: 'left' },
{ name: 'status', title: 'Current Status' },
],
colorMap: {
urgent: '\x1b[31m',
on_track: '\x1b[32m',
},
});
p.addRow({ id: 1, project: 'Website Redesign', status: 'On Track' }, { color: 'on_track' });
p.addRow({ id: 2, project: 'Mobile App', status: 'Behind Schedule' }, { color: 'urgent' });
p.addRow({ id: 3, project: 'API Integration', status: 'Completed' }, { color: 'green' });
p.printTable();
There is also a CLI tool for printing Tables on Terminal directly table-printer-cli
Official documentation has been moved here: console-table-documentation
3 ways to Table Instance creation:
Simplest way new Table()
Only with column names: new Table(['column1', 'column2', 'column3'])
Detailed way of creating table instance
new Table({
title: '📊 Sales Report Q4 2024', // A text showsup on top of table (optional)
columns: [
{ name: 'region', alignment: 'left', color: 'blue' }, // with alignment and color
{ name: 'sales', alignment: 'right', maxLen: 30 }, // lines bigger than this will be splitted in multiple lines
{ name: 'growth', title: 'Growth %' }, // Title is what will be shown while printing, by default title = name
],
rows: [
{ region: 'North America', sales: '$2.5M', growth: '+15%' },
{ region: 'Europe', sales: '$1.8M', growth: '+8%' },
{ region: 'Asia Pacific', sales: '$3.2M', growth: '+22%' },
],
sort: (row1, row2) => row2.sales - row1.sales, // sorting order of rows (optional), this is normal js sort function for Array.sort
filter: (row) => row.growth > '+10%', // filtering rows (optional)
enabledColumns: ['region', 'sales'], // array of columns that you want to see, all other will be ignored (optional)
disabledColumns: ['growth'], // array of columns that you DONT want to see, these will always be hidden
colorMap: {
high_growth: '\x1b[32m', // define customized color
},
charLength: {
'👋': 2,
'😅': 2,
}, // custom len of chars in console
defaultColumnOptions: {
alignment: 'center',
color: 'red',
maxLen: 40,
minLen: 20,
},
});
addRow(rowObjet, options)
adding single row. This can be chainedaddRows(rowObjects, options)
adding multiple rows. array of row object. This case options will be applied to all the objects in rowaddColumn(columnObject)
adding single columnaddColumns(columnObjects)
adding multiple columnsprintTable()
Prints the table on your consolecolor
values for rowsCheck Docs: color-vals
Example usage: To Create a row of color blue
table.addRow(rowObject, { color: 'blue' });
Example usage: To apply blue for all rows
table.addRows(rowsArray, { color: 'blue' });
alignment
values for columnsCheck Docs: alignment-vals
You can get color / alignment as types. Check Docs: types-docs
FAQs
Printing pretty tables on console log
The npm package console-table-printer receives a total of 1,096,033 weekly downloads. As such, console-table-printer popularity was classified as popular.
We found that console-table-printer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
Research
/Security News
Socket researchers investigate 4 malicious npm and PyPI packages with 56,000+ downloads that install surveillance malware.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.