UNPKG

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