• Stars
    star
    171
  • Rank 221,597 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ–ฅ๏ธ ๐Ÿญ Printing Pretty Tables on your console

console-table-printer

๐Ÿ–ฅ๏ธ๐ŸญPrinting Pretty Tables on your console

codecov npm version install size

code style: prettier semantic-release

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 table
const testCases = [
  { index: 3, text: 'I would like some gelb bananen bitte', value: 100 },
  { index: 4, text: 'I hope batch update is working', value: 300 },
];

//print
printTable(testCases);

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 table
const p = new Table();

//add rows with color
p.addRow({ index: 1, text: 'red wine please', value: 10.212 });
p.addRow({ index: 2, text: 'green gemuse please', value: 20.0 });
p.addRows([
  //adding multiple rows are possible
  { index: 3, text: 'gelb bananen bitte', value: 100 },
  { index: 4, text: 'update is working', value: 300 },
]);

//print
p.printTable();

Screenshot

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.0 }, { color: 'green' });
p.addRow({ index: 3, text: 'gelb bananen', value: 100 }, { color: 'yellow' });
p.printTable();

Screenshot

You can also put properties based on columns (color/alignment/title)

const p = new Table({
  columns: [
    { name: 'index', alignment: 'left', color: 'blue' }, // with alignment and color
    { name: 'text', alignment: 'right' },
    { name: 'is_priority_today', title: 'Is This Priority?' }, // with Title as separate Text
  ],
  colorMap: {
    custom_green: '\x1b[32m', // define customized color
  },
});

p.addRow({ index: 1, text: 'red wine', value: 10.212 }, { color: 'green' });
p.addRow(
  { index: 2, text: 'green gemuse', value: 20.0 },
  { color: 'custom_green' } // your green
);
p.addRow(
  { index: 3, text: 'gelb bananen', value: 100, is_priority_today: 'Y' },
  { color: 'yellow' }
);
p.addRow(
  { index: 3, text: 'rosa hemd wie immer', value: 100 },
  { color: 'cyan' }
);
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: 'Title of the Table', // A text showsup on top of table (optoinal)
  columns: [
    { name: 'column1', alignment: 'left', color: 'red' }, // with alignment and color
    { name: 'column2', alignment: 'right', maxLen: 30 }, // lines bigger than this will be splitted in multiple lines
    { name: 'column3', title: 'Column3' }, // Title is what will be shown while printing, by default title = name
  ],
  rows: [{ column1: 'row1' }, { column2: 'row2' }, { column3: 'row3' }],
  sort: (row1, row2) => row2.column1 - row1.column1, // sorting order of rows (optional), this is normal js sort function for Array.sort
  filter: (row) => row.column1 < 3, // filtering rows (optional)
  enabledColumns: ['column1'], // array of columns that you want to see, all other will be ignored (optional)
  disabledColumns: ['column2'], // array of columns that you DONT want to see, these will always be hidden
  colorMap: {
    custom_green: '\x1b[32m', // define customized color
  },
  charLength: {
    '๐Ÿ‘‹': 2,
    '๐Ÿ˜…': 2,
  }, // custom len of chars in console
});

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

More Repositories

1

File-Compression

๐Ÿ“ฆ Zip and Unzip files using Huffman's code and LZW Algorithm
Java
34
star
2

Vagrant-Best-Practices

โœŒLearn interesting features of Vagrant By practicing simple examples
17
star
3

Java-Best-Practices-Java8

โ˜• Functional Programming, Lamda, Stream, Function, Predicate
Java
8
star
4

lombok-examples

๐ŸŒถ๏ธPractice examples on Using Lombok: https://projectlombok.org/
Java
7
star
5

random-names

๐ŸŽฒ Random names generator
TypeScript
5
star
6

C-Code-Beautifier

โœ‚๏ธ A parser that can beautify Code written in C/C++
C++
5
star
7

simple-wcswidth

๐Ÿ–ฅ๏ธ ๐Ÿ’ฌ Simple implementation of wcswidth() of Markus Kuhn's C Code
TypeScript
4
star
8

react-context-example

Example Project to show Theme change using Context in React
TypeScript
3
star
9

nested-rules-engine

๐ŸŒฒ Decision Tree based Rules Engine
TypeScript
3
star
10

that-it-guy-tech-blog

๐ŸŽง My Personal Tech Blog
MDX
2
star
11

saltstack-best-practices-basics

๐ŸฅจLearn Saltstack by practicing simple examples
1
star
12

interview-cheatsheet

๐Ÿ‘” Common questions for tech interviews
TypeScript
1
star
13

console-table-docu

๐Ÿ–ฅ๏ธ ๐Ÿญ ๐Ÿ“– Documentation guide for using Console Table Printer
JavaScript
1
star
14

table-printer-cli

๐Ÿฆพ CLI to print table in terminal
TypeScript
1
star
15

gradle-tutorial

๐Ÿ˜ Learn interesting features of Gradle By practicing simple examples
Java
1
star
16

my-npm-packages-tester

๐Ÿ”ฌ๐Ÿ‘ทTesting all my npm packages in nodejs
JavaScript
1
star