@oh-my-pi/pi-tui 1.337.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.
@@ -0,0 +1,646 @@
1
+ import { marked, type Token } from "marked";
2
+ import type { Component } from "../tui.js";
3
+ import { applyBackgroundToLine, visibleWidth, wrapTextWithAnsi } from "../utils.js";
4
+
5
+ /**
6
+ * Default text styling for markdown content.
7
+ * Applied to all text unless overridden by markdown formatting.
8
+ */
9
+ export interface DefaultTextStyle {
10
+ /** Foreground color function */
11
+ color?: (text: string) => string;
12
+ /** Background color function */
13
+ bgColor?: (text: string) => string;
14
+ /** Bold text */
15
+ bold?: boolean;
16
+ /** Italic text */
17
+ italic?: boolean;
18
+ /** Strikethrough text */
19
+ strikethrough?: boolean;
20
+ /** Underline text */
21
+ underline?: boolean;
22
+ }
23
+
24
+ /**
25
+ * Theme functions for markdown elements.
26
+ * Each function takes text and returns styled text with ANSI codes.
27
+ */
28
+ export interface MarkdownTheme {
29
+ heading: (text: string) => string;
30
+ link: (text: string) => string;
31
+ linkUrl: (text: string) => string;
32
+ code: (text: string) => string;
33
+ codeBlock: (text: string) => string;
34
+ codeBlockBorder: (text: string) => string;
35
+ quote: (text: string) => string;
36
+ quoteBorder: (text: string) => string;
37
+ hr: (text: string) => string;
38
+ listBullet: (text: string) => string;
39
+ bold: (text: string) => string;
40
+ italic: (text: string) => string;
41
+ strikethrough: (text: string) => string;
42
+ underline: (text: string) => string;
43
+ highlightCode?: (code: string, lang?: string) => string[];
44
+ }
45
+
46
+ export class Markdown implements Component {
47
+ private text: string;
48
+ private paddingX: number; // Left/right padding
49
+ private paddingY: number; // Top/bottom padding
50
+ private defaultTextStyle?: DefaultTextStyle;
51
+ private theme: MarkdownTheme;
52
+ private defaultStylePrefix?: string;
53
+
54
+ // Cache for rendered output
55
+ private cachedText?: string;
56
+ private cachedWidth?: number;
57
+ private cachedLines?: string[];
58
+
59
+ constructor(
60
+ text: string,
61
+ paddingX: number,
62
+ paddingY: number,
63
+ theme: MarkdownTheme,
64
+ defaultTextStyle?: DefaultTextStyle,
65
+ ) {
66
+ this.text = text;
67
+ this.paddingX = paddingX;
68
+ this.paddingY = paddingY;
69
+ this.theme = theme;
70
+ this.defaultTextStyle = defaultTextStyle;
71
+ }
72
+
73
+ setText(text: string): void {
74
+ this.text = text;
75
+ this.invalidate();
76
+ }
77
+
78
+ invalidate(): void {
79
+ this.cachedText = undefined;
80
+ this.cachedWidth = undefined;
81
+ this.cachedLines = undefined;
82
+ }
83
+
84
+ render(width: number): string[] {
85
+ // Check cache
86
+ if (this.cachedLines && this.cachedText === this.text && this.cachedWidth === width) {
87
+ return this.cachedLines;
88
+ }
89
+
90
+ // Calculate available width for content (subtract horizontal padding)
91
+ const contentWidth = Math.max(1, width - this.paddingX * 2);
92
+
93
+ // Don't render anything if there's no actual text
94
+ if (!this.text || this.text.trim() === "") {
95
+ const result: string[] = [];
96
+ // Update cache
97
+ this.cachedText = this.text;
98
+ this.cachedWidth = width;
99
+ this.cachedLines = result;
100
+ return result;
101
+ }
102
+
103
+ // Replace tabs with 3 spaces for consistent rendering
104
+ const normalizedText = this.text.replace(/\t/g, " ");
105
+
106
+ // Parse markdown to HTML-like tokens
107
+ const tokens = marked.lexer(normalizedText);
108
+
109
+ // Convert tokens to styled terminal output
110
+ const renderedLines: string[] = [];
111
+
112
+ for (let i = 0; i < tokens.length; i++) {
113
+ const token = tokens[i];
114
+ const nextToken = tokens[i + 1];
115
+ const tokenLines = this.renderToken(token, contentWidth, nextToken?.type);
116
+ renderedLines.push(...tokenLines);
117
+ }
118
+
119
+ // Wrap lines (NO padding, NO background yet)
120
+ const wrappedLines: string[] = [];
121
+ for (const line of renderedLines) {
122
+ wrappedLines.push(...wrapTextWithAnsi(line, contentWidth));
123
+ }
124
+
125
+ // Add margins and background to each wrapped line
126
+ const leftMargin = " ".repeat(this.paddingX);
127
+ const rightMargin = " ".repeat(this.paddingX);
128
+ const bgFn = this.defaultTextStyle?.bgColor;
129
+ const contentLines: string[] = [];
130
+
131
+ for (const line of wrappedLines) {
132
+ const lineWithMargins = leftMargin + line + rightMargin;
133
+
134
+ if (bgFn) {
135
+ contentLines.push(applyBackgroundToLine(lineWithMargins, width, bgFn));
136
+ } else {
137
+ // No background - just pad to width
138
+ const visibleLen = visibleWidth(lineWithMargins);
139
+ const paddingNeeded = Math.max(0, width - visibleLen);
140
+ contentLines.push(lineWithMargins + " ".repeat(paddingNeeded));
141
+ }
142
+ }
143
+
144
+ // Add top/bottom padding (empty lines)
145
+ const emptyLine = " ".repeat(width);
146
+ const emptyLines: string[] = [];
147
+ for (let i = 0; i < this.paddingY; i++) {
148
+ const line = bgFn ? applyBackgroundToLine(emptyLine, width, bgFn) : emptyLine;
149
+ emptyLines.push(line);
150
+ }
151
+
152
+ // Combine top padding, content, and bottom padding
153
+ const result = [...emptyLines, ...contentLines, ...emptyLines];
154
+
155
+ // Update cache
156
+ this.cachedText = this.text;
157
+ this.cachedWidth = width;
158
+ this.cachedLines = result;
159
+
160
+ return result.length > 0 ? result : [""];
161
+ }
162
+
163
+ /**
164
+ * Apply default text style to a string.
165
+ * This is the base styling applied to all text content.
166
+ * NOTE: Background color is NOT applied here - it's applied at the padding stage
167
+ * to ensure it extends to the full line width.
168
+ */
169
+ private applyDefaultStyle(text: string): string {
170
+ if (!this.defaultTextStyle) {
171
+ return text;
172
+ }
173
+
174
+ let styled = text;
175
+
176
+ // Apply foreground color (NOT background - that's applied at padding stage)
177
+ if (this.defaultTextStyle.color) {
178
+ styled = this.defaultTextStyle.color(styled);
179
+ }
180
+
181
+ // Apply text decorations using this.theme
182
+ if (this.defaultTextStyle.bold) {
183
+ styled = this.theme.bold(styled);
184
+ }
185
+ if (this.defaultTextStyle.italic) {
186
+ styled = this.theme.italic(styled);
187
+ }
188
+ if (this.defaultTextStyle.strikethrough) {
189
+ styled = this.theme.strikethrough(styled);
190
+ }
191
+ if (this.defaultTextStyle.underline) {
192
+ styled = this.theme.underline(styled);
193
+ }
194
+
195
+ return styled;
196
+ }
197
+
198
+ private getDefaultStylePrefix(): string {
199
+ if (!this.defaultTextStyle) {
200
+ return "";
201
+ }
202
+
203
+ if (this.defaultStylePrefix !== undefined) {
204
+ return this.defaultStylePrefix;
205
+ }
206
+
207
+ const sentinel = "\u0000";
208
+ let styled = sentinel;
209
+
210
+ if (this.defaultTextStyle.color) {
211
+ styled = this.defaultTextStyle.color(styled);
212
+ }
213
+
214
+ if (this.defaultTextStyle.bold) {
215
+ styled = this.theme.bold(styled);
216
+ }
217
+ if (this.defaultTextStyle.italic) {
218
+ styled = this.theme.italic(styled);
219
+ }
220
+ if (this.defaultTextStyle.strikethrough) {
221
+ styled = this.theme.strikethrough(styled);
222
+ }
223
+ if (this.defaultTextStyle.underline) {
224
+ styled = this.theme.underline(styled);
225
+ }
226
+
227
+ const sentinelIndex = styled.indexOf(sentinel);
228
+ this.defaultStylePrefix = sentinelIndex >= 0 ? styled.slice(0, sentinelIndex) : "";
229
+ return this.defaultStylePrefix;
230
+ }
231
+
232
+ private renderToken(token: Token, width: number, nextTokenType?: string): string[] {
233
+ const lines: string[] = [];
234
+
235
+ switch (token.type) {
236
+ case "heading": {
237
+ const headingLevel = token.depth;
238
+ const headingPrefix = `${"#".repeat(headingLevel)} `;
239
+ const headingText = this.renderInlineTokens(token.tokens || []);
240
+ let styledHeading: string;
241
+ if (headingLevel === 1) {
242
+ styledHeading = this.theme.heading(this.theme.bold(this.theme.underline(headingText)));
243
+ } else if (headingLevel === 2) {
244
+ styledHeading = this.theme.heading(this.theme.bold(headingText));
245
+ } else {
246
+ styledHeading = this.theme.heading(this.theme.bold(headingPrefix + headingText));
247
+ }
248
+ lines.push(styledHeading);
249
+ if (nextTokenType !== "space") {
250
+ lines.push(""); // Add spacing after headings (unless space token follows)
251
+ }
252
+ break;
253
+ }
254
+
255
+ case "paragraph": {
256
+ const paragraphText = this.renderInlineTokens(token.tokens || []);
257
+ lines.push(paragraphText);
258
+ // Don't add spacing if next token is space or list
259
+ if (nextTokenType && nextTokenType !== "list" && nextTokenType !== "space") {
260
+ lines.push("");
261
+ }
262
+ break;
263
+ }
264
+
265
+ case "code": {
266
+ lines.push(this.theme.codeBlockBorder(`\`\`\`${token.lang || ""}`));
267
+ if (this.theme.highlightCode) {
268
+ const highlightedLines = this.theme.highlightCode(token.text, token.lang);
269
+ for (const hlLine of highlightedLines) {
270
+ lines.push(` ${hlLine}`);
271
+ }
272
+ } else {
273
+ // Split code by newlines and style each line
274
+ const codeLines = token.text.split("\n");
275
+ for (const codeLine of codeLines) {
276
+ lines.push(` ${this.theme.codeBlock(codeLine)}`);
277
+ }
278
+ }
279
+ lines.push(this.theme.codeBlockBorder("```"));
280
+ if (nextTokenType !== "space") {
281
+ lines.push(""); // Add spacing after code blocks (unless space token follows)
282
+ }
283
+ break;
284
+ }
285
+
286
+ case "list": {
287
+ const listLines = this.renderList(token as any, 0);
288
+ lines.push(...listLines);
289
+ // Don't add spacing after lists if a space token follows
290
+ // (the space token will handle it)
291
+ break;
292
+ }
293
+
294
+ case "table": {
295
+ const tableLines = this.renderTable(token as any, width);
296
+ lines.push(...tableLines);
297
+ break;
298
+ }
299
+
300
+ case "blockquote": {
301
+ const quoteText = this.renderInlineTokens(token.tokens || []);
302
+ const quoteLines = quoteText.split("\n");
303
+ for (const quoteLine of quoteLines) {
304
+ lines.push(this.theme.quoteBorder("│ ") + this.theme.quote(this.theme.italic(quoteLine)));
305
+ }
306
+ if (nextTokenType !== "space") {
307
+ lines.push(""); // Add spacing after blockquotes (unless space token follows)
308
+ }
309
+ break;
310
+ }
311
+
312
+ case "hr":
313
+ lines.push(this.theme.hr("─".repeat(Math.min(width, 80))));
314
+ if (nextTokenType !== "space") {
315
+ lines.push(""); // Add spacing after horizontal rules (unless space token follows)
316
+ }
317
+ break;
318
+
319
+ case "html":
320
+ // Render HTML as plain text (escaped for terminal)
321
+ if ("raw" in token && typeof token.raw === "string") {
322
+ lines.push(this.applyDefaultStyle(token.raw.trim()));
323
+ }
324
+ break;
325
+
326
+ case "space":
327
+ // Space tokens represent blank lines in markdown
328
+ lines.push("");
329
+ break;
330
+
331
+ default:
332
+ // Handle any other token types as plain text
333
+ if ("text" in token && typeof token.text === "string") {
334
+ lines.push(token.text);
335
+ }
336
+ }
337
+
338
+ return lines;
339
+ }
340
+
341
+ private renderInlineTokens(tokens: Token[]): string {
342
+ let result = "";
343
+
344
+ for (const token of tokens) {
345
+ switch (token.type) {
346
+ case "text":
347
+ // Text tokens in list items can have nested tokens for inline formatting
348
+ if (token.tokens && token.tokens.length > 0) {
349
+ result += this.renderInlineTokens(token.tokens);
350
+ } else {
351
+ // Apply default style to plain text
352
+ result += this.applyDefaultStyle(token.text);
353
+ }
354
+ break;
355
+
356
+ case "strong": {
357
+ // Apply bold, then reapply default style after
358
+ const boldContent = this.renderInlineTokens(token.tokens || []);
359
+ result += this.theme.bold(boldContent) + this.getDefaultStylePrefix();
360
+ break;
361
+ }
362
+
363
+ case "em": {
364
+ // Apply italic, then reapply default style after
365
+ const italicContent = this.renderInlineTokens(token.tokens || []);
366
+ result += this.theme.italic(italicContent) + this.getDefaultStylePrefix();
367
+ break;
368
+ }
369
+
370
+ case "codespan":
371
+ // Apply code styling without backticks
372
+ result += this.theme.code(token.text) + this.getDefaultStylePrefix();
373
+ break;
374
+
375
+ case "link": {
376
+ const linkText = this.renderInlineTokens(token.tokens || []);
377
+ // If link text matches href, only show the link once
378
+ // Compare raw text (token.text) not styled text (linkText) since linkText has ANSI codes
379
+ if (token.text === token.href) {
380
+ result += this.theme.link(this.theme.underline(linkText)) + this.getDefaultStylePrefix();
381
+ } else {
382
+ result +=
383
+ this.theme.link(this.theme.underline(linkText)) +
384
+ this.theme.linkUrl(` (${token.href})`) +
385
+ this.getDefaultStylePrefix();
386
+ }
387
+ break;
388
+ }
389
+
390
+ case "br":
391
+ result += "\n";
392
+ break;
393
+
394
+ case "del": {
395
+ const delContent = this.renderInlineTokens(token.tokens || []);
396
+ result += this.theme.strikethrough(delContent) + this.getDefaultStylePrefix();
397
+ break;
398
+ }
399
+
400
+ case "html":
401
+ // Render inline HTML as plain text
402
+ if ("raw" in token && typeof token.raw === "string") {
403
+ result += this.applyDefaultStyle(token.raw);
404
+ }
405
+ break;
406
+
407
+ default:
408
+ // Handle any other inline token types as plain text
409
+ if ("text" in token && typeof token.text === "string") {
410
+ result += this.applyDefaultStyle(token.text);
411
+ }
412
+ }
413
+ }
414
+
415
+ return result;
416
+ }
417
+
418
+ /**
419
+ * Render a list with proper nesting support
420
+ */
421
+ private renderList(token: Token & { items: any[]; ordered: boolean }, depth: number): string[] {
422
+ const lines: string[] = [];
423
+ const indent = " ".repeat(depth);
424
+
425
+ for (let i = 0; i < token.items.length; i++) {
426
+ const item = token.items[i];
427
+ const bullet = token.ordered ? `${i + 1}. ` : "- ";
428
+
429
+ // Process item tokens to handle nested lists
430
+ const itemLines = this.renderListItem(item.tokens || [], depth);
431
+
432
+ if (itemLines.length > 0) {
433
+ // First line - check if it's a nested list
434
+ // A nested list will start with indent (spaces) followed by cyan bullet
435
+ const firstLine = itemLines[0];
436
+ const isNestedList = /^\s+\x1b\[36m[-\d]/.test(firstLine); // starts with spaces + cyan + bullet char
437
+
438
+ if (isNestedList) {
439
+ // This is a nested list, just add it as-is (already has full indent)
440
+ lines.push(firstLine);
441
+ } else {
442
+ // Regular text content - add indent and bullet
443
+ lines.push(indent + this.theme.listBullet(bullet) + firstLine);
444
+ }
445
+
446
+ // Rest of the lines
447
+ for (let j = 1; j < itemLines.length; j++) {
448
+ const line = itemLines[j];
449
+ const isNestedListLine = /^\s+\x1b\[36m[-\d]/.test(line); // starts with spaces + cyan + bullet char
450
+
451
+ if (isNestedListLine) {
452
+ // Nested list line - already has full indent
453
+ lines.push(line);
454
+ } else {
455
+ // Regular content - add parent indent + 2 spaces for continuation
456
+ lines.push(`${indent} ${line}`);
457
+ }
458
+ }
459
+ } else {
460
+ lines.push(indent + this.theme.listBullet(bullet));
461
+ }
462
+ }
463
+
464
+ return lines;
465
+ }
466
+
467
+ /**
468
+ * Render list item tokens, handling nested lists
469
+ * Returns lines WITHOUT the parent indent (renderList will add it)
470
+ */
471
+ private renderListItem(tokens: Token[], parentDepth: number): string[] {
472
+ const lines: string[] = [];
473
+
474
+ for (const token of tokens) {
475
+ if (token.type === "list") {
476
+ // Nested list - render with one additional indent level
477
+ // These lines will have their own indent, so we just add them as-is
478
+ const nestedLines = this.renderList(token as any, parentDepth + 1);
479
+ lines.push(...nestedLines);
480
+ } else if (token.type === "text") {
481
+ // Text content (may have inline tokens)
482
+ const text =
483
+ token.tokens && token.tokens.length > 0 ? this.renderInlineTokens(token.tokens) : token.text || "";
484
+ lines.push(text);
485
+ } else if (token.type === "paragraph") {
486
+ // Paragraph in list item
487
+ const text = this.renderInlineTokens(token.tokens || []);
488
+ lines.push(text);
489
+ } else if (token.type === "code") {
490
+ // Code block in list item
491
+ lines.push(this.theme.codeBlockBorder(`\`\`\`${token.lang || ""}`));
492
+ if (this.theme.highlightCode) {
493
+ const highlightedLines = this.theme.highlightCode(token.text, token.lang);
494
+ for (const hlLine of highlightedLines) {
495
+ lines.push(` ${hlLine}`);
496
+ }
497
+ } else {
498
+ const codeLines = token.text.split("\n");
499
+ for (const codeLine of codeLines) {
500
+ lines.push(` ${this.theme.codeBlock(codeLine)}`);
501
+ }
502
+ }
503
+ lines.push(this.theme.codeBlockBorder("```"));
504
+ } else {
505
+ // Other token types - try to render as inline
506
+ const text = this.renderInlineTokens([token]);
507
+ if (text) {
508
+ lines.push(text);
509
+ }
510
+ }
511
+ }
512
+
513
+ return lines;
514
+ }
515
+
516
+ /**
517
+ * Wrap a table cell to fit into a column.
518
+ *
519
+ * Delegates to wrapTextWithAnsi() so ANSI codes + long tokens are handled
520
+ * consistently with the rest of the renderer.
521
+ */
522
+ private wrapCellText(text: string, maxWidth: number): string[] {
523
+ return wrapTextWithAnsi(text, Math.max(1, maxWidth));
524
+ }
525
+
526
+ /**
527
+ * Render a table with width-aware cell wrapping.
528
+ * Cells that don't fit are wrapped to multiple lines.
529
+ */
530
+ private renderTable(
531
+ token: Token & { header: any[]; rows: any[][]; raw?: string },
532
+ availableWidth: number,
533
+ ): string[] {
534
+ const lines: string[] = [];
535
+ const numCols = token.header.length;
536
+
537
+ if (numCols === 0) {
538
+ return lines;
539
+ }
540
+
541
+ // Calculate border overhead: "│ " + (n-1) * " │ " + " │"
542
+ // = 2 + (n-1) * 3 + 2 = 3n + 1
543
+ const borderOverhead = 3 * numCols + 1;
544
+
545
+ // Minimum width for a bordered table with at least 1 char per column.
546
+ const minTableWidth = borderOverhead + numCols;
547
+ if (availableWidth < minTableWidth) {
548
+ // Too narrow to render a stable table. Fall back to raw markdown.
549
+ const fallbackLines = token.raw ? wrapTextWithAnsi(token.raw, availableWidth) : [];
550
+ fallbackLines.push("");
551
+ return fallbackLines;
552
+ }
553
+
554
+ // Calculate natural column widths (what each column needs without constraints)
555
+ const naturalWidths: number[] = [];
556
+ for (let i = 0; i < numCols; i++) {
557
+ const headerText = this.renderInlineTokens(token.header[i].tokens || []);
558
+ naturalWidths[i] = visibleWidth(headerText);
559
+ }
560
+ for (const row of token.rows) {
561
+ for (let i = 0; i < row.length; i++) {
562
+ const cellText = this.renderInlineTokens(row[i].tokens || []);
563
+ naturalWidths[i] = Math.max(naturalWidths[i] || 0, visibleWidth(cellText));
564
+ }
565
+ }
566
+
567
+ // Calculate column widths that fit within available width
568
+ const totalNaturalWidth = naturalWidths.reduce((a, b) => a + b, 0) + borderOverhead;
569
+ let columnWidths: number[];
570
+
571
+ if (totalNaturalWidth <= availableWidth) {
572
+ // Everything fits naturally
573
+ columnWidths = naturalWidths;
574
+ } else {
575
+ // Need to shrink columns to fit
576
+ const availableForCells = availableWidth - borderOverhead;
577
+ if (availableForCells <= numCols) {
578
+ // Extremely narrow - give each column at least 1 char
579
+ columnWidths = naturalWidths.map(() => Math.max(1, Math.floor(availableForCells / numCols)));
580
+ } else {
581
+ // Distribute space proportionally based on natural widths
582
+ const totalNatural = naturalWidths.reduce((a, b) => a + b, 0);
583
+ columnWidths = naturalWidths.map((w) => {
584
+ const proportion = w / totalNatural;
585
+ return Math.max(1, Math.floor(proportion * availableForCells));
586
+ });
587
+
588
+ // Adjust for rounding errors - distribute remaining space
589
+ const allocated = columnWidths.reduce((a, b) => a + b, 0);
590
+ let remaining = availableForCells - allocated;
591
+ for (let i = 0; remaining > 0 && i < numCols; i++) {
592
+ columnWidths[i]++;
593
+ remaining--;
594
+ }
595
+ }
596
+ }
597
+
598
+ // Render top border
599
+ const topBorderCells = columnWidths.map((w) => "─".repeat(w));
600
+ lines.push(`┌─${topBorderCells.join("─┬─")}─┐`);
601
+
602
+ // Render header with wrapping
603
+ const headerCellLines: string[][] = token.header.map((cell, i) => {
604
+ const text = this.renderInlineTokens(cell.tokens || []);
605
+ return this.wrapCellText(text, columnWidths[i]);
606
+ });
607
+ const headerLineCount = Math.max(...headerCellLines.map((c) => c.length));
608
+
609
+ for (let lineIdx = 0; lineIdx < headerLineCount; lineIdx++) {
610
+ const rowParts = headerCellLines.map((cellLines, colIdx) => {
611
+ const text = cellLines[lineIdx] || "";
612
+ const padded = text + " ".repeat(Math.max(0, columnWidths[colIdx] - visibleWidth(text)));
613
+ return this.theme.bold(padded);
614
+ });
615
+ lines.push(`│ ${rowParts.join(" │ ")} │`);
616
+ }
617
+
618
+ // Render separator
619
+ const separatorCells = columnWidths.map((w) => "─".repeat(w));
620
+ lines.push(`├─${separatorCells.join("─┼─")}─┤`);
621
+
622
+ // Render rows with wrapping
623
+ for (const row of token.rows) {
624
+ const rowCellLines: string[][] = row.map((cell, i) => {
625
+ const text = this.renderInlineTokens(cell.tokens || []);
626
+ return this.wrapCellText(text, columnWidths[i]);
627
+ });
628
+ const rowLineCount = Math.max(...rowCellLines.map((c) => c.length));
629
+
630
+ for (let lineIdx = 0; lineIdx < rowLineCount; lineIdx++) {
631
+ const rowParts = rowCellLines.map((cellLines, colIdx) => {
632
+ const text = cellLines[lineIdx] || "";
633
+ return text + " ".repeat(Math.max(0, columnWidths[colIdx] - visibleWidth(text)));
634
+ });
635
+ lines.push(`│ ${rowParts.join(" │ ")} │`);
636
+ }
637
+ }
638
+
639
+ // Render bottom border
640
+ const bottomBorderCells = columnWidths.map((w) => "─".repeat(w));
641
+ lines.push(`└─${bottomBorderCells.join("─┴─")}─┘`);
642
+
643
+ lines.push(""); // Add spacing after table
644
+ return lines;
645
+ }
646
+ }