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