tty-table 5.0.0 → 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/dist/index.js ADDED
@@ -0,0 +1,1020 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __commonJS = (cb, mod) => function __require() {
9
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
+ };
11
+ var __export = (target, all) => {
12
+ for (var name in all)
13
+ __defProp(target, name, { get: all[name], enumerable: true });
14
+ };
15
+ var __copyProps = (to, from, except, desc) => {
16
+ if (from && typeof from === "object" || typeof from === "function") {
17
+ for (let key of __getOwnPropNames(from))
18
+ if (!__hasOwnProp.call(to, key) && key !== except)
19
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
20
+ }
21
+ return to;
22
+ };
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
24
+ // If the importer is in node compatibility mode or this is not an ESM
25
+ // file that has been converted to a CommonJS file using a Babel-
26
+ // compatible transform (i.e. "__esModule" has not been set), then set
27
+ // "default" to the CommonJS "module.exports" for node compatibility.
28
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
29
+ mod
30
+ ));
31
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
+
33
+ // node_modules/clone/clone.js
34
+ var require_clone = __commonJS({
35
+ "node_modules/clone/clone.js"(exports2, module2) {
36
+ "use strict";
37
+ var clone = (function() {
38
+ "use strict";
39
+ function clone2(parent, circular, depth, prototype) {
40
+ var filter;
41
+ if (typeof circular === "object") {
42
+ depth = circular.depth;
43
+ prototype = circular.prototype;
44
+ filter = circular.filter;
45
+ circular = circular.circular;
46
+ }
47
+ var allParents = [];
48
+ var allChildren = [];
49
+ var useBuffer = typeof Buffer != "undefined";
50
+ if (typeof circular == "undefined")
51
+ circular = true;
52
+ if (typeof depth == "undefined")
53
+ depth = Infinity;
54
+ function _clone(parent2, depth2) {
55
+ if (parent2 === null)
56
+ return null;
57
+ if (depth2 == 0)
58
+ return parent2;
59
+ var child;
60
+ var proto;
61
+ if (typeof parent2 != "object") {
62
+ return parent2;
63
+ }
64
+ if (clone2.__isArray(parent2)) {
65
+ child = [];
66
+ } else if (clone2.__isRegExp(parent2)) {
67
+ child = new RegExp(parent2.source, __getRegExpFlags(parent2));
68
+ if (parent2.lastIndex) child.lastIndex = parent2.lastIndex;
69
+ } else if (clone2.__isDate(parent2)) {
70
+ child = new Date(parent2.getTime());
71
+ } else if (useBuffer && Buffer.isBuffer(parent2)) {
72
+ if (Buffer.allocUnsafe) {
73
+ child = Buffer.allocUnsafe(parent2.length);
74
+ } else {
75
+ child = new Buffer(parent2.length);
76
+ }
77
+ parent2.copy(child);
78
+ return child;
79
+ } else {
80
+ if (typeof prototype == "undefined") {
81
+ proto = Object.getPrototypeOf(parent2);
82
+ child = Object.create(proto);
83
+ } else {
84
+ child = Object.create(prototype);
85
+ proto = prototype;
86
+ }
87
+ }
88
+ if (circular) {
89
+ var index = allParents.indexOf(parent2);
90
+ if (index != -1) {
91
+ return allChildren[index];
92
+ }
93
+ allParents.push(parent2);
94
+ allChildren.push(child);
95
+ }
96
+ for (var i in parent2) {
97
+ var attrs;
98
+ if (proto) {
99
+ attrs = Object.getOwnPropertyDescriptor(proto, i);
100
+ }
101
+ if (attrs && attrs.set == null) {
102
+ continue;
103
+ }
104
+ child[i] = _clone(parent2[i], depth2 - 1);
105
+ }
106
+ return child;
107
+ }
108
+ return _clone(parent, depth);
109
+ }
110
+ clone2.clonePrototype = function clonePrototype(parent) {
111
+ if (parent === null)
112
+ return null;
113
+ var c = function() {
114
+ };
115
+ c.prototype = parent;
116
+ return new c();
117
+ };
118
+ function __objToStr(o) {
119
+ return Object.prototype.toString.call(o);
120
+ }
121
+ ;
122
+ clone2.__objToStr = __objToStr;
123
+ function __isDate(o) {
124
+ return typeof o === "object" && __objToStr(o) === "[object Date]";
125
+ }
126
+ ;
127
+ clone2.__isDate = __isDate;
128
+ function __isArray(o) {
129
+ return typeof o === "object" && __objToStr(o) === "[object Array]";
130
+ }
131
+ ;
132
+ clone2.__isArray = __isArray;
133
+ function __isRegExp(o) {
134
+ return typeof o === "object" && __objToStr(o) === "[object RegExp]";
135
+ }
136
+ ;
137
+ clone2.__isRegExp = __isRegExp;
138
+ function __getRegExpFlags(re) {
139
+ var flags = "";
140
+ if (re.global) flags += "g";
141
+ if (re.ignoreCase) flags += "i";
142
+ if (re.multiline) flags += "m";
143
+ return flags;
144
+ }
145
+ ;
146
+ clone2.__getRegExpFlags = __getRegExpFlags;
147
+ return clone2;
148
+ })();
149
+ if (typeof module2 === "object" && module2.exports) {
150
+ module2.exports = clone;
151
+ }
152
+ }
153
+ });
154
+
155
+ // node_modules/defaults/index.js
156
+ var require_defaults = __commonJS({
157
+ "node_modules/defaults/index.js"(exports2, module2) {
158
+ "use strict";
159
+ var clone = require_clone();
160
+ module2.exports = function(options, defaults2) {
161
+ options = options || {};
162
+ Object.keys(defaults2).forEach(function(key) {
163
+ if (typeof options[key] === "undefined") {
164
+ options[key] = clone(defaults2[key]);
165
+ }
166
+ });
167
+ return options;
168
+ };
169
+ }
170
+ });
171
+
172
+ // node_modules/wcwidth/combining.js
173
+ var require_combining = __commonJS({
174
+ "node_modules/wcwidth/combining.js"(exports2, module2) {
175
+ "use strict";
176
+ module2.exports = [
177
+ [768, 879],
178
+ [1155, 1158],
179
+ [1160, 1161],
180
+ [1425, 1469],
181
+ [1471, 1471],
182
+ [1473, 1474],
183
+ [1476, 1477],
184
+ [1479, 1479],
185
+ [1536, 1539],
186
+ [1552, 1557],
187
+ [1611, 1630],
188
+ [1648, 1648],
189
+ [1750, 1764],
190
+ [1767, 1768],
191
+ [1770, 1773],
192
+ [1807, 1807],
193
+ [1809, 1809],
194
+ [1840, 1866],
195
+ [1958, 1968],
196
+ [2027, 2035],
197
+ [2305, 2306],
198
+ [2364, 2364],
199
+ [2369, 2376],
200
+ [2381, 2381],
201
+ [2385, 2388],
202
+ [2402, 2403],
203
+ [2433, 2433],
204
+ [2492, 2492],
205
+ [2497, 2500],
206
+ [2509, 2509],
207
+ [2530, 2531],
208
+ [2561, 2562],
209
+ [2620, 2620],
210
+ [2625, 2626],
211
+ [2631, 2632],
212
+ [2635, 2637],
213
+ [2672, 2673],
214
+ [2689, 2690],
215
+ [2748, 2748],
216
+ [2753, 2757],
217
+ [2759, 2760],
218
+ [2765, 2765],
219
+ [2786, 2787],
220
+ [2817, 2817],
221
+ [2876, 2876],
222
+ [2879, 2879],
223
+ [2881, 2883],
224
+ [2893, 2893],
225
+ [2902, 2902],
226
+ [2946, 2946],
227
+ [3008, 3008],
228
+ [3021, 3021],
229
+ [3134, 3136],
230
+ [3142, 3144],
231
+ [3146, 3149],
232
+ [3157, 3158],
233
+ [3260, 3260],
234
+ [3263, 3263],
235
+ [3270, 3270],
236
+ [3276, 3277],
237
+ [3298, 3299],
238
+ [3393, 3395],
239
+ [3405, 3405],
240
+ [3530, 3530],
241
+ [3538, 3540],
242
+ [3542, 3542],
243
+ [3633, 3633],
244
+ [3636, 3642],
245
+ [3655, 3662],
246
+ [3761, 3761],
247
+ [3764, 3769],
248
+ [3771, 3772],
249
+ [3784, 3789],
250
+ [3864, 3865],
251
+ [3893, 3893],
252
+ [3895, 3895],
253
+ [3897, 3897],
254
+ [3953, 3966],
255
+ [3968, 3972],
256
+ [3974, 3975],
257
+ [3984, 3991],
258
+ [3993, 4028],
259
+ [4038, 4038],
260
+ [4141, 4144],
261
+ [4146, 4146],
262
+ [4150, 4151],
263
+ [4153, 4153],
264
+ [4184, 4185],
265
+ [4448, 4607],
266
+ [4959, 4959],
267
+ [5906, 5908],
268
+ [5938, 5940],
269
+ [5970, 5971],
270
+ [6002, 6003],
271
+ [6068, 6069],
272
+ [6071, 6077],
273
+ [6086, 6086],
274
+ [6089, 6099],
275
+ [6109, 6109],
276
+ [6155, 6157],
277
+ [6313, 6313],
278
+ [6432, 6434],
279
+ [6439, 6440],
280
+ [6450, 6450],
281
+ [6457, 6459],
282
+ [6679, 6680],
283
+ [6912, 6915],
284
+ [6964, 6964],
285
+ [6966, 6970],
286
+ [6972, 6972],
287
+ [6978, 6978],
288
+ [7019, 7027],
289
+ [7616, 7626],
290
+ [7678, 7679],
291
+ [8203, 8207],
292
+ [8234, 8238],
293
+ [8288, 8291],
294
+ [8298, 8303],
295
+ [8400, 8431],
296
+ [12330, 12335],
297
+ [12441, 12442],
298
+ [43014, 43014],
299
+ [43019, 43019],
300
+ [43045, 43046],
301
+ [64286, 64286],
302
+ [65024, 65039],
303
+ [65056, 65059],
304
+ [65279, 65279],
305
+ [65529, 65531],
306
+ [68097, 68099],
307
+ [68101, 68102],
308
+ [68108, 68111],
309
+ [68152, 68154],
310
+ [68159, 68159],
311
+ [119143, 119145],
312
+ [119155, 119170],
313
+ [119173, 119179],
314
+ [119210, 119213],
315
+ [119362, 119364],
316
+ [917505, 917505],
317
+ [917536, 917631],
318
+ [917760, 917999]
319
+ ];
320
+ }
321
+ });
322
+
323
+ // node_modules/wcwidth/index.js
324
+ var require_wcwidth = __commonJS({
325
+ "node_modules/wcwidth/index.js"(exports2, module2) {
326
+ "use strict";
327
+ var defaults2 = require_defaults();
328
+ var combining = require_combining();
329
+ var DEFAULTS = {
330
+ nul: 0,
331
+ control: 0
332
+ };
333
+ module2.exports = function wcwidth3(str) {
334
+ return wcswidth(str, DEFAULTS);
335
+ };
336
+ module2.exports.config = function(opts) {
337
+ opts = defaults2(opts || {}, DEFAULTS);
338
+ return function wcwidth3(str) {
339
+ return wcswidth(str, opts);
340
+ };
341
+ };
342
+ function wcswidth(str, opts) {
343
+ if (typeof str !== "string") return wcwidth2(str, opts);
344
+ var s = 0;
345
+ for (var i = 0; i < str.length; i++) {
346
+ var n = wcwidth2(str.charCodeAt(i), opts);
347
+ if (n < 0) return -1;
348
+ s += n;
349
+ }
350
+ return s;
351
+ }
352
+ function wcwidth2(ucs, opts) {
353
+ if (ucs === 0) return opts.nul;
354
+ if (ucs < 32 || ucs >= 127 && ucs < 160) return opts.control;
355
+ if (bisearch(ucs)) return 0;
356
+ return 1 + (ucs >= 4352 && (ucs <= 4447 || // Hangul Jamo init. consonants
357
+ ucs == 9001 || ucs == 9002 || ucs >= 11904 && ucs <= 42191 && ucs != 12351 || // CJK ... Yi
358
+ ucs >= 44032 && ucs <= 55203 || // Hangul Syllables
359
+ ucs >= 63744 && ucs <= 64255 || // CJK Compatibility Ideographs
360
+ ucs >= 65040 && ucs <= 65049 || // Vertical forms
361
+ ucs >= 65072 && ucs <= 65135 || // CJK Compatibility Forms
362
+ ucs >= 65280 && ucs <= 65376 || // Fullwidth Forms
363
+ ucs >= 65504 && ucs <= 65510 || ucs >= 131072 && ucs <= 196605 || ucs >= 196608 && ucs <= 262141));
364
+ }
365
+ function bisearch(ucs) {
366
+ var min = 0;
367
+ var max = combining.length - 1;
368
+ var mid;
369
+ if (ucs < combining[0][0] || ucs > combining[max][1]) return false;
370
+ while (max >= min) {
371
+ mid = Math.floor((min + max) / 2);
372
+ if (ucs > combining[mid][1]) min = mid + 1;
373
+ else if (ucs < combining[mid][0]) max = mid - 1;
374
+ else return true;
375
+ }
376
+ return false;
377
+ }
378
+ }
379
+ });
380
+
381
+ // src/index.ts
382
+ var src_exports = {};
383
+ __export(src_exports, {
384
+ default: () => src_default
385
+ });
386
+ module.exports = __toCommonJS(src_exports);
387
+
388
+ // src/defaults.ts
389
+ var defaults = {
390
+ borderCharacters: {
391
+ invisible: [
392
+ { v: " ", l: " ", j: " ", h: " ", r: " " },
393
+ { v: " ", l: " ", j: " ", h: " ", r: " " },
394
+ { v: " ", l: " ", j: " ", h: " ", r: " " }
395
+ ],
396
+ solid: [
397
+ { v: "\u2502", l: "\u250C", j: "\u252C", h: "\u2500", r: "\u2510" },
398
+ { v: "\u2502", l: "\u251C", j: "\u253C", h: "\u2500", r: "\u2524" },
399
+ { v: "\u2502", l: "\u2514", j: "\u2534", h: "\u2500", r: "\u2518" }
400
+ ],
401
+ dashed: [
402
+ { v: "|", l: "+", j: "+", h: "-", r: "+" },
403
+ { v: "|", l: "+", j: "+", h: "-", r: "+" },
404
+ { v: "|", l: "+", j: "+", h: "-", r: "+" }
405
+ ],
406
+ none: [
407
+ { v: "", l: "", j: "", h: "", r: "" },
408
+ { v: "", l: "", j: "", h: "", r: "" },
409
+ { v: "", l: "", j: "", h: "", r: "" }
410
+ ]
411
+ },
412
+ align: "center",
413
+ borderColor: null,
414
+ borderStyle: "solid",
415
+ color: false,
416
+ COLUMNS: 80,
417
+ // if !process.stdout.columns assume redirecting to write stream 80 columns is VT200 standard
418
+ compact: false,
419
+ defaultErrorValue: "\uFFFD",
420
+ defaultValue: "",
421
+ errorOnNull: false,
422
+ FIXED_WIDTH: false,
423
+ footerAlign: "center",
424
+ footerColor: false,
425
+ formatter: null,
426
+ headerAlign: "center",
427
+ headerColor: "yellow",
428
+ isNull: false,
429
+ // undocumented cell setting
430
+ marginLeft: 2,
431
+ marginTop: 1,
432
+ paddingBottom: 0,
433
+ paddingLeft: 1,
434
+ paddingRight: 1,
435
+ paddingTop: 0,
436
+ showHeader: null,
437
+ // undocumented
438
+ truncate: false,
439
+ width: "100%",
440
+ GUTTER: 1,
441
+ // undocumented
442
+ columnSettings: [],
443
+ // save so cell options can be merged into column options
444
+ table: {
445
+ body: "",
446
+ columnInnerWidths: [],
447
+ columnWidths: [],
448
+ columns: [],
449
+ footer: "",
450
+ header: "",
451
+ // post-rendered strings.
452
+ height: 0,
453
+ typeLocked: false
454
+ // once a table type is selected can't switch
455
+ }
456
+ };
457
+ defaults.borderCharacters["0"] = defaults.borderCharacters.none;
458
+ defaults.borderCharacters["1"] = defaults.borderCharacters.solid;
459
+ defaults.borderCharacters["2"] = defaults.borderCharacters.dashed;
460
+ var defaults_default = defaults;
461
+
462
+ // src/style.ts
463
+ var import_chalk = __toESM(require("chalk"));
464
+ var import_kleur = __toESM(require("kleur"));
465
+ var import_strip_ansi = __toESM(require("strip-ansi"));
466
+ var colorLib = process && process.stdout ? import_chalk.default : import_kleur.default;
467
+ var style = (str, ...colors) => {
468
+ const out = colors.reduce(function(input, color) {
469
+ return colorLib[color](input);
470
+ }, str);
471
+ return out;
472
+ };
473
+ var styleEachChar = (str, ...colors) => {
474
+ const chars = [...(0, import_strip_ansi.default)(str)];
475
+ const out = chars.reduce((prev, current) => {
476
+ const coded = colors.reduce((input, color) => {
477
+ return colorLib[color](input);
478
+ }, current);
479
+ return prev + coded;
480
+ }, "");
481
+ return out;
482
+ };
483
+ var resetStyle = function(str) {
484
+ this.configure({ reset: true });
485
+ return (0, import_strip_ansi.default)(str);
486
+ };
487
+ var colorizeCell = (str, cellOptions, rowType) => {
488
+ let color = false;
489
+ switch (true) {
490
+ case rowType === "body":
491
+ color = cellOptions.color || color;
492
+ break;
493
+ case rowType === "header":
494
+ color = cellOptions.headerColor || color;
495
+ break;
496
+ default:
497
+ color = cellOptions.footerColor || color;
498
+ }
499
+ if (color) {
500
+ str = style(str, color);
501
+ }
502
+ return str;
503
+ };
504
+ var isColorEnabled = () => {
505
+ return process && process.stdout ? colorLib.level > 0 : colorLib.enabled;
506
+ };
507
+
508
+ // src/format.ts
509
+ var import_strip_ansi2 = __toESM(require("strip-ansi"));
510
+ var import_smartwrap = __toESM(require("smartwrap"));
511
+ var import_wcwidth = __toESM(require_wcwidth());
512
+ var addPadding = (config, width) => {
513
+ return width + config.paddingLeft + config.paddingRight;
514
+ };
515
+ var getMaxLength = (columnOptions, rows, columnIndex) => {
516
+ let iterable;
517
+ if (columnOptions && (columnOptions.value || columnOptions.alias)) {
518
+ let val = columnOptions.alias || columnOptions.value;
519
+ val = val.toString();
520
+ const headerRow = Array(rows[0].length);
521
+ headerRow[columnIndex] = val;
522
+ iterable = rows.slice();
523
+ iterable.push(headerRow);
524
+ } else {
525
+ iterable = rows;
526
+ }
527
+ const widest = iterable.reduce((prev, row) => {
528
+ if (row[columnIndex]) {
529
+ const value = row[columnIndex].value ? row[columnIndex].value : row[columnIndex];
530
+ const width = Math.max(
531
+ ...(0, import_strip_ansi2.default)(value.toString()).split(/[\n\r]/).map((s) => (0, import_wcwidth.default)(s))
532
+ );
533
+ return width > prev ? width : prev;
534
+ }
535
+ return prev;
536
+ }, 0);
537
+ return widest;
538
+ };
539
+ var getAvailableWidth = (config) => {
540
+ if (process && (process.stdout && process.stdout.columns || process.env && process.env.COLUMNS)) {
541
+ let viewport = process.stdout && process.stdout.columns ? process.stdout.columns : process.env.COLUMNS;
542
+ viewport = viewport - config.marginLeft;
543
+ if (config.width !== "auto" && /^\d+%$/.test(config.width)) {
544
+ return Math.min(1, config.width.slice(0, -1) * 0.01) * viewport;
545
+ }
546
+ if (config.width !== "auto" && /^\d+$/.test(config.width)) {
547
+ config.FIXED_WIDTH = true;
548
+ return config.width;
549
+ }
550
+ return viewport;
551
+ }
552
+ if (typeof globalThis.window !== "undefined") return globalThis.window.innerWidth;
553
+ return config.COLUMNS - config.marginLeft;
554
+ };
555
+ var getStringLength = (str) => {
556
+ return (0, import_wcwidth.default)((0, import_strip_ansi2.default)(str));
557
+ };
558
+ var wrapCellText = (config, cellValue, columnIndex, cellOptions, rowType) => {
559
+ const startAnsiRegexp = /^(\x1b\[[0-9;]*m)+/;
560
+ const endAnsiRegexp = /(\x1b\[[0-9;]*m)+$/;
561
+ let str = cellValue.toString();
562
+ const startMatches = str.match(startAnsiRegexp) || [""];
563
+ str = str.replace(startAnsiRegexp, "");
564
+ const endMatches = str.match(endAnsiRegexp) || [""];
565
+ str = str.replace(endAnsiRegexp, "");
566
+ let alignTgt;
567
+ switch (rowType) {
568
+ case "header":
569
+ alignTgt = "headerAlign";
570
+ break;
571
+ case "body":
572
+ alignTgt = "align";
573
+ break;
574
+ default:
575
+ alignTgt = "footerAlign";
576
+ }
577
+ if (cellOptions[alignTgt] === "center") {
578
+ cellOptions.paddingLeft = cellOptions.paddingRight = Math.max(
579
+ cellOptions.paddingRight,
580
+ cellOptions.paddingLeft,
581
+ 0
582
+ );
583
+ }
584
+ const columnWidth = config.table.columnWidths[columnIndex];
585
+ const innerWidth = columnWidth - cellOptions.paddingLeft - cellOptions.paddingRight - config.GUTTER;
586
+ if (typeof config.truncate === "string") {
587
+ str = truncate(str, cellOptions, innerWidth);
588
+ } else {
589
+ str = wrap(str, cellOptions, innerWidth);
590
+ }
591
+ const cell = str.split("\n").map((line) => {
592
+ line = line.trim();
593
+ const lineLength = getStringLength(line);
594
+ if (lineLength < columnWidth) {
595
+ let emptySpace = columnWidth - lineLength;
596
+ switch (true) {
597
+ case cellOptions[alignTgt] === "center":
598
+ emptySpace--;
599
+ const padBoth = Math.floor(emptySpace / 2);
600
+ const padRemainder = emptySpace % 2;
601
+ line = Array(padBoth + 1).join(" ") + line + Array(padBoth + 1 + padRemainder).join(" ");
602
+ break;
603
+ case cellOptions[alignTgt] === "right":
604
+ line = Array(emptySpace - cellOptions.paddingRight).join(" ") + line + Array(cellOptions.paddingRight + 1).join(" ");
605
+ break;
606
+ default:
607
+ line = Array(cellOptions.paddingLeft + 1).join(" ") + line + Array(emptySpace - cellOptions.paddingLeft).join(" ");
608
+ }
609
+ }
610
+ return startMatches[0] + line + endMatches[0];
611
+ });
612
+ return { cell, innerWidth };
613
+ };
614
+ var truncate = (str, cellOptions, maxWidth) => {
615
+ const stringWidth = (0, import_wcwidth.default)(str);
616
+ if (maxWidth < stringWidth) {
617
+ str = (0, import_smartwrap.default)(str, {
618
+ width: maxWidth - cellOptions.truncate.length,
619
+ breakword: true
620
+ }).split("\n")[0];
621
+ str = str + cellOptions.truncate;
622
+ }
623
+ return str;
624
+ };
625
+ var wrap = (str, cellOptions, innerWidth) => {
626
+ const outstring = (0, import_smartwrap.default)(str, {
627
+ errorChar: cellOptions.defaultErrorValue,
628
+ minWidth: 1,
629
+ trim: true,
630
+ width: innerWidth
631
+ });
632
+ return outstring;
633
+ };
634
+ var getColumnWidths = (config, rows) => {
635
+ const availableWidth = getAvailableWidth(config);
636
+ const iterable = config.table.header[0] && config.table.header[0].length > 0 ? config.table.header[0] : rows[0];
637
+ let widths = iterable.map((column, columnIndex) => {
638
+ let result;
639
+ switch (true) {
640
+ // column width is a percentage of table width specified in column header
641
+ case (typeof column === "object" && /^\d+%$/.test(column.width)):
642
+ result = column.width.slice(0, -1) * 0.01 * availableWidth;
643
+ result = addPadding(config, result);
644
+ break;
645
+ // column width is specified in column header
646
+ case (typeof column === "object" && /^\d+$/.test(column.width)):
647
+ result = column.width;
648
+ break;
649
+ // 'auto' sets column width to its longest value in the initial data set
650
+ default: {
651
+ const columnOptions = config.table.header[0][columnIndex] ? config.table.header[0][columnIndex] : {};
652
+ const measurableRows = rows.length ? rows : config.table.header[0];
653
+ result = getMaxLength(columnOptions, measurableRows, columnIndex);
654
+ result = addPadding(config, result);
655
+ }
656
+ }
657
+ result = result + config.GUTTER;
658
+ return result;
659
+ });
660
+ const totalWidth = widths.reduce((prev, current) => prev + current);
661
+ if (totalWidth > availableWidth || config.FIXED_WIDTH) {
662
+ const proportion = (availableWidth / totalWidth).toFixed(2) - 0.01;
663
+ const relativeWidths = widths.map((value) => Math.max(2, Math.floor(proportion * value)));
664
+ if (config.FIXED_WIDTH) return relativeWidths;
665
+ if (proportion > 0) {
666
+ const totalRelativeWidths = relativeWidths.reduce((prev, current) => prev + current);
667
+ widths = totalRelativeWidths < totalWidth ? relativeWidths : widths;
668
+ }
669
+ } else {
670
+ widths = widths.map(Math.floor);
671
+ }
672
+ return widths;
673
+ };
674
+
675
+ // src/render.ts
676
+ var import_strip_ansi3 = __toESM(require("strip-ansi"));
677
+ var stringifyData = (config, inputData) => {
678
+ const sections = {
679
+ header: [],
680
+ body: [],
681
+ footer: []
682
+ };
683
+ const marginLeft = Array(config.marginLeft + 1).join(" ");
684
+ const borderStyle = config.borderCharacters[config.borderStyle];
685
+ const borders = [];
686
+ const constructorType = getConstructorGeometry(inputData[0] || [], config);
687
+ const rows = coerceConstructorGeometry(config, inputData, constructorType);
688
+ if (!global.columnWidths) {
689
+ global.columnWidths = {};
690
+ }
691
+ if (global.columnWidths[config.tableId]) {
692
+ config.table.columnWidths = global.columnWidths[config.tableId];
693
+ } else {
694
+ const formattedRows = rows.map((row, rowIndex) => {
695
+ return row.map((cell, cellIndex) => {
696
+ return buildCell(config, cell, cellIndex, "body", rowIndex, rows, inputData, true);
697
+ });
698
+ });
699
+ global.columnWidths[config.tableId] = config.table.columnWidths = getColumnWidths(config, formattedRows);
700
+ }
701
+ switch (true) {
702
+ case (config.showHeader !== null && !config.showHeader):
703
+ sections.header = [];
704
+ break;
705
+ case config.showHeader === true:
706
+ // explicitly true, show
707
+ case !!config.table.header[0].find((obj) => obj.value || obj.alias):
708
+ sections.header = config.table.header.map((row) => {
709
+ return buildRow(config, row, "header", null, rows, inputData);
710
+ });
711
+ break;
712
+ default:
713
+ sections.header = [];
714
+ }
715
+ sections.body = rows.map((row, rowIndex) => {
716
+ return buildRow(config, row, "body", rowIndex, rows, inputData);
717
+ });
718
+ sections.footer = config.table.footer instanceof Array && config.table.footer.length > 0 ? [config.table.footer] : [];
719
+ sections.footer = sections.footer.map((row) => {
720
+ return buildRow(config, row, "footer", null, rows, inputData);
721
+ });
722
+ for (let a = 0; a < 3; a++) {
723
+ borders[a] = borderStyle[a].l;
724
+ config.table.columnWidths.forEach((columnWidth, index, arr) => {
725
+ borders[a] += Array(Math.max(columnWidth, 2)).join(borderStyle[a].h);
726
+ borders[a] += index + 1 < arr.length ? borderStyle[a].j : "";
727
+ });
728
+ borders[a] += borderStyle[a].r;
729
+ borders[a] = a < 2 ? `${marginLeft + borders[a]}
730
+ ` : marginLeft + borders[a];
731
+ }
732
+ let output = borders[0];
733
+ Object.keys(sections).forEach((p, i) => {
734
+ while (sections[p].length) {
735
+ const row = sections[p].shift();
736
+ row.forEach((line) => {
737
+ output = `${output + marginLeft + borderStyle[1].v + line.join(borderStyle[1].v) + borderStyle[1].v}
738
+ `;
739
+ });
740
+ switch (true) {
741
+ // skip if end of body and no footer
742
+ case (sections[p].length === 0 && i === 1 && sections.footer.length === 0):
743
+ break;
744
+ // skip if end of footer
745
+ case (sections[p].length === 0 && i === 2):
746
+ break;
747
+ // skip if compact
748
+ case (config.compact && p === "body" && !row.empty):
749
+ break;
750
+ // skip if border style is "none"
751
+ case (config.borderStyle === "none" && config.compact):
752
+ break;
753
+ default:
754
+ output += borders[1];
755
+ }
756
+ }
757
+ });
758
+ output += borders[2];
759
+ const finalOutput = Array(config.marginTop + 1).join("\n") + output;
760
+ config.height = finalOutput.split(/\r\n|\r|\n/).length;
761
+ return finalOutput;
762
+ };
763
+ var buildRow = (config, row, rowType, rowIndex, rowData, inputData) => {
764
+ let minRowHeight = 0;
765
+ if (row.length === 0 && config.compact) {
766
+ row.empty = true;
767
+ return row;
768
+ }
769
+ const lengthDifference = config.table.columnWidths.length - row.length;
770
+ if (lengthDifference > 0) {
771
+ row = row.concat(Array.apply(null, new Array(lengthDifference)).map(() => null));
772
+ } else if (lengthDifference < 0) {
773
+ row.length = config.table.columnWidths.length;
774
+ }
775
+ row = row.map((elem, elemIndex) => {
776
+ const cell = buildCell(config, elem, elemIndex, rowType, rowIndex, rowData, inputData);
777
+ minRowHeight = minRowHeight < cell.length ? cell.length : minRowHeight;
778
+ return cell;
779
+ });
780
+ minRowHeight = rowType === "header" ? minRowHeight : minRowHeight + (config.paddingBottom + config.paddingTop);
781
+ const linedRow = Array.apply(null, { length: minRowHeight }).map(Function.call, () => []);
782
+ row.forEach(function(cell, a) {
783
+ const whitespace = Array(config.table.columnWidths[a]).join(" ");
784
+ if (rowType === "body") {
785
+ for (let i = 0; i < config.paddingTop; i++) {
786
+ cell.unshift(whitespace);
787
+ }
788
+ for (let i = 0; i < config.paddingBottom; i++) {
789
+ cell.push(whitespace);
790
+ }
791
+ }
792
+ for (let i = 0; i < minRowHeight; i++) {
793
+ linedRow[i].push(typeof cell[i] !== "undefined" ? cell[i] : whitespace);
794
+ }
795
+ });
796
+ return linedRow;
797
+ };
798
+ var buildCell = (config, elem, columnIndex, rowType, rowIndex, rowData, inputData, dryRun = false) => {
799
+ let cellValue = null;
800
+ const cellOptions = Object.assign(
801
+ { reset: false },
802
+ config,
803
+ rowType !== "header" ? config.columnSettings[columnIndex] : {},
804
+ typeof elem === "object" ? elem : {}
805
+ );
806
+ if (rowType === "header") {
807
+ config.table.columns.push(cellOptions);
808
+ cellValue = cellOptions.alias || cellOptions.value || "";
809
+ } else {
810
+ switch (true) {
811
+ case (typeof elem === "undefined" || elem === null):
812
+ cellValue = config.errorOnNull ? config.defaultErrorValue : config.defaultValue;
813
+ if (!isColorEnabled()) {
814
+ cellValue = (0, import_strip_ansi3.default)(cellValue);
815
+ }
816
+ cellOptions.isNull = true;
817
+ break;
818
+ case (typeof elem === "object" && elem !== null && typeof elem.value !== "undefined"):
819
+ cellValue = elem.value;
820
+ break;
821
+ case typeof elem === "function":
822
+ cellValue = elem.bind({
823
+ configure: function(object) {
824
+ return Object.assign(cellOptions, object);
825
+ },
826
+ style,
827
+ resetStyle
828
+ })(
829
+ cellValue,
830
+ columnIndex,
831
+ rowIndex,
832
+ rowData,
833
+ inputData
834
+ );
835
+ break;
836
+ default:
837
+ cellValue = elem;
838
+ }
839
+ if (rowType === "body" && typeof cellOptions.formatter === "function") {
840
+ cellValue = cellOptions.formatter.bind({
841
+ configure: function(object) {
842
+ return Object.assign(cellOptions, object);
843
+ },
844
+ style,
845
+ resetStyle
846
+ })(
847
+ cellValue,
848
+ columnIndex,
849
+ rowIndex,
850
+ rowData,
851
+ inputData
852
+ );
853
+ }
854
+ if (dryRun) {
855
+ return cellValue;
856
+ }
857
+ }
858
+ if (!cellOptions.reset) {
859
+ cellValue = colorizeCell(cellValue, cellOptions, rowType);
860
+ }
861
+ const { cell, innerWidth } = wrapCellText(cellOptions, cellValue, columnIndex, cellOptions, rowType);
862
+ if (rowType === "header") {
863
+ config.table.columnInnerWidths.push(innerWidth);
864
+ }
865
+ return cell;
866
+ };
867
+ var getConstructorGeometry = (row, config) => {
868
+ let type;
869
+ if (typeof row === "object" && !(row instanceof Array)) {
870
+ const keys = Object.keys(row);
871
+ if (config.adapter === "automattic") {
872
+ const key = keys[0];
873
+ if (row[key] instanceof Array) {
874
+ type = "automattic-cross";
875
+ } else {
876
+ type = "automattic-vertical";
877
+ }
878
+ } else {
879
+ type = "o-horizontal";
880
+ }
881
+ } else {
882
+ type = "a-horizontal";
883
+ }
884
+ return type;
885
+ };
886
+ var coerceConstructorGeometry = (config, rows, constructorType) => {
887
+ let output = [];
888
+ switch (constructorType) {
889
+ case "automattic-cross":
890
+ config.columnSettings[0] = config.columnSettings[0] || {};
891
+ config.columnSettings[0].color = config.headerColor;
892
+ output = rows.map((obj) => {
893
+ const arr = [];
894
+ const key = Object.keys(obj)[0];
895
+ arr.push(key);
896
+ return arr.concat(obj[key]);
897
+ });
898
+ break;
899
+ case "automattic-vertical":
900
+ config.columnSettings[0] = config.columnSettings[0] || {};
901
+ config.columnSettings[0].color = config.headerColor;
902
+ output = rows.map(function(value) {
903
+ const key = Object.keys(value)[0];
904
+ return [key, value[key]];
905
+ });
906
+ break;
907
+ case "o-horizontal":
908
+ if (config.table.header[0].length && config.table.header[0].every((obj) => obj.value)) {
909
+ output = rows.map((row) => config.table.header[0].map((obj) => row[obj.value]));
910
+ } else {
911
+ output = rows.map((obj) => Object.values(obj));
912
+ }
913
+ break;
914
+ case "a-horizontal":
915
+ output = rows;
916
+ break;
917
+ default:
918
+ }
919
+ return output;
920
+ };
921
+
922
+ // src/factory.ts
923
+ var counter = 0;
924
+ var Factory = function(paramsArr) {
925
+ const _configKey = Symbol.config;
926
+ let header = [];
927
+ const body = [];
928
+ let footer = [];
929
+ let options = {};
930
+ switch (true) {
931
+ // header, rows, footer, and options
932
+ case paramsArr.length === 4:
933
+ header = paramsArr[0];
934
+ body.push(...paramsArr[1]);
935
+ footer = paramsArr[2];
936
+ options = paramsArr[3];
937
+ break;
938
+ // header, rows, footer
939
+ case (paramsArr.length === 3 && paramsArr[2] instanceof Array):
940
+ header = paramsArr[0];
941
+ body.push(...paramsArr[1]);
942
+ footer = paramsArr[2];
943
+ break;
944
+ // header, rows, options
945
+ case (paramsArr.length === 3 && typeof paramsArr[2] === "object"):
946
+ header = paramsArr[0];
947
+ body.push(...paramsArr[1]);
948
+ options = paramsArr[2];
949
+ break;
950
+ // header, rows (rows, footer is not an option)
951
+ case (paramsArr.length === 2 && paramsArr[1] instanceof Array):
952
+ header = paramsArr[0];
953
+ body.push(...paramsArr[1]);
954
+ break;
955
+ // rows, options
956
+ case (paramsArr.length === 2 && typeof paramsArr[1] === "object"):
957
+ body.push(...paramsArr[0]);
958
+ options = paramsArr[1];
959
+ break;
960
+ // rows
961
+ case (paramsArr.length === 1 && paramsArr[0] instanceof Array):
962
+ body.push(...paramsArr[0]);
963
+ break;
964
+ // adapter called: i.e. `require('tty-table')('automattic-cli-table')`
965
+ case (paramsArr.length === 1 && typeof paramsArr[0] === "string"): {
966
+ const adapters = {
967
+ "automattic-cli-table": () => require("../adapters/automattic-cli-table.js"),
968
+ "default-adapter": () => require("../adapters/default-adapter.js"),
969
+ "terminal-adapter": () => require("../adapters/terminal-adapter.js")
970
+ };
971
+ const load = adapters[paramsArr[0]];
972
+ if (!load) throw new Error(`Unknown adapter: "${paramsArr[0]}". Available adapters: ${Object.keys(adapters).join(", ")}`);
973
+ return load();
974
+ }
975
+ /* istanbul ignore next */
976
+ default:
977
+ console.log("Error: Bad params. \nSee docs at github.com/tecfu/tty-table");
978
+ process.exit();
979
+ }
980
+ const cloneddefaults = JSON.parse(JSON.stringify(defaults_default));
981
+ const config = Object.assign({}, cloneddefaults, options);
982
+ config.align = config.alignment || config.align;
983
+ config.headerAlign = config.headerAlignment || config.headerAlign;
984
+ if (config.truncate === true) config.truncate = "";
985
+ if (config.borderColor) {
986
+ config.borderCharacters[config.borderStyle] = config.borderCharacters[config.borderStyle].map(function(obj) {
987
+ Object.keys(obj).forEach(function(key) {
988
+ obj[key] = style(obj[key], config.borderColor);
989
+ });
990
+ return obj;
991
+ });
992
+ }
993
+ config.columnSettings = header.slice(0);
994
+ config.table.header = header;
995
+ config.table.header = [config.table.header];
996
+ config.table.footer = footer;
997
+ if (config.terminalAdapter !== true) {
998
+ counter++;
999
+ }
1000
+ config.tableId = counter;
1001
+ const tableObject = Object.create(body);
1002
+ tableObject[_configKey] = config;
1003
+ tableObject.render = function() {
1004
+ const output = stringifyData(this[_configKey], this.slice(0));
1005
+ tableObject.height = this[_configKey].height;
1006
+ return output;
1007
+ };
1008
+ return tableObject;
1009
+ };
1010
+ var Table = function(...params) {
1011
+ return Factory(params);
1012
+ };
1013
+ Table.resetStyle = resetStyle;
1014
+ Table.style = styleEachChar;
1015
+ var factory_default = Table;
1016
+
1017
+ // src/index.ts
1018
+ var src_default = factory_default;
1019
+ ;if (typeof module !== "undefined" && typeof module.exports?.default === "function") { module.exports = Object.assign(module.exports.default, module.exports) }
1020
+ //# sourceMappingURL=index.js.map