tty-table 4.2.3 → 5.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 CHANGED
@@ -1,4 +1,4 @@
1
- # tty-table 端子台
1
+ # tty-table
2
2
 
3
3
  [![NPM version](https://badge.fury.io/js/tty-table.svg)](http://badge.fury.io/js/tty-table) [![Coverage Status](https://coveralls.io/repos/github/tecfu/tty-table/badge.svg?branch=master)](https://coveralls.io/github/tecfu/tty-table?branch=master)
4
4
  ---
@@ -178,7 +178,7 @@ const footer = [
178
178
  | color | <code>string</code> | default: terminal default color |
179
179
  | compact | <code>boolean</code> | default: false <br/> Removes horizontal borders when true. |
180
180
  | defaultErrorValue | <code>mixed</code> | default: '�' |
181
- | defaultValue | <code>mixed</code> | default: '?' |
181
+ | defaultValue | <code>mixed</code> | default: '""' |
182
182
  | errorOnNull | <code>boolean</code> | default: false |
183
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
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".|
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tty-table",
3
- "version": "4.2.3",
3
+ "version": "5.0.0",
4
4
  "description": "Node cli table",
5
5
  "main": "src/main.js",
6
6
  "types": "src/factory.d.ts",
@@ -25,7 +25,7 @@
25
25
  "save-tests": "node npm_scripts/run-examples.js save",
26
26
  "view-examples": "node npm_scripts/run-examples.js view",
27
27
  "lint": "npx eslint adapters/*.js src/*.js",
28
- "lint-fix": "npx eslint adapters/*.js src/*.js --fix",
28
+ "lint-fix": "npx eslint adapters/*.js src/*.js examples/*.js --fix",
29
29
  "lint-examples": "npx eslint examples/",
30
30
  "lint-fix-examples": "npx eslint examples/ --fix",
31
31
  "prepublishOnly": "npm run dist",
package/src/defaults.js CHANGED
@@ -29,8 +29,7 @@ const defaults = {
29
29
  COLUMNS: 80, // if !process.stdout.columns assume redirecting to write stream 80 columns is VT200 standard
30
30
  compact: false,
31
31
  defaultErrorValue: "�",
32
- // defaultValue: "\u001b[31m?\u001b[39m",
33
- defaultValue: " ?",
32
+ defaultValue: "",
34
33
  errorOnNull: false,
35
34
  FIXED_WIDTH: false,
36
35
  footerAlign: "center",
package/src/render.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const Style = require("./style.js")
2
2
  const Format = require("./format.js")
3
+ const stripAnsi = require("strip-ansi")
3
4
 
4
5
  /**
5
6
  * Converts arrays of data into arrays of cell strings
@@ -31,7 +32,12 @@ module.exports.stringifyData = (config, inputData) => {
31
32
  if (global.columnWidths[config.tableId]) {
32
33
  config.table.columnWidths = global.columnWidths[config.tableId]
33
34
  } else {
34
- global.columnWidths[config.tableId] = config.table.columnWidths = Format.getColumnWidths(config, rows)
35
+ const formattedRows = rows.map((row, rowIndex) => {
36
+ return row.map((cell, cellIndex) => {
37
+ return exports.buildCell(config, cell, cellIndex, "body", rowIndex, rows, inputData, true)
38
+ })
39
+ })
40
+ global.columnWidths[config.tableId] = config.table.columnWidths = Format.getColumnWidths(config, formattedRows)
35
41
  }
36
42
 
37
43
  // stringify header cells
@@ -207,13 +213,13 @@ module.exports.buildRow = (config, row, rowType, rowIndex, rowData, inputData) =
207
213
  return linedRow
208
214
  }
209
215
 
210
- module.exports.buildCell = (config, elem, columnIndex, rowType, rowIndex, rowData, inputData) => {
216
+ module.exports.buildCell = (config, elem, columnIndex, rowType, rowIndex, rowData, inputData, dryRun = false) => {
211
217
  let cellValue = null
212
218
 
213
219
  const cellOptions = Object.assign(
214
220
  { reset: false },
215
221
  config,
216
- (rowType === "body") ? config.columnSettings[columnIndex] : {}, // ignore columnSettings for footer
222
+ (rowType !== "header") ? config.columnSettings[columnIndex] : {},
217
223
  (typeof elem === "object") ? elem : {}
218
224
  )
219
225
 
@@ -226,6 +232,9 @@ module.exports.buildCell = (config, elem, columnIndex, rowType, rowIndex, rowDat
226
232
  case (typeof elem === "undefined" || elem === null):
227
233
  // replace undefined/null elem values with placeholder
228
234
  cellValue = (config.errorOnNull) ? config.defaultErrorValue : config.defaultValue
235
+ if (!Style.isColorEnabled()) {
236
+ cellValue = stripAnsi(cellValue)
237
+ }
229
238
  // @TODO add to elem defaults
230
239
  cellOptions.isNull = true
231
240
  break
@@ -242,7 +251,7 @@ module.exports.buildCell = (config, elem, columnIndex, rowType, rowIndex, rowDat
242
251
  style: Style.style,
243
252
  resetStyle: Style.resetStyle
244
253
  })(
245
- (!cellOptions.isNull) ? cellValue : "",
254
+ cellValue,
246
255
  columnIndex,
247
256
  rowIndex,
248
257
  rowData,
@@ -256,7 +265,7 @@ module.exports.buildCell = (config, elem, columnIndex, rowType, rowIndex, rowDat
256
265
  }
257
266
 
258
267
  // run formatter
259
- if (typeof cellOptions.formatter === "function") {
268
+ if (rowType === "body" && typeof cellOptions.formatter === "function") {
260
269
  cellValue = cellOptions.formatter
261
270
  .bind({
262
271
  configure: function (object) {
@@ -265,13 +274,17 @@ module.exports.buildCell = (config, elem, columnIndex, rowType, rowIndex, rowDat
265
274
  style: Style.style,
266
275
  resetStyle: Style.resetStyle
267
276
  })(
268
- (!cellOptions.isNull) ? cellValue : "",
277
+ cellValue,
269
278
  columnIndex,
270
279
  rowIndex,
271
280
  rowData,
272
281
  inputData
273
282
  )
274
283
  }
284
+
285
+ if (dryRun) {
286
+ return cellValue
287
+ }
275
288
  }
276
289
 
277
290
  // colorize cellValue
package/src/style.js CHANGED
@@ -56,3 +56,7 @@ module.exports.colorizeCell = (str, cellOptions, rowType) => {
56
56
 
57
57
  return str
58
58
  }
59
+
60
+ module.exports.isColorEnabled = () => {
61
+ return (process && process.stdout) ? colorLib.level > 0 : colorLib.enabled
62
+ }