Detalhes do pacote

console-table-printer

Printing pretty tables on console log

console-table, console-log, print-table, node-table-printing

readme (leia-me)

console-table-printer

🖥️🍭Printing Pretty Tables on your console

NPM Downloads install size npm version codecov

Synopsis

Printing Simple Table with Coloring rows on your console. Its useful when you want to present some tables on console using js.

Installation

npm install console-table-printer --save

Basic Example

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);

Screenshot

🚨🚨Announcement🚨🚨 Official Documentation is moved Here

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();

Screenshot

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();

Screenshot

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();

Screenshot

CLI

There is also a CLI tool for printing Tables on Terminal directly table-printer-cli

Documentation

Official documentation has been moved here: console-table-documentation

Table instance creation

3 ways to Table Instance creation:

  1. Simplest way new Table()

  2. Only with column names: new Table(['column1', 'column2', 'column3'])

  3. 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,
  },
});

Functions

  • addRow(rowObjet, options) adding single row. This can be chained
  • addRows(rowObjects, options) adding multiple rows. array of row object. This case options will be applied to all the objects in row
  • addColumn(columnObject) adding single column
  • addColumns(columnObjects) adding multiple columns
  • printTable() Prints the table on your console

possible color values for rows

Check 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' });

possible alignment values for columns

Check Docs: alignment-vals

Typescript Support

You can get color / alignment as types. Check Docs: types-docs

License

MIT

changelog (log de mudanças)

2.14.4 (2025-06-24)

Bug Fixes

  • adding some docs regarding the exported types (#539) (5b13545)

2.14.3 (2025-06-10)

Bug Fixes

  • change return type for row filter function (boolean instead Boolean) (#526) (48bafca)

2.14.2 (2025-06-08)

Bug Fixes

2.14.1 (2025-06-01)

Bug Fixes

2.14.0 (2025-05-27)

Bug Fixes

Features

  • allow options with printTable function (#485) (74c96ea)

2.13.0 (2025-05-23)

Bug Fixes

  • Allow Column object adding for Table instance creation (#478) (3bd08df)

Features

  • Allow configurable Default styles for columns (#479) (51bc334)
  • The Computed Column function is passed row, index, and array (#464) (d6793c6)

2.12.1 (2024-06-20)

Bug Fixes

2.12.0 (2023-12-22)

Bug Fixes

Features

  • Allow for color: false to wipe the colorMap. (#455) (9405d83)

2.11.2 (2023-07-02)

Bug Fixes

  • Adding Better regex for ansi codes stripping from the string (#435) (a90525f)
  • Allow empty Column Title (#451) (4323d60)
  • Remove Crimson as basic color (#450) (ec89482)

2.11.1 (2022-08-24)

Bug Fixes

  • add character length dictionary for string length computation in console (#431) (89f6db2)
  • dont allow negative numbers in string.repeat (#422) (c9e8994)
  • Fix color resetting for borders (#428) (bcb6e8c)

2.11.0 (2022-04-03)

Features

  • add option to specify initial rows, make add methods chainable, export renderSimpleTable (#396) (6e6d8f3)
  • allow specifying custom color map (#394) (4c6eecd)

2.10.0 (2021-08-04)

Bug Fixes

  • row-separator: make row separator optional (#373) (325db51)

Features

  • row separator: Added row separator option (#372) (63855ae)

2.9.0 (2021-05-12)

Bug Fixes

  • technical: fixing types error for compilation (68a8bf7)

Features

2.8.2 (2021-03-19)

Bug Fixes

2.8.1 (2021-03-06)

Bug Fixes

2.8.0 (2021-03-06)

Features

2.7.5 (2020-12-08)

Bug Fixes

  • technical: migrate to yarn and fix deployment (0a1babb)

2.7.4 (2020-12-08)

Bug Fixes

  • Asian Languages width fixing with simple-wcswidth (a14272f)

2.7.3 (2020-12-06)

Bug Fixes

  • revert wcwidth and special character support (#329) (d6e7847)

2.7.2 (2020-12-06)

Bug Fixes

  • column width calculation with special characters (#303) (5a288e7)

2.7.1 (2020-12-05)

Bug Fixes

  • docs: readme update for Header title (#321) (572979f)

2.7.0 (2020-12-05)

Features

  • Header title - View Header Title in console instead of ugly header name (#319) (78bb5aa)

2.6.0 (2020-12-04)

Features

2.5.1 (2020-11-07)

Bug Fixes

  • technical: update dependency eslint to ^7.13.0 and remove package-lock deps updates (#290) (adc9c05)

2.5.0 (2020-11-04)

Features