UNPKG

5.95 kBMarkdownView Raw
1<h1 align="center">console-table-printer</h1>
2<h3 align="center">🖥️🍭Printing Pretty Tables on your console</h3>
3<p align="center">
4 <a href="https://travis-ci.org/ayonious/console-table-printer">
5 <img alt="Build Status" src="https://travis-ci.org/ayonious/console-table-printer.svg?branch=master">
6 </a>
7 <a href="https://codecov.io/gh/ayonious/console-table-printer">
8 <img alt="codecov" src="https://codecov.io/gh/ayonious/console-table-printer/branch/master/graph/badge.svg">
9 </a>
10 <a href="https://badge.fury.io/js/console-table-printer">
11 <img alt="npm version" src="https://badge.fury.io/js/console-table-printer.svg">
12 </a>
13 <a href="https://packagephobia.now.sh/result?p=console-table-printer">
14 <img alt="install size" src="https://packagephobia.now.sh/badge?p=console-table-printer@latest">
15 </a>
16</p>
17<p align="center">
18 <a href="https://github.com/prettier/prettier">
19 <img alt="code style: prettier" src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=plastic">
20 </a>
21 <a href="https://github.com/semantic-release/semantic-release">
22 <img alt="semantic-release" src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg">
23 </a>
24</p>
25
26## Synopsis
27
28Printing Simple Table with Coloring rows on your console. Its useful when you want to present some tables on console using js.
29
30## Installation
31
32```bash
33npm install console-table-printer --save
34```
35
36## Basic Example
37
38```javascript
39const { printTable } = require('console-table-printer');
40
41//Create a table
42const testCases = [
43 { index: 3, text: 'I would like some gelb bananen bitte', value: 100 },
44 { index: 4, text: 'I hope batch update is working', value: 300 },
45];
46
47//print
48printTable(testCases);
49```
50
51Output:
52
53![Screenshot](https://cdn.jsdelivr.net/gh/ayonious/console-table-printer@master/static-resources/quick-print.png)
54
55You can also create a Table instance and print it:
56
57```javascript
58const { Table } = require('console-table-printer');
59
60//Create a table
61const p = new Table();
62
63//add rows with color
64p.addRow({ index: 1, text: 'red wine please', value: 10.212 });
65p.addRow({ index: 2, text: 'green gemuse please', value: 20.0 });
66p.addRows([
67 //adding multiple rows are possible
68 { index: 3, text: 'gelb bananen bitte', value: 100 },
69 { index: 4, text: 'update is working', value: 300 },
70]);
71
72//print
73p.printTable();
74```
75
76![Screenshot](https://cdn.jsdelivr.net/gh/ayonious/console-table-printer@master/static-resources/screenshot-simple.png)
77
78You can also put some color to your table like this:
79
80```javascript
81const p = new Table();
82p.addRow({ index: 1, text: 'red wine', value: 10.212 }, { color: 'red' });
83p.addRow({ index: 2, text: 'green gemuse', value: 20.0 }, { color: 'green' });
84p.addRow({ index: 3, text: 'gelb bananen', value: 100 }, { color: 'yellow' });
85p.printTable();
86```
87
88![Screenshot](https://cdn.jsdelivr.net/gh/ayonious/console-table-printer@master/static-resources/screenshot-colored.png)
89
90You can also put properties based on columns (color/alignment)
91
92```javascript
93const p = new Table({
94 columns: [
95 { name: 'index', alignment: 'left', color: 'blue' }, //with alignment and color
96 { name: 'text', alignment: 'right' },
97 ],
98});
99
100p.addRow({ index: 1, text: 'red wine', value: 10.212 }, { color: 'green' });
101p.addRow({ index: 2, text: 'green gemuse', value: 20.0 });
102p.addRow(
103 { index: 3, text: 'gelb bananen', value: 100, is_priority_today: 'Y' },
104 { color: 'yellow' }
105);
106p.addRow(
107 { index: 3, text: 'rosa hemd wie immer', value: 100 },
108 { color: 'cyan' }
109);
110p.printTable();
111```
112
113![Screenshot](https://cdn.jsdelivr.net/gh/ayonious/console-table-printer@master/static-resources/screenshot-thin-border-column-props.2.png)
114
115## CLI
116
117There is also a CLI tool for printing Tables on Terminal directly [table-printer-cli](https://www.npmjs.com/package/table-printer-cli)
118
119## Documentation
120
121Official documentation has been moved here: [console-table-documentation](https://console-table.netlify.app)
122
123### Table instance creation
124
1253 ways to Table Instance creation:
126
1271. Simplest way `new Table()`
128
1292. Only with column names: `new Table(['column1', 'column2', 'column3'])`
130
1313. Detailed way of creating table instance
132
133```javascript
134new Table({
135 title: 'Title of the Table', // A text showsup on top of table (optoinal)
136 columns: [
137 { name: 'column1', alignment: 'left', color: 'red' }, //with alignment and color
138 { name: 'column2', alignment: 'right' },
139 { name: 'column3' },
140 ],
141 sort: (row1, row2) => row2.column1 - row1.column1, // sorting order of rows (optional), this is normal js sort function for Array.sort
142 filter: (row) => row.column1 < 3, // filtering rows (optional)
143 enabledColumns: ['column1'], // array of columns that you want to see, all other will be ignored (optional)
144 disabledColumns: ['column2'], // array of columns that you DONT want to see, these will always be hidden
145});
146```
147
148### Functions
149
150- `addRow(rowObjet, options)` adding single row.
151- `addRows(rowObjects, options)` adding multiple rows. array of row object. This case options will be applied to all the objects in row
152- `addColumn(columnObject)` adding single column
153- `addColumns(columnObjects)` adding multiple columns
154- `printTable()` Prints the table on your console
155
156### possible `color` values for rows
157
158Check Docs: [color-vals](https://console-table.netlify.app/docs/doc-color)
159
160Example usage: To Create a row of color blue
161
162```js
163table.addRow(rowObject, { color: 'blue' });
164```
165
166Example usage: To apply blue for all rows
167
168```js
169table.addRows(rowsArray, { color: 'blue' });
170```
171
172### possible `alignment` values for columns
173
174Check Docs: [alignment-vals](https://console-table.netlify.app/docs/doc-alignment)
175
176### Typesccript Support
177
178You can get color / alignment as types. Check Docs: [types-docs](https://console-table.netlify.app/docs/doc-typescript)
179
180## License
181
182[MIT](https://github.com/ayonious/console-table-printer/blob/master/LICENSE)