tty-table 6.0.1 → 7.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/mcp.js ADDED
@@ -0,0 +1,937 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __commonJS = (cb, mod) => function __require() {
10
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
11
+ };
12
+ var __export = (target, all) => {
13
+ for (var name in all)
14
+ __defProp(target, name, { get: all[name], enumerable: true });
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from))
19
+ if (!__hasOwnProp.call(to, key) && key !== except)
20
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
+ }
22
+ return to;
23
+ };
24
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
25
+ // If the importer is in node compatibility mode or this is not an ESM
26
+ // file that has been converted to a CommonJS file using a Babel-
27
+ // compatible transform (i.e. "__esModule" has not been set), then set
28
+ // "default" to the CommonJS "module.exports" for node compatibility.
29
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
30
+ mod
31
+ ));
32
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
33
+
34
+ // node_modules/breakword/src/main.js
35
+ var require_main = __commonJS({
36
+ "node_modules/breakword/src/main.js"(exports2, module2) {
37
+ "use strict";
38
+ var UNICODE_VERSION = "18.0.0";
39
+ var WIDE_RANGES = [
40
+ [4352, 4447],
41
+ [8986, 8987],
42
+ [9001, 9002],
43
+ [9193, 9196],
44
+ [9200, 9200],
45
+ [9203, 9203],
46
+ [9725, 9726],
47
+ [9748, 9749],
48
+ [9776, 9783],
49
+ [9800, 9811],
50
+ [9855, 9855],
51
+ [9866, 9871],
52
+ [9875, 9875],
53
+ [9889, 9889],
54
+ [9898, 9899],
55
+ [9917, 9918],
56
+ [9924, 9925],
57
+ [9934, 9934],
58
+ [9940, 9940],
59
+ [9962, 9962],
60
+ [9970, 9971],
61
+ [9973, 9973],
62
+ [9978, 9978],
63
+ [9981, 9981],
64
+ [9989, 9989],
65
+ [9994, 9995],
66
+ [10024, 10024],
67
+ [10060, 10060],
68
+ [10062, 10062],
69
+ [10067, 10069],
70
+ [10071, 10071],
71
+ [10133, 10135],
72
+ [10160, 10160],
73
+ [10175, 10175],
74
+ [11035, 11036],
75
+ [11088, 11088],
76
+ [11093, 11093],
77
+ [11904, 11929],
78
+ [11931, 12019],
79
+ [12032, 12245],
80
+ [12272, 12350],
81
+ [12353, 12438],
82
+ [12441, 12543],
83
+ [12549, 12591],
84
+ [12593, 12686],
85
+ [12688, 12773],
86
+ [12783, 12830],
87
+ [12832, 12871],
88
+ [12880, 42124],
89
+ [42128, 42182],
90
+ [43360, 43388],
91
+ [44032, 55203],
92
+ [63744, 64255],
93
+ [65040, 65049],
94
+ [65072, 65106],
95
+ [65108, 65126],
96
+ [65128, 65131],
97
+ [65281, 65376],
98
+ [65504, 65510],
99
+ [94176, 94180],
100
+ [94192, 94198],
101
+ [94208, 101594],
102
+ [101631, 101664],
103
+ [101760, 101874],
104
+ [101888, 102801],
105
+ [102816, 102866],
106
+ [110576, 110579],
107
+ [110581, 110587],
108
+ [110589, 110590],
109
+ [110592, 110888],
110
+ [110898, 110898],
111
+ [110928, 110930],
112
+ [110933, 110933],
113
+ [110948, 110952],
114
+ [110960, 111355],
115
+ [119552, 119638],
116
+ [119648, 119670],
117
+ [126980, 126980],
118
+ [127183, 127183],
119
+ [127374, 127374],
120
+ [127377, 127386],
121
+ [127406, 127406],
122
+ [127488, 127490],
123
+ [127504, 127547],
124
+ [127552, 127560],
125
+ [127568, 127569],
126
+ [127584, 127589],
127
+ [127744, 127776],
128
+ [127789, 127797],
129
+ [127799, 127868],
130
+ [127870, 127891],
131
+ [127904, 127946],
132
+ [127951, 127955],
133
+ [127968, 127984],
134
+ [127988, 127988],
135
+ [127992, 128062],
136
+ [128064, 128064],
137
+ [128066, 128252],
138
+ [128255, 128317],
139
+ [128331, 128334],
140
+ [128336, 128359],
141
+ [128378, 128378],
142
+ [128405, 128406],
143
+ [128420, 128420],
144
+ [128507, 128591],
145
+ [128640, 128709],
146
+ [128716, 128716],
147
+ [128720, 128722],
148
+ [128725, 128729],
149
+ [128732, 128735],
150
+ [128747, 128748],
151
+ [128756, 128764],
152
+ [128986, 128986],
153
+ [128992, 129003],
154
+ [129008, 129008],
155
+ [129292, 129338],
156
+ [129340, 129349],
157
+ [129351, 129535],
158
+ [129648, 129660],
159
+ [129664, 129734],
160
+ [129736, 129736],
161
+ [129740, 129757],
162
+ [129759, 129771],
163
+ [129775, 129786],
164
+ [131072, 196605],
165
+ [196608, 262141]
166
+ ];
167
+ var cell = (cp) => cp <= 65535 ? `\\u${cp.toString(16).toUpperCase().padStart(4, "0")}` : `\\u{${cp.toString(16).toUpperCase()}}`;
168
+ var WIDE = new RegExp(
169
+ "[" + WIDE_RANGES.map(([lo, hi]) => cell(lo) + (hi > lo ? `-${cell(hi)}` : "")).join("") + "]",
170
+ "u"
171
+ );
172
+ var ZERO = /[\u200B\p{Mn}\p{Me}\p{Cf}\u1160-\u11FF]/u;
173
+ var EMOJI = /\p{Emoji_Presentation}/u;
174
+ function width(char) {
175
+ if (typeof char !== "string") {
176
+ throw new TypeError("width() expects a string");
177
+ }
178
+ if (char.length === 0) {
179
+ return 0;
180
+ }
181
+ if (char.length > 1 && [...char].length > 1) {
182
+ throw new TypeError("width() expects exactly one Unicode code point");
183
+ }
184
+ const cp = char.codePointAt(0);
185
+ if (cp < 32 || cp >= 127 && cp < 160) return 0;
186
+ if (cp === 173) return 1;
187
+ if (ZERO.test(char)) return 0;
188
+ if (EMOJI.test(char) && !(cp >= 127462 && cp <= 127487)) return 2;
189
+ return WIDE.test(char) ? 2 : 1;
190
+ }
191
+ module2.exports = function breakword2(input, breakAtLength) {
192
+ const str = String(input);
193
+ let indexOfLastFitChar = 0;
194
+ let fittableLength = 0;
195
+ let index = 0;
196
+ for (const char of str) {
197
+ const currentLength = fittableLength + width(char);
198
+ if (currentLength > breakAtLength) {
199
+ break;
200
+ }
201
+ indexOfLastFitChar = index;
202
+ fittableLength = currentLength;
203
+ index += 1;
204
+ }
205
+ return indexOfLastFitChar;
206
+ };
207
+ module2.exports.width = width;
208
+ module2.exports.internals = { UNICODE_VERSION, WIDE_RANGES, WIDE, ZERO, EMOJI, width };
209
+ }
210
+ });
211
+
212
+ // src/mcp.ts
213
+ var mcp_exports = {};
214
+ __export(mcp_exports, {
215
+ createMcpServer: () => createMcpServer,
216
+ handleRenderTable: () => handleRenderTable,
217
+ renderTableInputSchema: () => renderTableInputSchema
218
+ });
219
+ module.exports = __toCommonJS(mcp_exports);
220
+ var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
221
+ var import_stdio = require("@modelcontextprotocol/sdk/server/stdio.js");
222
+ var import_zod = require("zod");
223
+
224
+ // src/defaults.ts
225
+ var defaults = {
226
+ borderCharacters: {
227
+ invisible: [
228
+ { v: " ", l: " ", j: " ", h: " ", r: " " },
229
+ { v: " ", l: " ", j: " ", h: " ", r: " " },
230
+ { v: " ", l: " ", j: " ", h: " ", r: " " }
231
+ ],
232
+ solid: [
233
+ { v: "\u2502", l: "\u250C", j: "\u252C", h: "\u2500", r: "\u2510" },
234
+ { v: "\u2502", l: "\u251C", j: "\u253C", h: "\u2500", r: "\u2524" },
235
+ { v: "\u2502", l: "\u2514", j: "\u2534", h: "\u2500", r: "\u2518" }
236
+ ],
237
+ dashed: [
238
+ { v: "|", l: "+", j: "+", h: "-", r: "+" },
239
+ { v: "|", l: "+", j: "+", h: "-", r: "+" },
240
+ { v: "|", l: "+", j: "+", h: "-", r: "+" }
241
+ ],
242
+ none: [
243
+ { v: "", l: "", j: "", h: "", r: "" },
244
+ { v: "", l: "", j: "", h: "", r: "" },
245
+ { v: "", l: "", j: "", h: "", r: "" }
246
+ ]
247
+ },
248
+ align: "center",
249
+ borderColor: null,
250
+ borderStyle: "solid",
251
+ color: false,
252
+ COLUMNS: 80,
253
+ // if !process.stdout.columns assume redirecting to write stream 80 columns is VT200 standard
254
+ compact: false,
255
+ defaultErrorValue: "\uFFFD",
256
+ defaultValue: "",
257
+ errorOnNull: false,
258
+ FIXED_WIDTH: false,
259
+ footerAlign: "center",
260
+ footerColor: false,
261
+ formatter: null,
262
+ headerAlign: "center",
263
+ headerColor: "yellow",
264
+ isNull: false,
265
+ // undocumented cell setting
266
+ marginLeft: 2,
267
+ marginTop: 1,
268
+ paddingBottom: 0,
269
+ paddingLeft: 1,
270
+ paddingRight: 1,
271
+ paddingTop: 0,
272
+ showHeader: null,
273
+ // undocumented
274
+ truncate: false,
275
+ width: "100%",
276
+ GUTTER: 1,
277
+ // undocumented
278
+ columnSettings: [],
279
+ // save so cell options can be merged into column options
280
+ table: {
281
+ body: "",
282
+ columnInnerWidths: [],
283
+ columnWidths: [],
284
+ columns: [],
285
+ footer: "",
286
+ header: "",
287
+ // post-rendered strings.
288
+ height: 0,
289
+ typeLocked: false
290
+ // once a table type is selected can't switch
291
+ }
292
+ };
293
+ defaults.borderCharacters["0"] = defaults.borderCharacters.none;
294
+ defaults.borderCharacters["1"] = defaults.borderCharacters.solid;
295
+ defaults.borderCharacters["2"] = defaults.borderCharacters.dashed;
296
+ var defaults_default = defaults;
297
+
298
+ // src/style.ts
299
+ var import_chalk = __toESM(require("chalk"));
300
+ var import_kleur = __toESM(require("kleur"));
301
+ var import_strip_ansi = __toESM(require("strip-ansi"));
302
+ var colorLib = process && process.stdout ? import_chalk.default : import_kleur.default;
303
+ var style = (str, ...colors) => {
304
+ const out = colors.reduce(function(input, color) {
305
+ return colorLib[color](input);
306
+ }, str);
307
+ return out;
308
+ };
309
+ var styleEachChar = (str, ...colors) => {
310
+ const chars = [...(0, import_strip_ansi.default)(str)];
311
+ const out = chars.reduce((prev, current) => {
312
+ const coded = colors.reduce((input, color) => {
313
+ return colorLib[color](input);
314
+ }, current);
315
+ return prev + coded;
316
+ }, "");
317
+ return out;
318
+ };
319
+ var resetStyle = function(str) {
320
+ this.configure({ reset: true });
321
+ return (0, import_strip_ansi.default)(str);
322
+ };
323
+ var colorizeCell = (str, cellOptions, rowType) => {
324
+ let color = false;
325
+ switch (true) {
326
+ case rowType === "body":
327
+ color = cellOptions.color || color;
328
+ break;
329
+ case rowType === "header":
330
+ color = cellOptions.headerColor || color;
331
+ break;
332
+ default:
333
+ color = cellOptions.footerColor || color;
334
+ }
335
+ if (color) {
336
+ str = style(str, color);
337
+ }
338
+ return str;
339
+ };
340
+ var isColorEnabled = () => {
341
+ return process && process.stdout ? colorLib.level > 0 : colorLib.enabled;
342
+ };
343
+
344
+ // src/format.ts
345
+ var import_smartwrap = __toESM(require("smartwrap"));
346
+
347
+ // src/ansi.ts
348
+ var import_breakword = __toESM(require_main());
349
+ var ANSI = /\u001B\[[0-?]*[ -\/]*[@-~]/g;
350
+ var stripAnsi2 = (value) => value.replace(ANSI, "");
351
+ var codePointWidth = (value) => [...value].reduce((total, char) => total + import_breakword.default.width(char), 0);
352
+ var displayWidth = (value) => Math.max(0, ...stripAnsi2(value).split(/\r?\n/).map(codePointWidth));
353
+
354
+ // src/format.ts
355
+ var addPadding = (config, width) => {
356
+ return width + config.paddingLeft + config.paddingRight;
357
+ };
358
+ var getDisplayWidth = (value) => {
359
+ return displayWidth(value.toString());
360
+ };
361
+ var getMaxLength = (columnOptions, rows, columnIndex) => {
362
+ let widest = 0;
363
+ if (columnOptions && (columnOptions.value || columnOptions.alias)) {
364
+ const value = columnOptions.alias || columnOptions.value;
365
+ widest = getDisplayWidth(value);
366
+ }
367
+ for (const row of rows) {
368
+ if (row[columnIndex]) {
369
+ const value = row[columnIndex].value ? row[columnIndex].value : row[columnIndex];
370
+ const width = getDisplayWidth(value);
371
+ if (width > widest) widest = width;
372
+ }
373
+ }
374
+ return widest;
375
+ };
376
+ var getAvailableWidth = (config) => {
377
+ if (process && (process.stdout && process.stdout.columns || process.env && process.env.COLUMNS)) {
378
+ let viewport = process.stdout && process.stdout.columns ? process.stdout.columns : process.env.COLUMNS;
379
+ viewport = viewport - config.marginLeft;
380
+ if (config.width !== "auto" && /^\d+%$/.test(config.width)) {
381
+ return Math.min(1, config.width.slice(0, -1) * 0.01) * viewport;
382
+ }
383
+ if (config.width !== "auto" && /^\d+$/.test(config.width)) {
384
+ config.FIXED_WIDTH = true;
385
+ return config.width;
386
+ }
387
+ return viewport;
388
+ }
389
+ if (typeof globalThis.window !== "undefined") return globalThis.window.innerWidth;
390
+ return config.COLUMNS - config.marginLeft;
391
+ };
392
+ var getStringLength = (str) => {
393
+ return displayWidth(str);
394
+ };
395
+ var wrapCellText = (config, cellValue, columnIndex, cellOptions, rowType) => {
396
+ const startAnsiRegexp = /^(\x1b\[[0-9;]*m)+/;
397
+ const endAnsiRegexp = /(\x1b\[[0-9;]*m)+$/;
398
+ let str = cellValue.toString();
399
+ const startMatches = str.match(startAnsiRegexp) || [""];
400
+ str = str.replace(startAnsiRegexp, "");
401
+ const endMatches = str.match(endAnsiRegexp) || [""];
402
+ str = str.replace(endAnsiRegexp, "");
403
+ let alignTgt;
404
+ switch (rowType) {
405
+ case "header":
406
+ alignTgt = "headerAlign";
407
+ break;
408
+ case "body":
409
+ alignTgt = "align";
410
+ break;
411
+ default:
412
+ alignTgt = "footerAlign";
413
+ }
414
+ if (cellOptions[alignTgt] === "center") {
415
+ cellOptions.paddingLeft = cellOptions.paddingRight = Math.max(
416
+ cellOptions.paddingRight,
417
+ cellOptions.paddingLeft,
418
+ 0
419
+ );
420
+ }
421
+ const columnWidth = config.table.columnWidths[columnIndex];
422
+ const innerWidth = columnWidth - cellOptions.paddingLeft - cellOptions.paddingRight - config.GUTTER;
423
+ if (typeof config.truncate === "string") {
424
+ str = truncate(str, cellOptions, innerWidth);
425
+ } else {
426
+ str = wrap(str, cellOptions, innerWidth);
427
+ }
428
+ const cell = str.split("\n").map((line) => {
429
+ line = line.trim();
430
+ const lineLength = getStringLength(line);
431
+ if (lineLength < columnWidth) {
432
+ let emptySpace = columnWidth - lineLength;
433
+ switch (true) {
434
+ case cellOptions[alignTgt] === "center":
435
+ emptySpace--;
436
+ const padBoth = Math.floor(emptySpace / 2);
437
+ const padRemainder = emptySpace % 2;
438
+ line = " ".repeat(padBoth) + line + " ".repeat(padBoth + padRemainder);
439
+ break;
440
+ case cellOptions[alignTgt] === "right":
441
+ line = " ".repeat(emptySpace - cellOptions.paddingRight - 1) + line + " ".repeat(cellOptions.paddingRight);
442
+ break;
443
+ default:
444
+ line = " ".repeat(cellOptions.paddingLeft) + line + " ".repeat(emptySpace - cellOptions.paddingLeft - 1);
445
+ }
446
+ }
447
+ return startMatches[0] + line + endMatches[0];
448
+ });
449
+ return { cell, innerWidth };
450
+ };
451
+ var truncate = (str, cellOptions, maxWidth) => {
452
+ const stringWidth = displayWidth(str);
453
+ if (maxWidth < stringWidth) {
454
+ str = (0, import_smartwrap.default)(str, {
455
+ width: maxWidth - displayWidth(cellOptions.truncate),
456
+ breakword: true
457
+ }).split("\n")[0];
458
+ str = str + cellOptions.truncate;
459
+ }
460
+ return str;
461
+ };
462
+ var wrap = (str, cellOptions, innerWidth) => {
463
+ const outstring = (0, import_smartwrap.default)(str, {
464
+ errorChar: cellOptions.defaultErrorValue,
465
+ minWidth: 1,
466
+ trim: true,
467
+ width: innerWidth
468
+ });
469
+ return outstring;
470
+ };
471
+ var getColumnWidths = (config, rows) => {
472
+ const availableWidth = getAvailableWidth(config);
473
+ const iterable = config.table.header[0] && config.table.header[0].length > 0 ? config.table.header[0] : rows[0];
474
+ let widths = iterable.map((column, columnIndex) => {
475
+ let result;
476
+ switch (true) {
477
+ // column width is a percentage of table width specified in column header
478
+ case (typeof column === "object" && /^\d+%$/.test(column.width)):
479
+ result = column.width.slice(0, -1) * 0.01 * availableWidth;
480
+ result = addPadding(config, result);
481
+ break;
482
+ // column width is specified in column header
483
+ case (typeof column === "object" && /^\d+$/.test(column.width)):
484
+ result = column.width;
485
+ break;
486
+ // 'auto' sets column width to its longest value in the initial data set
487
+ default: {
488
+ const columnOptions = config.table.header[0][columnIndex] ? config.table.header[0][columnIndex] : {};
489
+ const measurableRows = rows.length ? rows : config.table.header[0];
490
+ result = getMaxLength(columnOptions, measurableRows, columnIndex);
491
+ result = addPadding(config, result);
492
+ }
493
+ }
494
+ result = result + config.GUTTER;
495
+ return result;
496
+ });
497
+ const totalWidth = widths.reduce((prev, current) => prev + current);
498
+ if (totalWidth > availableWidth || config.FIXED_WIDTH) {
499
+ const proportion = (availableWidth / totalWidth).toFixed(2) - 0.01;
500
+ const relativeWidths = widths.map((value) => Math.max(2, Math.floor(proportion * value)));
501
+ if (config.FIXED_WIDTH) return relativeWidths;
502
+ if (proportion > 0) {
503
+ const totalRelativeWidths = relativeWidths.reduce((prev, current) => prev + current);
504
+ widths = totalRelativeWidths < totalWidth ? relativeWidths : widths;
505
+ }
506
+ } else {
507
+ widths = widths.map(Math.floor);
508
+ }
509
+ return widths;
510
+ };
511
+
512
+ // src/render.ts
513
+ var import_strip_ansi2 = __toESM(require("strip-ansi"));
514
+ var stringifyData = (config, inputData) => {
515
+ const sections = { header: [], body: [], footer: [] };
516
+ const marginLeft = " ".repeat(config.marginLeft);
517
+ const borderStyle = config.borderCharacters[config.borderStyle];
518
+ const borders = [];
519
+ const constructorType = getConstructorGeometry(inputData[0] || [], config);
520
+ const rows = coerceConstructorGeometry(config, inputData, constructorType);
521
+ if (!global.columnWidths) global.columnWidths = {};
522
+ if (global.columnWidths[config.tableId]) {
523
+ config.table.columnWidths = global.columnWidths[config.tableId];
524
+ } else {
525
+ const formattedRows = rows.map((row, rowIndex) => {
526
+ return row.map((cell, cellIndex) => buildCell(config, cell, cellIndex, "body", rowIndex, rows, inputData, true));
527
+ });
528
+ global.columnWidths[config.tableId] = config.table.columnWidths = getColumnWidths(config, formattedRows);
529
+ }
530
+ switch (true) {
531
+ case (config.showHeader !== null && !config.showHeader):
532
+ sections.header = [];
533
+ break;
534
+ case config.showHeader === true:
535
+ case !!config.table.header[0].find((obj) => obj.value || obj.alias):
536
+ sections.header = config.table.header.map((row) => buildRow(config, row, "header", null, rows, inputData));
537
+ break;
538
+ default:
539
+ sections.header = [];
540
+ }
541
+ sections.body = rows.map((row, rowIndex) => buildRow(config, row, "body", rowIndex, rows, inputData));
542
+ sections.footer = config.table.footer instanceof Array && config.table.footer.length > 0 ? [config.table.footer] : [];
543
+ sections.footer = sections.footer.map((row) => buildRow(config, row, "footer", null, rows, inputData));
544
+ for (let a = 0; a < 3; a++) {
545
+ borders[a] = borderStyle[a].l;
546
+ config.table.columnWidths.forEach((columnWidth, index, arr) => {
547
+ borders[a] += borderStyle[a].h.repeat(Math.max(columnWidth, 2) - 1);
548
+ borders[a] += index + 1 < arr.length ? borderStyle[a].j : "";
549
+ });
550
+ borders[a] += borderStyle[a].r;
551
+ borders[a] = a < 2 ? `${marginLeft + borders[a]}
552
+ ` : marginLeft + borders[a];
553
+ }
554
+ const output = [borders[0]];
555
+ for (const [sectionIndex, sectionName] of Object.keys(sections).entries()) {
556
+ const section = sections[sectionName];
557
+ for (let rowIndex = 0; rowIndex < section.length; rowIndex++) {
558
+ const row = section[rowIndex];
559
+ row.forEach((line) => {
560
+ output.push(marginLeft + borderStyle[1].v + line.join(borderStyle[1].v) + borderStyle[1].v + "\n");
561
+ });
562
+ switch (true) {
563
+ case (rowIndex === section.length - 1 && sectionIndex === 1 && sections.footer.length === 0):
564
+ break;
565
+ case (rowIndex === section.length - 1 && sectionIndex === 2):
566
+ break;
567
+ case (config.compact && sectionName === "body" && !row.empty):
568
+ break;
569
+ case (config.borderStyle === "none" && config.compact):
570
+ break;
571
+ default:
572
+ output.push(borders[1]);
573
+ }
574
+ }
575
+ }
576
+ output.push(borders[2]);
577
+ const finalOutput = "\n".repeat(config.marginTop) + output.join("");
578
+ config.height = finalOutput.split(/\r\n|\r|\n/).length;
579
+ return finalOutput;
580
+ };
581
+ var buildRow = (config, row, rowType, rowIndex, rowData, inputData) => {
582
+ let minRowHeight = 0;
583
+ if (row.length === 0 && config.compact) {
584
+ row.empty = true;
585
+ return row;
586
+ }
587
+ const lengthDifference = config.table.columnWidths.length - row.length;
588
+ if (lengthDifference > 0) row = row.concat(Array.apply(null, new Array(lengthDifference)).map(() => null));
589
+ else if (lengthDifference < 0) row.length = config.table.columnWidths.length;
590
+ row = row.map((elem, elemIndex) => {
591
+ const cell = buildCell(config, elem, elemIndex, rowType, rowIndex, rowData, inputData);
592
+ minRowHeight = minRowHeight < cell.length ? cell.length : minRowHeight;
593
+ return cell;
594
+ });
595
+ minRowHeight = rowType === "header" ? minRowHeight : minRowHeight + (config.paddingBottom + config.paddingTop);
596
+ const linedRow = Array.apply(null, { length: minRowHeight }).map(Function.call, () => []);
597
+ row.forEach(function(cell, a) {
598
+ const whitespace = " ".repeat(Math.max(config.table.columnWidths[a] - 1, 0));
599
+ if (rowType === "body") {
600
+ for (let i = 0; i < config.paddingTop; i++) cell.unshift(whitespace);
601
+ for (let i = 0; i < config.paddingBottom; i++) cell.push(whitespace);
602
+ }
603
+ for (let i = 0; i < minRowHeight; i++) linedRow[i].push(typeof cell[i] !== "undefined" ? cell[i] : whitespace);
604
+ });
605
+ return linedRow;
606
+ };
607
+ var buildCell = (config, elem, columnIndex, rowType, rowIndex, rowData, inputData, dryRun = false) => {
608
+ let cellValue = null;
609
+ const cellOptions = Object.assign(
610
+ { reset: false },
611
+ config,
612
+ rowType !== "header" ? config.columnSettings[columnIndex] : {},
613
+ typeof elem === "object" ? elem : {}
614
+ );
615
+ if (rowType === "header") {
616
+ config.table.columns.push(cellOptions);
617
+ cellValue = cellOptions.alias || cellOptions.value || "";
618
+ } else {
619
+ switch (true) {
620
+ case (typeof elem === "undefined" || elem === null):
621
+ cellValue = config.errorOnNull ? config.defaultErrorValue : config.defaultValue;
622
+ if (!isColorEnabled()) cellValue = (0, import_strip_ansi2.default)(cellValue);
623
+ cellOptions.isNull = true;
624
+ break;
625
+ case (typeof elem === "object" && elem !== null && typeof elem.value !== "undefined"):
626
+ cellValue = elem.value;
627
+ break;
628
+ case typeof elem === "function":
629
+ cellValue = elem.bind({
630
+ configure: function(object) {
631
+ return Object.assign(cellOptions, object);
632
+ },
633
+ style,
634
+ resetStyle
635
+ })(cellValue, columnIndex, rowIndex, rowData, inputData);
636
+ break;
637
+ default:
638
+ cellValue = elem;
639
+ }
640
+ if (rowType === "body" && typeof cellOptions.formatter === "function") {
641
+ cellValue = cellOptions.formatter.bind({
642
+ configure: function(object) {
643
+ return Object.assign(cellOptions, object);
644
+ },
645
+ style,
646
+ resetStyle
647
+ })(cellValue, columnIndex, rowIndex, rowData, inputData);
648
+ }
649
+ if (dryRun) return cellValue;
650
+ }
651
+ if (!cellOptions.reset) cellValue = colorizeCell(cellValue, cellOptions, rowType);
652
+ const { cell, innerWidth } = wrapCellText(cellOptions, cellValue, columnIndex, cellOptions, rowType);
653
+ if (rowType === "header") config.table.columnInnerWidths.push(innerWidth);
654
+ return cell;
655
+ };
656
+ var getConstructorGeometry = (row, config) => {
657
+ let type;
658
+ if (typeof row === "object" && !(row instanceof Array)) {
659
+ const keys = Object.keys(row);
660
+ if (config.adapter === "automattic") {
661
+ const key = keys[0];
662
+ type = row[key] instanceof Array ? "automattic-cross" : "automattic-vertical";
663
+ } else type = "o-horizontal";
664
+ } else type = "a-horizontal";
665
+ return type;
666
+ };
667
+ var coerceConstructorGeometry = (config, rows, constructorType) => {
668
+ let output = [];
669
+ switch (constructorType) {
670
+ case "automattic-cross":
671
+ config.columnSettings[0] = config.columnSettings[0] || {};
672
+ config.columnSettings[0].color = config.headerColor;
673
+ output = rows.map((obj) => {
674
+ const key = Object.keys(obj)[0];
675
+ return [key].concat(obj[key]);
676
+ });
677
+ break;
678
+ case "automattic-vertical":
679
+ config.columnSettings[0] = config.columnSettings[0] || {};
680
+ config.columnSettings[0].color = config.headerColor;
681
+ output = rows.map((value) => {
682
+ const key = Object.keys(value)[0];
683
+ return [key, value[key]];
684
+ });
685
+ break;
686
+ case "o-horizontal":
687
+ if (config.table.header[0].length && config.table.header[0].every((obj) => obj.value)) {
688
+ output = rows.map((row) => config.table.header[0].map((obj) => row[obj.value]));
689
+ } else output = rows.map((obj) => Object.values(obj));
690
+ break;
691
+ case "a-horizontal":
692
+ output = rows;
693
+ break;
694
+ }
695
+ return output;
696
+ };
697
+
698
+ // src/factory.ts
699
+ var counter = 0;
700
+ var Factory = function(paramsArr) {
701
+ const _configKey = Symbol.config;
702
+ let header = [];
703
+ const body = [];
704
+ let footer = [];
705
+ let options = {};
706
+ switch (true) {
707
+ // header, rows, footer, and options
708
+ case paramsArr.length === 4:
709
+ header = paramsArr[0];
710
+ body.push(...paramsArr[1]);
711
+ footer = paramsArr[2];
712
+ options = paramsArr[3];
713
+ break;
714
+ // header, rows, footer
715
+ case (paramsArr.length === 3 && paramsArr[2] instanceof Array):
716
+ header = paramsArr[0];
717
+ body.push(...paramsArr[1]);
718
+ footer = paramsArr[2];
719
+ break;
720
+ // header, rows, options
721
+ case (paramsArr.length === 3 && typeof paramsArr[2] === "object"):
722
+ header = paramsArr[0];
723
+ body.push(...paramsArr[1]);
724
+ options = paramsArr[2];
725
+ break;
726
+ // header, rows (rows, footer is not an option)
727
+ case (paramsArr.length === 2 && paramsArr[1] instanceof Array):
728
+ header = paramsArr[0];
729
+ body.push(...paramsArr[1]);
730
+ break;
731
+ // rows, options
732
+ case (paramsArr.length === 2 && typeof paramsArr[1] === "object"):
733
+ body.push(...paramsArr[0]);
734
+ options = paramsArr[1];
735
+ break;
736
+ // rows
737
+ case (paramsArr.length === 1 && paramsArr[0] instanceof Array):
738
+ body.push(...paramsArr[0]);
739
+ break;
740
+ // adapter called: i.e. `require('tty-table')('automattic-cli-table')`
741
+ case (paramsArr.length === 1 && typeof paramsArr[0] === "string"): {
742
+ const adapters = {
743
+ "automattic-cli-table": () => require("../adapters/automattic-cli-table.js"),
744
+ "default-adapter": () => require("../adapters/default-adapter.js"),
745
+ "terminal-adapter": () => require("../adapters/terminal-adapter.js")
746
+ };
747
+ const load = adapters[paramsArr[0]];
748
+ if (!load) throw new Error(`Unknown adapter: "${paramsArr[0]}". Available adapters: ${Object.keys(adapters).join(", ")}`);
749
+ return load();
750
+ }
751
+ /* istanbul ignore next */
752
+ default:
753
+ console.log("Error: Bad params. \nSee docs at github.com/tecfu/tty-table");
754
+ process.exit();
755
+ }
756
+ const cloneddefaults = JSON.parse(JSON.stringify(defaults_default));
757
+ const config = Object.assign({}, cloneddefaults, options);
758
+ config.align = config.alignment || config.align;
759
+ config.headerAlign = config.headerAlignment || config.headerAlign;
760
+ if (config.truncate === true) config.truncate = "";
761
+ if (config.borderColor) {
762
+ config.borderCharacters[config.borderStyle] = config.borderCharacters[config.borderStyle].map(function(obj) {
763
+ Object.keys(obj).forEach(function(key) {
764
+ obj[key] = style(obj[key], config.borderColor);
765
+ });
766
+ return obj;
767
+ });
768
+ }
769
+ config.columnSettings = header.slice(0);
770
+ config.table.header = header;
771
+ config.table.header = [config.table.header];
772
+ config.table.footer = footer;
773
+ if (config.terminalAdapter !== true) {
774
+ counter++;
775
+ }
776
+ config.tableId = counter;
777
+ const tableObject = Object.create(body);
778
+ tableObject[_configKey] = config;
779
+ tableObject.render = function() {
780
+ const output = stringifyData(this[_configKey], this.slice(0));
781
+ tableObject.height = this[_configKey].height;
782
+ return output;
783
+ };
784
+ return tableObject;
785
+ };
786
+ var Table = function(...params) {
787
+ return Factory(params);
788
+ };
789
+ Table.resetStyle = resetStyle;
790
+ Table.style = styleEachChar;
791
+ var factory_default = Table;
792
+
793
+ // src/index.ts
794
+ var src_default = factory_default;
795
+
796
+ // package.json
797
+ var package_default = {
798
+ name: "tty-table",
799
+ version: "7.0.0",
800
+ description: "Typed terminal table renderer for Node.js and browsers",
801
+ main: "dist/index.js",
802
+ module: "dist/index.mjs",
803
+ types: "dist/index.d.ts",
804
+ browser: "dist/browser/tty-table.global.js",
805
+ exports: {
806
+ ".": {
807
+ types: "./dist/index.d.ts",
808
+ import: "./dist/index.mjs",
809
+ require: "./dist/index.js"
810
+ }
811
+ },
812
+ engines: {
813
+ node: ">=22"
814
+ },
815
+ bin: {
816
+ "tty-table": "adapters/terminal-adapter.js",
817
+ "tty-table-mcp": "dist/mcp.js"
818
+ },
819
+ files: [
820
+ "dist/",
821
+ "adapters/",
822
+ "README.md",
823
+ "LICENSE.txt"
824
+ ],
825
+ scripts: {
826
+ build: "tsup && tsup --config tsup.browser.config.ts",
827
+ test: "npx mocha",
828
+ "test:watch": "vitest",
829
+ typecheck: "tsc --noEmit",
830
+ lint: "eslint .",
831
+ "lint:fix": "eslint . --fix",
832
+ prepublishOnly: "npm run typecheck && npm run build && npm test && npm run test:unit",
833
+ "test:unit": "vitest run",
834
+ "view-examples": "node npm_scripts/run-examples.js view",
835
+ "save-tests": "node npm_scripts/run-examples.js save"
836
+ },
837
+ repository: {
838
+ type: "git",
839
+ url: "https://github.com/tecfu/tty-table.git"
840
+ },
841
+ keywords: [
842
+ "table",
843
+ "cli-table",
844
+ "terminal table",
845
+ "console table",
846
+ "ascii table"
847
+ ],
848
+ author: "Tecfu",
849
+ license: "MIT",
850
+ bugs: {
851
+ url: "https://github.com/tecfu/tty-table/issues"
852
+ },
853
+ homepage: "https://github.com/tecfu/tty-table",
854
+ dependencies: {
855
+ "@modelcontextprotocol/sdk": "^1.30.0",
856
+ breakword: "^2.1.0",
857
+ chalk: "^4.1.2",
858
+ csv: "^6.3.11",
859
+ kleur: "^4.1.5",
860
+ smartwrap: "^4.0.0",
861
+ "strip-ansi": "^6.0.1",
862
+ yargs: "^17.7.2",
863
+ zod: "^4.6.5"
864
+ },
865
+ devDependencies: {
866
+ "@eslint/js": "^9.30.0",
867
+ "@types/node": "^22.15.0",
868
+ "@types/yargs": "^17.0.33",
869
+ chai: "^4.5.0",
870
+ commander: "^4.1.1",
871
+ eslint: "^9.30.0",
872
+ glob: "^13.0.6",
873
+ mocha: "^12.0.0",
874
+ tsup: "^8.5.0",
875
+ typescript: "^5.8.3",
876
+ "typescript-eslint": "^8.35.0",
877
+ vitest: "^4.1.11"
878
+ },
879
+ defaultTestColumns: 90
880
+ };
881
+
882
+ // src/mcp.ts
883
+ var headerEntry = import_zod.z.union([
884
+ import_zod.z.string(),
885
+ import_zod.z.object({
886
+ value: import_zod.z.string(),
887
+ align: import_zod.z.enum(["left", "center", "right"]).optional(),
888
+ width: import_zod.z.union([import_zod.z.number(), import_zod.z.string()]).optional()
889
+ })
890
+ ]);
891
+ var renderTableInputSchema = {
892
+ header: import_zod.z.array(headerEntry).optional().describe("Column definitions: names, or {value, align, width} objects"),
893
+ rows: import_zod.z.array(import_zod.z.union([import_zod.z.array(import_zod.z.unknown()), import_zod.z.record(import_zod.z.string(), import_zod.z.unknown())])).describe("Row data: arrays of cells, or objects keyed by column name"),
894
+ options: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional().describe('tty-table options, e.g. {width: 80, borderStyle: "solid"}')
895
+ };
896
+ function handleRenderTable({ header, rows, options }) {
897
+ try {
898
+ const opts = options ?? {};
899
+ const head = header;
900
+ const table = head?.length ? src_default(head, rows, opts) : src_default(rows, opts);
901
+ return { content: [{ type: "text", text: table.render() }] };
902
+ } catch (error) {
903
+ return {
904
+ isError: true,
905
+ content: [{ type: "text", text: error instanceof Error ? error.message : String(error) }]
906
+ };
907
+ }
908
+ }
909
+ function createMcpServer() {
910
+ const server = new import_mcp.McpServer({ name: "tty-table", version: package_default.version });
911
+ server.registerTool(
912
+ "render_table",
913
+ {
914
+ title: "Render a terminal table",
915
+ 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.",
916
+ inputSchema: renderTableInputSchema
917
+ },
918
+ (args) => handleRenderTable(args)
919
+ );
920
+ return server;
921
+ }
922
+ 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"));
923
+ if (isMain) {
924
+ const server = createMcpServer();
925
+ void server.connect(new import_stdio.StdioServerTransport()).catch((error) => {
926
+ process.stderr.write(error instanceof Error ? error.stack ?? error.message : String(error));
927
+ process.exit(1);
928
+ });
929
+ }
930
+ // Annotate the CommonJS export names for ESM import in node:
931
+ 0 && (module.exports = {
932
+ createMcpServer,
933
+ handleRenderTable,
934
+ renderTableInputSchema
935
+ });
936
+ ;if (typeof module !== "undefined" && typeof module.exports?.default === "function") { module.exports = Object.assign(module.exports.default, module.exports) }
937
+ //# sourceMappingURL=mcp.js.map