tty-table 4.2.3 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -229
- package/adapters/automattic-cli-table.js +1 -1
- package/adapters/default-adapter.js +1 -1
- package/adapters/terminal-adapter.js +3 -2
- package/dist/browser/tty-table.global.js +4411 -0
- package/dist/browser/tty-table.global.js.map +1 -0
- package/dist/browser-BC4npgub.d.mts +128 -0
- package/dist/browser-BC4npgub.d.ts +128 -0
- package/dist/browser.d.mts +1 -0
- package/dist/browser.d.ts +1 -0
- package/dist/browser.js +1020 -0
- package/dist/browser.js.map +1 -0
- package/dist/browser.mjs +1016 -0
- package/dist/browser.mjs.map +1 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +1113 -0
- package/dist/cli.js.map +1 -0
- package/dist/cli.mjs +1118 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +1020 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1016 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +38 -59
- package/src/defaults.js +0 -71
- package/src/factory.d.ts +0 -67
- package/src/factory.js +0 -137
- package/src/format.js +0 -282
- package/src/main.js +0 -8
- package/src/render.js +0 -398
- package/src/style.js +0 -58
package/README.md
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
# tty-table
|
|
1
|
+
# tty-table
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
Display your data in a table using a terminal, browser, or browser console.
|
|
7
|
-
|
|
8
|
-
---
|
|
3
|
+
A TypeScript-first terminal table renderer with a compatibility-oriented factory API.
|
|
9
4
|
|
|
10
5
|
## [Examples](examples/)
|
|
11
6
|
|
|
@@ -57,246 +52,61 @@ $ tty-table -h
|
|
|
57
52
|
<br/>
|
|
58
53
|
<br/>
|
|
59
54
|
|
|
60
|
-
##
|
|
61
|
-
<!--API-REF-->
|
|
62
|
-
|
|
63
|
-
<a name="new_Table_new"></a>
|
|
64
|
-
### Table(header ```array```, rows ```array```, options ```object```)
|
|
65
|
-
|
|
66
|
-
| Param | Type | Description |
|
|
67
|
-
| --- | --- | --- |
|
|
68
|
-
| [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) |
|
|
69
|
-
| [rows](#rows_examples) | <code>array</code> | Your data. An array of arrays or objects. [See examples](#rows_examples) |
|
|
70
|
-
| [options](#options_properties) | <code>object</code> | Global table configuration. [See available properties](#options_properties) |
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
<br/>
|
|
74
|
-
<a name="header_options"></a>
|
|
75
|
-
|
|
76
|
-
#### header ```array of objects```
|
|
77
|
-
|
|
78
|
-
| Param | Type | Description |
|
|
79
|
-
| --- | --- | --- |
|
|
80
|
-
| alias | <code>string</code> | Text to display in column header cell |
|
|
81
|
-
| align | <code>string</code> | default: "center" |
|
|
82
|
-
| color | <code>string</code> | default: terminal default color |
|
|
83
|
-
| footerAlign | <code>string</code> | default: "center" |
|
|
84
|
-
| footerColor | <code>string</code> | default: terminal default color |
|
|
85
|
-
| formatter | <code>function(cellValue, columnIndex, rowIndex, rowData, inputData</code> | Runs a callback on each cell value in the parent column. <br/>Please note that fat arrow functions `() => {}` don't support scope overrides, and this feature won't work correctly within them. |
|
|
86
|
-
| @formatter configure | <code>function(object)</code> | Configure cell properties. For example: <br/>`this.configure({ truncate: false, align: "left" })` [More here](https://github.com/tecfu/tty-table/blob/master/examples/truncated-lines.js#L100-L110). |
|
|
87
|
-
| @formatter resetStyle | <code>function(cellValue)</code> | Removes ANSI escape sequences. For example: <br/>`this.resetStyle("[32m myText[39m") // "myText"`<br/> |
|
|
88
|
-
| @formatter style | <code>function(cellValue, effect)</code> | Style cell value. For example: <br/>`this.style("mytext", "bold", "green", "underline")`<br/>For a full list of options in the terminal: [chalk](https://github.com/chalk/chalk). For a full list of options in the browser: [kleur](https://github.com/lukeed/kleur)|
|
|
89
|
-
| headerAlign | <code>string</code> | default: "center" |
|
|
90
|
-
| headerColor | <code>string</code> | default: terminal's default color |
|
|
91
|
-
| marginLeft | <code>integer</code> | default: 0 |
|
|
92
|
-
| marginTop | <code>integer</code> | default: 0 |
|
|
93
|
-
| paddingBottom | <code>integer</code> | default: 0 |
|
|
94
|
-
| paddingLeft | <code>integer</code> | default: 1 |
|
|
95
|
-
| paddingRight | <code>integer</code> | default: 1 |
|
|
96
|
-
| paddingTop | <code>integer</code> | default: 0 |
|
|
97
|
-
| value | <code>string</code> | Name of the property to display in each cell when data passed as an array of objects |
|
|
98
|
-
| width | <code>string</code> \|\| <code>integer</code> | default: "auto" <br/> Can be a percentage of table width i.e. "20%" or a fixed number of columns i.e. "20". <br/> When set to the default ("auto"), the column widths are made proportionate by the longest value in each column. <br/> Note: Percentage columns and fixed value colums not intended to be mixed in the same table.|
|
|
99
|
-
|
|
100
|
-
**Example**
|
|
101
|
-
|
|
102
|
-
```js
|
|
103
|
-
let header = [{
|
|
104
|
-
value: "item",
|
|
105
|
-
headerColor: "cyan",
|
|
106
|
-
color: "white",
|
|
107
|
-
align: "left",
|
|
108
|
-
width: 20
|
|
109
|
-
},
|
|
110
|
-
{
|
|
111
|
-
value: "price",
|
|
112
|
-
color: "red",
|
|
113
|
-
width: 10,
|
|
114
|
-
formatter: function (value) {
|
|
115
|
-
let str = `$${value.toFixed(2)}`
|
|
116
|
-
return (value > 5) ? this.style(str, "green", "bold") :
|
|
117
|
-
this.style(str, "red", "underline")
|
|
118
|
-
}
|
|
119
|
-
}]
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
<br/>
|
|
123
|
-
<br/>
|
|
124
|
-
<a name="rows_examples"></a>
|
|
125
|
-
|
|
126
|
-
#### rows ```array```
|
|
127
|
-
|
|
128
|
-
**Example**
|
|
129
|
-
- each row an array
|
|
130
|
-
```js
|
|
131
|
-
const rows = [
|
|
132
|
-
["hamburger",2.50],
|
|
133
|
-
]
|
|
134
|
-
```
|
|
135
|
-
- each row an object
|
|
136
|
-
```js
|
|
137
|
-
const rows = [
|
|
138
|
-
{
|
|
139
|
-
item: "hamburger",
|
|
140
|
-
price: 2.50
|
|
141
|
-
}
|
|
142
|
-
]
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
<br/>
|
|
147
|
-
<br/>
|
|
148
|
-
<a name="footer_example"></a>
|
|
149
|
-
|
|
150
|
-
#### footer ```array```
|
|
151
|
-
- Footer is optional
|
|
152
|
-
|
|
153
|
-
**Example**
|
|
154
|
-
```js
|
|
155
|
-
const footer = [
|
|
156
|
-
"TOTAL",
|
|
157
|
-
function (cellValue, columnIndex, rowIndex, rowData) {
|
|
158
|
-
let total = rowData.reduce((prev, curr) => {
|
|
159
|
-
return prev + curr[1]
|
|
160
|
-
}, 0)
|
|
161
|
-
.toFixed(2)
|
|
162
|
-
|
|
163
|
-
return this.style(`$${total}`, "italic")
|
|
164
|
-
}
|
|
165
|
-
]
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
<br/>
|
|
169
|
-
<br/>
|
|
170
|
-
<a name="options_properties"></a>
|
|
171
|
-
|
|
172
|
-
#### options ```object```
|
|
173
|
-
|
|
174
|
-
| Param | Type | Description |
|
|
175
|
-
| --- | --- | --- |
|
|
176
|
-
| borderStyle | <code>string</code> | default: "solid". <br/> options: "solid", "dashed", "none" |
|
|
177
|
-
| borderColor | <code>string</code> | default: terminal default color |
|
|
178
|
-
| color | <code>string</code> | default: terminal default color |
|
|
179
|
-
| compact | <code>boolean</code> | default: false <br/> Removes horizontal borders when true. |
|
|
180
|
-
| defaultErrorValue | <code>mixed</code> | default: '�' |
|
|
181
|
-
| defaultValue | <code>mixed</code> | default: '?' |
|
|
182
|
-
| errorOnNull | <code>boolean</code> | default: false |
|
|
183
|
-
| 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 "..." <br/> Note: tty-table wraps overflowing cell text into multiple lines by default, so you would likely only utilize `truncate` for extremely long values. |
|
|
184
|
-
| width | <code>string</code> | default: "100%" <br/> Width of the table. Can be a percentage of i.e. "50%" or a fixed number of columns in the terminal viewport i.e. "100". <br/> Note: When you use a percentage, your table will be "responsive".|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
**Example**
|
|
188
|
-
```js
|
|
189
|
-
const options = {
|
|
190
|
-
borderStyle: "solid",
|
|
191
|
-
borderColor: "blue",
|
|
192
|
-
headerAlign: "center",
|
|
193
|
-
align: "left",
|
|
194
|
-
color: "white",
|
|
195
|
-
truncate: "...",
|
|
196
|
-
width: "90%"
|
|
197
|
-
}
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
<br/>
|
|
201
|
-
|
|
202
|
-
### Table.render() ⇒ <code>String</code>
|
|
203
|
-
<a name="Table.tableObject.render"></a>
|
|
204
|
-
|
|
205
|
-
Add method to render table to a string
|
|
206
|
-
|
|
207
|
-
**Example**
|
|
208
|
-
```js
|
|
209
|
-
const out = Table(header,rows,options).render()
|
|
210
|
-
console.log(out); //prints output
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
<!--END-API-REF-->
|
|
214
|
-
|
|
215
|
-
<br/>
|
|
216
|
-
<br/>
|
|
217
|
-
|
|
218
|
-
## Installation
|
|
55
|
+
## What's new in 6.0
|
|
219
56
|
|
|
220
|
-
-
|
|
57
|
+
- ANSI-safe display-width calculation and Unicode-aware wrapping/truncation.
|
|
58
|
+
- Typed column/table options and formatter context.
|
|
59
|
+
- ESM and CommonJS package exports.
|
|
60
|
+
- Modern Node.js LTS baseline (Node 20+).
|
|
61
|
+
- A standalone browser bundle is produced for direct use from a browser console or `<script>` tag.
|
|
62
|
+
- Legacy `Table(header, rows, footer, options)` and `Table(rows, options)` construction remains supported.
|
|
221
63
|
|
|
222
|
-
|
|
223
|
-
$ npm install tty-table -g
|
|
224
|
-
```
|
|
64
|
+
## Compatibility
|
|
225
65
|
|
|
226
|
-
|
|
66
|
+
### Node.js
|
|
227
67
|
|
|
228
|
-
|
|
229
|
-
$ npm install tty-table
|
|
230
|
-
```
|
|
231
|
-
|
|
232
|
-
- Browser
|
|
68
|
+
**v6 requires Node.js 20 or newer.** This is a breaking change from the v5 line, which supported older Node.js releases. If your application must remain on an older Node version, stay on the v5 release line.
|
|
233
69
|
|
|
234
|
-
|
|
235
|
-
import Table from 'https://cdn.jsdelivr.net/gh/tecfu/tty-table/dist/tty-table.esm.js'
|
|
236
|
-
let Table = require('tty-table') // https://cdn.jsdelivr.net/gh/tecfu/tty-table/dist/tty-table.cjs.js
|
|
237
|
-
let Table = TTY_Table; // https://cdn.jsdelivr.net/gh/tecfu/tty-table/dist/tty-table.umd.js
|
|
238
|
-
```
|
|
70
|
+
The published package provides both ESM and CommonJS entry points for Node.js. The CLI requires Node.js 20+ as well.
|
|
239
71
|
|
|
240
|
-
##
|
|
72
|
+
## API
|
|
241
73
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
| 8 | >= 2.0 |
|
|
245
|
-
| 0.11 | >= 0.0 |
|
|
74
|
+
```ts
|
|
75
|
+
import Table from "tty-table"
|
|
246
76
|
|
|
247
|
-
|
|
77
|
+
const table = Table(
|
|
78
|
+
[{ value: "name" }, { value: "score", align: "right" }],
|
|
79
|
+
[
|
|
80
|
+
{ name: "Ada", score: 100 },
|
|
81
|
+
{ name: "Grace", score: 98 }
|
|
82
|
+
],
|
|
83
|
+
{ borderStyle: "solid" }
|
|
84
|
+
)
|
|
248
85
|
|
|
249
|
-
|
|
250
|
-
$ npm test
|
|
86
|
+
console.log(table.render())
|
|
251
87
|
```
|
|
252
88
|
|
|
253
|
-
|
|
254
|
-
$ npm run coverage
|
|
255
|
-
```
|
|
89
|
+
### Formatter context
|
|
256
90
|
|
|
257
|
-
|
|
91
|
+
New code can use the explicit context form:
|
|
258
92
|
|
|
259
|
-
```
|
|
260
|
-
|
|
93
|
+
```ts
|
|
94
|
+
const formatter = (value: unknown) => String(value).toUpperCase()
|
|
261
95
|
```
|
|
262
96
|
|
|
263
|
-
|
|
97
|
+
The compatibility callback signature is still accepted. New integrations should prefer a formatter that accepts the documented context object and avoid relying on dynamic `this` mutation.
|
|
264
98
|
|
|
265
|
-
|
|
99
|
+
### Width semantics
|
|
266
100
|
|
|
267
|
-
|
|
268
|
-
$ npm run tags
|
|
269
|
-
```
|
|
101
|
+
Widths are measured in terminal display columns, not JavaScript string length. ANSI escape sequences are ignored for measurement; wide Unicode characters are counted using `wcwidth`. Wrapping and truncation operate on the same measurement primitive.
|
|
270
102
|
|
|
271
|
-
|
|
103
|
+
## Development
|
|
272
104
|
|
|
273
105
|
```sh
|
|
274
|
-
|
|
106
|
+
npm install
|
|
107
|
+
npm run typecheck
|
|
108
|
+
npm run build
|
|
109
|
+
npm test
|
|
110
|
+
npm run test:unit
|
|
111
|
+
npm run lint
|
|
275
112
|
```
|
|
276
|
-
|
|
277
|
-
## Pull Requests
|
|
278
|
-
|
|
279
|
-
Pull requests are encouraged!
|
|
280
|
-
|
|
281
|
-
- Please remember to add a unit test when necessary
|
|
282
|
-
- Please format your commit messages according to the ["Conventional Commits"](https://www.conventionalcommits.org/en/v1.0.0/) specification
|
|
283
|
-
|
|
284
|
-
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)
|
|
285
|
-
|
|
286
|
-
TL/DR:
|
|
287
|
-
|
|
288
|
-
- feat: a feature that is visible for end users.
|
|
289
|
-
- fix: a bugfix that is visible for end users.
|
|
290
|
-
- chore: a change that doesn't impact end users (e.g. chances to CI pipeline)
|
|
291
|
-
- docs: a change in the README or documentation
|
|
292
|
-
- refactor: a change in production code focused on readability, style and/or performance.
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
## [Packaging as a distributable](packaging.md)
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
## License
|
|
299
|
-
|
|
300
|
-
[MIT License](https://opensource.org/licenses/MIT)
|
|
301
|
-
|
|
302
|
-
Copyright 2015-2020, Tecfu.
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const Factory = require("./../
|
|
1
|
+
const Factory = require("./../dist/index.js")
|
|
2
2
|
module.exports = Factory
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
const path = require("path")
|
|
3
3
|
const fs = require("fs")
|
|
4
4
|
const csv = require("csv")
|
|
5
|
-
const
|
|
5
|
+
const ttytable = require("../dist/index.js")
|
|
6
|
+
const style = ttytable.style
|
|
6
7
|
let yargs = require("yargs")
|
|
7
8
|
|
|
8
9
|
yargs.epilog("Copyright github.com/tecfu 2018")
|
|
@@ -85,7 +86,7 @@ if (yargs.header) {
|
|
|
85
86
|
// because different dataFormats
|
|
86
87
|
const runTable = function (header, body) {
|
|
87
88
|
// footer = [],
|
|
88
|
-
const Table =
|
|
89
|
+
const Table = ttytable
|
|
89
90
|
options.terminalAdapter = true
|
|
90
91
|
const t1 = Table(header, body, options)
|
|
91
92
|
|