1 | # tty-table 电传打字台
|
2 |
|
3 | [![Build Status](https://travis-ci.org/tecfu/tty-table.svg?branch=master)](https://travis-ci.org/tecfu/tty-table) [![NPM version](https://badge.fury.io/js/tty-table.svg)](http://badge.fury.io/js/tty-table)
|
4 |
|
5 | ---
|
6 |
|
7 | Display your data in a table using a terminal, browser, or browser console.
|
8 |
|
9 | ---
|
10 |
|
11 | ## [Examples](examples/)
|
12 |
|
13 | [See here for complete example list](examples/)
|
14 |
|
15 |
|
16 | ### Terminal (Static)
|
17 |
|
18 | [examples/styles-and-formatting.js](examples/styles-and-formatting.js)
|
19 |
|
20 | ![Static](https://cloud.githubusercontent.com/assets/7478359/15691679/07142030-273f-11e6-8f1e-25728d558a2d.png "Static Example")
|
21 |
|
22 | ### Terminal (Streaming)
|
23 |
|
24 | ```
|
25 | $ node examples/data/fake-stream.js | tty-table --format json --header examples/config/header.js
|
26 | ```
|
27 |
|
28 | ![Streaming](https://user-images.githubusercontent.com/7478359/51738817-47c25700-204d-11e9-9df1-04e478331658.gif "tty-table streaming example")
|
29 |
|
30 | - See the built-in help for the terminal version of tty-table with:
|
31 | ```
|
32 | $ tty-table -h
|
33 | ```
|
34 |
|
35 | ### Browser & Browser Console
|
36 |
|
37 | - [examples/browser-example.html](examples/browser-example.html)
|
38 |
|
39 | ![Browser Console Example](https://user-images.githubusercontent.com/7478359/74614563-cbcaff00-50e6-11ea-9101-5457497696b8.jpg "tty-table in the browser console")
|
40 |
|
41 | [Working Example in Browser](https://cdn.rawgit.com/tecfu/tty-table/master/examples/browser-example.html)
|
42 |
|
43 | <br/>
|
44 | <br/>
|
45 |
|
46 | ## API Reference
|
47 |
|
48 |
|
49 | <a name="new_Table_new"></a>
|
50 | ### Table(header ```array```, rows ```array```, options ```object```)
|
51 |
|
52 | | Param | Type | Description |
|
53 | | --- | --- | --- |
|
54 | | [header](#header_options) | <code>array</code> | Per-column configuration. An array of objects, one object for each column. Each object contains properties you can use to configure that particular column. [See available properties](#header_options) |
|
55 | | [rows](#rows_examples) | <code>array</code> | Your data. An array of arrays or objects. [See examples](#rows_examples) |
|
56 | | [options](#options_properties) | <code>object</code> | Global table configuration. [See available properties](#options_properties) |
|
57 |
|
58 |
|
59 | <br/>
|
60 | <a name="header_options"></a>
|
61 |
|
62 | #### header ```array of objects```
|
63 |
|
64 | | Param | Type | Description |
|
65 | | --- | --- | --- |
|
66 | | alias | <code>string</code> | Text to display in column header cell |
|
67 | | align | <code>string</code> | default: "center" |
|
68 | | color | <code>string</code> | default: terminal default color |
|
69 | | footerAlign | <code>string</code> | default: "center" |
|
70 | | footerColor | <code>string</code> | default: terminal default color |
|
71 | | formatter | <code>function(cellValue, columnIndex, rowIndex, rowData, inputData)</code> | Runs a callback on each cell value in the parent column. <br/>Use `this.style` within function body to style text, i.e. `this.style("mytext", "bold", "green", "underline")`. <br/>Please note that fat arrow functions `() => {}` don't support scope overrides, and this feature won't work within them. For a full list of options, see: [kleur](https://github.com/lukeed/kleur). |
|
72 | | headerAlign | <code>string</code> | default: "center" |
|
73 | | headerColor | <code>string</code> | default: terminal's default color |
|
74 | | marginLeft | <code>integer</code> | default: 0 |
|
75 | | marginTop | <code>integer</code> | default: 0 |
|
76 | | width | <code>string</code> \|\| <code>integer</code> | default: "auto" |
|
77 | | paddingBottom | <code>integer</code> | default: 0 |
|
78 | | paddingLeft | <code>integer</code> | default: 1 |
|
79 | | paddingRight | <code>integer</code> | default: 1 |
|
80 | | paddingTop | <code>integer</code> | default: 0 |
|
81 | | value | <code>string</code> | Name of the property to display in each cell when data passed as an array of objects |
|
82 |
|
83 |
|
84 | **Example**
|
85 |
|
86 | ```js
|
87 | let header = [{
|
88 | value: "item",
|
89 | headerColor: "cyan",
|
90 | color: "white",
|
91 | align: "left",
|
92 | width: 20
|
93 | },
|
94 | {
|
95 | value: "price",
|
96 | color: "red",
|
97 | width: 10,
|
98 | formatter: function (value) {
|
99 | let str = `$${value.toFixed(2)}`
|
100 | return (value > 5) ? this.style(str, "green", "bold") :
|
101 | this.style(str, "red", "underline")
|
102 | }
|
103 | }]
|
104 | ```
|
105 |
|
106 | <br/>
|
107 | <br/>
|
108 | <a name="rows_examples"></a>
|
109 |
|
110 | #### rows ```array```
|
111 |
|
112 | **Example**
|
113 | - each row an array
|
114 | ```js
|
115 | const rows = [
|
116 | ["hamburger",2.50],
|
117 | ]
|
118 | ```
|
119 | - each row an object
|
120 | ```js
|
121 | const rows = [
|
122 | {
|
123 | item: "hamburger",
|
124 | price: 2.50
|
125 | }
|
126 | ]
|
127 | ```
|
128 |
|
129 |
|
130 | <br/>
|
131 | <br/>
|
132 | <a name="footer_example"></a>
|
133 |
|
134 | #### footer ```array```
|
135 | - Footer is optional
|
136 |
|
137 | **Example**
|
138 | ```js
|
139 | const footer = [
|
140 | "TOTAL",
|
141 | function (cellValue, columnIndex, rowIndex, rowData) {
|
142 | let total = rowData.reduce((prev, curr) => {
|
143 | return prev + curr[1]
|
144 | }, 0)
|
145 | .toFixed(2)
|
146 |
|
147 | return this.style(`$${total}`, "italic")
|
148 | }
|
149 | ]
|
150 | ```
|
151 |
|
152 | <br/>
|
153 | <br/>
|
154 | <a name="options_properties"></a>
|
155 |
|
156 | #### options ```object```
|
157 |
|
158 | | Param | Type | Description |
|
159 | | --- | --- | --- |
|
160 | | borderStyle | <code>string</code> | default: "solid". "solid", "dashed", "none" |
|
161 | | borderColor | <code>string</code> | default: terminal default color |
|
162 | | color | <code>string</code> | default: terminal default color |
|
163 | | compact | <code>boolean</code> | default: false Removes horizontal lines when true. |
|
164 | | defaultErrorValue | <code>mixed</code> | default: '�' |
|
165 | | defaultValue | <code>mixed</code> | default: '?' |
|
166 | | errorOnNull | <code>boolean</code> | default: false |
|
167 | | truncate | <code>mixed</code> | default: false <br/> When this property is set to a string, cell contents will be truncated by that string instead of wrapped when they extend beyond of the width of the cell. <br/> For example if: <br/> <code>"truncate":"..."</code> <br/> the cell will be truncated with "..." |
|
168 |
|
169 | **Example**
|
170 | ```js
|
171 | const options = {
|
172 | borderStyle: 1,
|
173 | borderColor: "blue",
|
174 | headerAlign: "center",
|
175 | align: "left",
|
176 | color: "white",
|
177 | truncate: "..."
|
178 | }
|
179 | ```
|
180 |
|
181 | <br/>
|
182 |
|
183 | ### Table.render() ⇒ <code>String</code>
|
184 | <a name="Table.tableObject.render"></a>
|
185 |
|
186 | Add method to render table to a string
|
187 |
|
188 | **Example**
|
189 | ```js
|
190 | const out = Table(header,rows,options).render()
|
191 | console.log(out); //prints output
|
192 | ```
|
193 |
|
194 |
|
195 |
|
196 | <br/>
|
197 | <br/>
|
198 |
|
199 | ## Installation
|
200 |
|
201 | - [Terminal](docs/terminal.md):
|
202 |
|
203 | ```sh
|
204 | $ npm install tty-table -g
|
205 | ```
|
206 |
|
207 | - Node Module
|
208 |
|
209 | ```sh
|
210 | $ npm install tty-table
|
211 | ```
|
212 |
|
213 | - Browser
|
214 |
|
215 | ```html
|
216 | import Table from './dist/tty-table.esm.js'
|
217 |
|
218 | // other options:
|
219 | // let Table = require('tty-table') // dist/tty-table.cjs.js
|
220 | // let Table = TTY_Table; // dist/tty-table.umd.js
|
221 | ```
|
222 |
|
223 |
|
224 | ## Running tests
|
225 |
|
226 | ```sh
|
227 | $ npm test
|
228 | ```
|
229 |
|
230 | ## Saving the output of new unit tests
|
231 |
|
232 | ```sh
|
233 | $ npm run save-tests
|
234 | ```
|
235 | - Because:
|
236 |
|
237 | `node script.js --color=always`
|
238 |
|
239 | ## Dev Tips
|
240 |
|
241 | - To generate vim tags (make sure [jsctags](https://github.com/ramitos/jsctags) is installed globally)
|
242 |
|
243 | ```sh
|
244 | $ npm run tags
|
245 | ```
|
246 |
|
247 | - To generate vim tags on file save
|
248 |
|
249 | ```sh
|
250 | $ npm run watch-tags
|
251 | ```
|
252 |
|
253 | ## Pull Requests
|
254 |
|
255 | Pull requests are encouraged!
|
256 |
|
257 | - Please remember to add a unit test when necessary
|
258 | - Please format your commit messages according to the ["Conventional Commits"](https://www.conventionalcommits.org/en/v1.0.0/) specification
|
259 |
|
260 | If you aren't familiar with Conventional Commits, here's a good [article on the topic](https://dev.to/maniflames/how-conventional-commits-improved-my-git-skills-1jfk)
|
261 |
|
262 | TL/DR:
|
263 |
|
264 | - feat: a feature that is visible for end users.
|
265 | - fix: a bugfix that is visible for end users.
|
266 | - chore: a change that doesn't impact end users (e.g. chances to CI pipeline)
|
267 | - docs: a change in the README or documentation
|
268 | - refactor: a change in production code focused on readability, style and/or performance.
|
269 |
|
270 |
|
271 | ## [Packaging as a distributable](packaging.md)
|
272 |
|
273 |
|
274 | ## License
|
275 |
|
276 | [MIT License](https://opensource.org/licenses/MIT)
|
277 |
|
278 | Copyright 2015-2020, Tecfu.
|