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 |
|
28 | Printing 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
|
33 | npm install console-table-printer --save
|
34 | ```
|
35 |
|
36 | ## Basic Example
|
37 |
|
38 | ```javascript
|
39 | const { printTable } = require('console-table-printer');
|
40 |
|
41 | //Create a table
|
42 | const 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
|
48 | printTable(testCases);
|
49 | ```
|
50 |
|
51 | Output:
|
52 |
|
53 | ![Screenshot](https://cdn.jsdelivr.net/gh/ayonious/console-table-printer@master/static-resources/quick-print.png)
|
54 |
|
55 | You can also create a Table instance and print it:
|
56 |
|
57 | ```javascript
|
58 | const { Table } = require('console-table-printer');
|
59 |
|
60 | //Create a table
|
61 | const p = new Table();
|
62 |
|
63 | //add rows with color
|
64 | p.addRow({ index: 1, text: 'red wine please', value: 10.212 });
|
65 | p.addRow({ index: 2, text: 'green gemuse please', value: 20.0 });
|
66 | p.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
|
73 | p.printTable();
|
74 | ```
|
75 |
|
76 | ![Screenshot](https://cdn.jsdelivr.net/gh/ayonious/console-table-printer@master/static-resources/screenshot-simple.png)
|
77 |
|
78 | You can also put some color to your table like this:
|
79 |
|
80 | ```javascript
|
81 | const p = new Table();
|
82 | p.addRow({ index: 1, text: 'red wine', value: 10.212 }, { color: 'red' });
|
83 | p.addRow({ index: 2, text: 'green gemuse', value: 20.0 }, { color: 'green' });
|
84 | p.addRow({ index: 3, text: 'gelb bananen', value: 100 }, { color: 'yellow' });
|
85 | p.printTable();
|
86 | ```
|
87 |
|
88 | ![Screenshot](https://cdn.jsdelivr.net/gh/ayonious/console-table-printer@master/static-resources/screenshot-colored.png)
|
89 |
|
90 | You can also put properties based on columns (color/alignment)
|
91 |
|
92 | ```javascript
|
93 | const p = new Table({
|
94 | columns: [
|
95 | { name: 'index', alignment: 'left', color: 'blue' }, //with alignment and color
|
96 | { name: 'text', alignment: 'right' },
|
97 | ],
|
98 | });
|
99 |
|
100 | p.addRow({ index: 1, text: 'red wine', value: 10.212 }, { color: 'green' });
|
101 | p.addRow({ index: 2, text: 'green gemuse', value: 20.0 });
|
102 | p.addRow(
|
103 | { index: 3, text: 'gelb bananen', value: 100, is_priority_today: 'Y' },
|
104 | { color: 'yellow' }
|
105 | );
|
106 | p.addRow(
|
107 | { index: 3, text: 'rosa hemd wie immer', value: 100 },
|
108 | { color: 'cyan' }
|
109 | );
|
110 | p.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 |
|
117 | There 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 |
|
121 | Official documentation has been moved here: [console-table-documentation](https://console-table.netlify.app)
|
122 |
|
123 | ### Table instance creation
|
124 |
|
125 | 3 ways to Table Instance creation:
|
126 |
|
127 | 1. Simplest way `new Table()`
|
128 |
|
129 | 2. Only with column names: `new Table(['column1', 'column2', 'column3'])`
|
130 |
|
131 | 3. Detailed way of creating table instance
|
132 |
|
133 | ```javascript
|
134 | new 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 |
|
158 | Check Docs: [color-vals](https://console-table.netlify.app/docs/doc-color)
|
159 |
|
160 | Example usage: To Create a row of color blue
|
161 |
|
162 | ```js
|
163 | table.addRow(rowObject, { color: 'blue' });
|
164 | ```
|
165 |
|
166 | Example usage: To apply blue for all rows
|
167 |
|
168 | ```js
|
169 | table.addRows(rowsArray, { color: 'blue' });
|
170 | ```
|
171 |
|
172 | ### possible `alignment` values for columns
|
173 |
|
174 | Check Docs: [alignment-vals](https://console-table.netlify.app/docs/doc-alignment)
|
175 |
|
176 | ### Typesccript Support
|
177 |
|
178 | You 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)
|