@caupulican/pi-tui 0.78.2

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.
Files changed (114) hide show
  1. package/README.md +779 -0
  2. package/dist/autocomplete.d.ts +54 -0
  3. package/dist/autocomplete.d.ts.map +1 -0
  4. package/dist/autocomplete.js +632 -0
  5. package/dist/autocomplete.js.map +1 -0
  6. package/dist/components/box.d.ts +22 -0
  7. package/dist/components/box.d.ts.map +1 -0
  8. package/dist/components/box.js +104 -0
  9. package/dist/components/box.js.map +1 -0
  10. package/dist/components/cancellable-loader.d.ts +22 -0
  11. package/dist/components/cancellable-loader.d.ts.map +1 -0
  12. package/dist/components/cancellable-loader.js +35 -0
  13. package/dist/components/cancellable-loader.js.map +1 -0
  14. package/dist/components/editor.d.ts +249 -0
  15. package/dist/components/editor.d.ts.map +1 -0
  16. package/dist/components/editor.js +1857 -0
  17. package/dist/components/editor.js.map +1 -0
  18. package/dist/components/image.d.ts +28 -0
  19. package/dist/components/image.d.ts.map +1 -0
  20. package/dist/components/image.js +89 -0
  21. package/dist/components/image.js.map +1 -0
  22. package/dist/components/input.d.ts +37 -0
  23. package/dist/components/input.d.ts.map +1 -0
  24. package/dist/components/input.js +378 -0
  25. package/dist/components/input.js.map +1 -0
  26. package/dist/components/loader.d.ts +31 -0
  27. package/dist/components/loader.d.ts.map +1 -0
  28. package/dist/components/loader.js +69 -0
  29. package/dist/components/loader.js.map +1 -0
  30. package/dist/components/markdown.d.ts +96 -0
  31. package/dist/components/markdown.d.ts.map +1 -0
  32. package/dist/components/markdown.js +644 -0
  33. package/dist/components/markdown.js.map +1 -0
  34. package/dist/components/select-list.d.ts +50 -0
  35. package/dist/components/select-list.d.ts.map +1 -0
  36. package/dist/components/select-list.js +159 -0
  37. package/dist/components/select-list.js.map +1 -0
  38. package/dist/components/settings-list.d.ts +50 -0
  39. package/dist/components/settings-list.d.ts.map +1 -0
  40. package/dist/components/settings-list.js +185 -0
  41. package/dist/components/settings-list.js.map +1 -0
  42. package/dist/components/spacer.d.ts +12 -0
  43. package/dist/components/spacer.d.ts.map +1 -0
  44. package/dist/components/spacer.js +23 -0
  45. package/dist/components/spacer.js.map +1 -0
  46. package/dist/components/text.d.ts +19 -0
  47. package/dist/components/text.d.ts.map +1 -0
  48. package/dist/components/text.js +89 -0
  49. package/dist/components/text.js.map +1 -0
  50. package/dist/components/truncated-text.d.ts +13 -0
  51. package/dist/components/truncated-text.d.ts.map +1 -0
  52. package/dist/components/truncated-text.js +51 -0
  53. package/dist/components/truncated-text.js.map +1 -0
  54. package/dist/editor-component.d.ts +39 -0
  55. package/dist/editor-component.d.ts.map +1 -0
  56. package/dist/editor-component.js +2 -0
  57. package/dist/editor-component.js.map +1 -0
  58. package/dist/fuzzy.d.ts +16 -0
  59. package/dist/fuzzy.d.ts.map +1 -0
  60. package/dist/fuzzy.js +110 -0
  61. package/dist/fuzzy.js.map +1 -0
  62. package/dist/index.d.ts +23 -0
  63. package/dist/index.d.ts.map +1 -0
  64. package/dist/index.js +32 -0
  65. package/dist/index.js.map +1 -0
  66. package/dist/keybindings.d.ts +193 -0
  67. package/dist/keybindings.d.ts.map +1 -0
  68. package/dist/keybindings.js +174 -0
  69. package/dist/keybindings.js.map +1 -0
  70. package/dist/keys.d.ts +184 -0
  71. package/dist/keys.d.ts.map +1 -0
  72. package/dist/keys.js +1173 -0
  73. package/dist/keys.js.map +1 -0
  74. package/dist/kill-ring.d.ts +28 -0
  75. package/dist/kill-ring.d.ts.map +1 -0
  76. package/dist/kill-ring.js +44 -0
  77. package/dist/kill-ring.js.map +1 -0
  78. package/dist/native-modifiers.d.ts +3 -0
  79. package/dist/native-modifiers.d.ts.map +1 -0
  80. package/dist/native-modifiers.js +53 -0
  81. package/dist/native-modifiers.js.map +1 -0
  82. package/dist/stdin-buffer.d.ts +50 -0
  83. package/dist/stdin-buffer.d.ts.map +1 -0
  84. package/dist/stdin-buffer.js +361 -0
  85. package/dist/stdin-buffer.js.map +1 -0
  86. package/dist/terminal-image.d.ts +90 -0
  87. package/dist/terminal-image.d.ts.map +1 -0
  88. package/dist/terminal-image.js +366 -0
  89. package/dist/terminal-image.js.map +1 -0
  90. package/dist/terminal.d.ts +113 -0
  91. package/dist/terminal.d.ts.map +1 -0
  92. package/dist/terminal.js +472 -0
  93. package/dist/terminal.js.map +1 -0
  94. package/dist/tui.d.ts +227 -0
  95. package/dist/tui.d.ts.map +1 -0
  96. package/dist/tui.js +1077 -0
  97. package/dist/tui.js.map +1 -0
  98. package/dist/undo-stack.d.ts +17 -0
  99. package/dist/undo-stack.d.ts.map +1 -0
  100. package/dist/undo-stack.js +25 -0
  101. package/dist/undo-stack.js.map +1 -0
  102. package/dist/utils.d.ts +84 -0
  103. package/dist/utils.d.ts.map +1 -0
  104. package/dist/utils.js +1029 -0
  105. package/dist/utils.js.map +1 -0
  106. package/dist/word-navigation.d.ts +25 -0
  107. package/dist/word-navigation.d.ts.map +1 -0
  108. package/dist/word-navigation.js +96 -0
  109. package/dist/word-navigation.js.map +1 -0
  110. package/native/darwin/prebuilds/darwin-arm64/darwin-modifiers.node +0 -0
  111. package/native/darwin/prebuilds/darwin-x64/darwin-modifiers.node +0 -0
  112. package/native/win32/prebuilds/win32-arm64/win32-console-mode.node +0 -0
  113. package/native/win32/prebuilds/win32-x64/win32-console-mode.node +0 -0
  114. package/package.json +47 -0
@@ -0,0 +1,644 @@
1
+ import { Marked, Tokenizer } from "marked";
2
+ import { getCapabilities, hyperlink, isImageLine } from "../terminal-image.js";
3
+ import { applyBackgroundToLine, visibleWidth, wrapTextWithAnsi } from "../utils.js";
4
+ const STRICT_STRIKETHROUGH_REGEX = /^(~~)(?=[^\s~])((?:\\.|[^\\])*?(?:\\.|[^\s~\\]))\1(?=[^~]|$)/;
5
+ class StrictStrikethroughTokenizer extends Tokenizer {
6
+ del(src) {
7
+ const match = STRICT_STRIKETHROUGH_REGEX.exec(src);
8
+ if (!match) {
9
+ return undefined;
10
+ }
11
+ const text = match[2];
12
+ return {
13
+ type: "del",
14
+ raw: match[0],
15
+ text,
16
+ tokens: this.lexer.inlineTokens(text),
17
+ };
18
+ }
19
+ }
20
+ const markdownParser = new Marked();
21
+ markdownParser.setOptions({
22
+ tokenizer: new StrictStrikethroughTokenizer(),
23
+ });
24
+ export class Markdown {
25
+ text;
26
+ paddingX; // Left/right padding
27
+ paddingY; // Top/bottom padding
28
+ defaultTextStyle;
29
+ theme;
30
+ options;
31
+ defaultStylePrefix;
32
+ // Cache for rendered output
33
+ cachedText;
34
+ cachedWidth;
35
+ cachedLines;
36
+ constructor(text, paddingX, paddingY, theme, defaultTextStyle, options) {
37
+ this.text = text;
38
+ this.paddingX = paddingX;
39
+ this.paddingY = paddingY;
40
+ this.theme = theme;
41
+ this.defaultTextStyle = defaultTextStyle;
42
+ this.options = options ? { ...options } : {};
43
+ }
44
+ setText(text) {
45
+ this.text = text;
46
+ this.invalidate();
47
+ }
48
+ invalidate() {
49
+ this.cachedText = undefined;
50
+ this.cachedWidth = undefined;
51
+ this.cachedLines = undefined;
52
+ }
53
+ render(width) {
54
+ // Check cache
55
+ if (this.cachedLines && this.cachedText === this.text && this.cachedWidth === width) {
56
+ return this.cachedLines;
57
+ }
58
+ // Calculate available width for content (subtract horizontal padding)
59
+ const contentWidth = Math.max(1, width - this.paddingX * 2);
60
+ // Don't render anything if there's no actual text
61
+ if (!this.text || this.text.trim() === "") {
62
+ const result = [];
63
+ // Update cache
64
+ this.cachedText = this.text;
65
+ this.cachedWidth = width;
66
+ this.cachedLines = result;
67
+ return result;
68
+ }
69
+ // Replace tabs with 3 spaces for consistent rendering
70
+ const normalizedText = this.text.replace(/\t/g, " ");
71
+ // Parse markdown to HTML-like tokens
72
+ const tokens = markdownParser.lexer(normalizedText);
73
+ // Convert tokens to styled terminal output
74
+ const renderedLines = [];
75
+ for (let i = 0; i < tokens.length; i++) {
76
+ const token = tokens[i];
77
+ const nextToken = tokens[i + 1];
78
+ const tokenLines = this.renderToken(token, contentWidth, nextToken?.type);
79
+ for (const tokenLine of tokenLines) {
80
+ renderedLines.push(tokenLine);
81
+ }
82
+ }
83
+ // Wrap lines (NO padding, NO background yet)
84
+ const wrappedLines = [];
85
+ for (const line of renderedLines) {
86
+ if (isImageLine(line)) {
87
+ wrappedLines.push(line);
88
+ }
89
+ else {
90
+ for (const wrappedLine of wrapTextWithAnsi(line, contentWidth)) {
91
+ wrappedLines.push(wrappedLine);
92
+ }
93
+ }
94
+ }
95
+ // Add margins and background to each wrapped line
96
+ const leftMargin = " ".repeat(this.paddingX);
97
+ const rightMargin = " ".repeat(this.paddingX);
98
+ const bgFn = this.defaultTextStyle?.bgColor;
99
+ const contentLines = [];
100
+ for (const line of wrappedLines) {
101
+ if (isImageLine(line)) {
102
+ contentLines.push(line);
103
+ continue;
104
+ }
105
+ const lineWithMargins = leftMargin + line + rightMargin;
106
+ if (bgFn) {
107
+ contentLines.push(applyBackgroundToLine(lineWithMargins, width, bgFn));
108
+ }
109
+ else {
110
+ // No background - just pad to width
111
+ const visibleLen = visibleWidth(lineWithMargins);
112
+ const paddingNeeded = Math.max(0, width - visibleLen);
113
+ contentLines.push(lineWithMargins + " ".repeat(paddingNeeded));
114
+ }
115
+ }
116
+ // Add top/bottom padding (empty lines)
117
+ const emptyLine = " ".repeat(width);
118
+ const emptyLines = [];
119
+ for (let i = 0; i < this.paddingY; i++) {
120
+ const line = bgFn ? applyBackgroundToLine(emptyLine, width, bgFn) : emptyLine;
121
+ emptyLines.push(line);
122
+ }
123
+ // Combine top padding, content, and bottom padding
124
+ const result = emptyLines.concat(contentLines, emptyLines);
125
+ // Update cache
126
+ this.cachedText = this.text;
127
+ this.cachedWidth = width;
128
+ this.cachedLines = result;
129
+ return result.length > 0 ? result : [""];
130
+ }
131
+ /**
132
+ * Apply default text style to a string.
133
+ * This is the base styling applied to all text content.
134
+ * NOTE: Background color is NOT applied here - it's applied at the padding stage
135
+ * to ensure it extends to the full line width.
136
+ */
137
+ applyDefaultStyle(text) {
138
+ if (!this.defaultTextStyle) {
139
+ return text;
140
+ }
141
+ let styled = text;
142
+ // Apply foreground color (NOT background - that's applied at padding stage)
143
+ if (this.defaultTextStyle.color) {
144
+ styled = this.defaultTextStyle.color(styled);
145
+ }
146
+ // Apply text decorations using this.theme
147
+ if (this.defaultTextStyle.bold) {
148
+ styled = this.theme.bold(styled);
149
+ }
150
+ if (this.defaultTextStyle.italic) {
151
+ styled = this.theme.italic(styled);
152
+ }
153
+ if (this.defaultTextStyle.strikethrough) {
154
+ styled = this.theme.strikethrough(styled);
155
+ }
156
+ if (this.defaultTextStyle.underline) {
157
+ styled = this.theme.underline(styled);
158
+ }
159
+ return styled;
160
+ }
161
+ getDefaultStylePrefix() {
162
+ if (!this.defaultTextStyle) {
163
+ return "";
164
+ }
165
+ if (this.defaultStylePrefix !== undefined) {
166
+ return this.defaultStylePrefix;
167
+ }
168
+ const sentinel = "\u0000";
169
+ let styled = sentinel;
170
+ if (this.defaultTextStyle.color) {
171
+ styled = this.defaultTextStyle.color(styled);
172
+ }
173
+ if (this.defaultTextStyle.bold) {
174
+ styled = this.theme.bold(styled);
175
+ }
176
+ if (this.defaultTextStyle.italic) {
177
+ styled = this.theme.italic(styled);
178
+ }
179
+ if (this.defaultTextStyle.strikethrough) {
180
+ styled = this.theme.strikethrough(styled);
181
+ }
182
+ if (this.defaultTextStyle.underline) {
183
+ styled = this.theme.underline(styled);
184
+ }
185
+ const sentinelIndex = styled.indexOf(sentinel);
186
+ this.defaultStylePrefix = sentinelIndex >= 0 ? styled.slice(0, sentinelIndex) : "";
187
+ return this.defaultStylePrefix;
188
+ }
189
+ getStylePrefix(styleFn) {
190
+ const sentinel = "\u0000";
191
+ const styled = styleFn(sentinel);
192
+ const sentinelIndex = styled.indexOf(sentinel);
193
+ return sentinelIndex >= 0 ? styled.slice(0, sentinelIndex) : "";
194
+ }
195
+ getDefaultInlineStyleContext() {
196
+ return {
197
+ applyText: (text) => this.applyDefaultStyle(text),
198
+ stylePrefix: this.getDefaultStylePrefix(),
199
+ };
200
+ }
201
+ renderToken(token, width, nextTokenType, styleContext) {
202
+ const lines = [];
203
+ switch (token.type) {
204
+ case "heading": {
205
+ const headingLevel = token.depth;
206
+ const headingPrefix = `${"#".repeat(headingLevel)} `;
207
+ // Build a heading-specific style context so inline tokens (codespan, bold, etc.)
208
+ // restore heading styling after their own ANSI resets instead of falling back to
209
+ // the default text style.
210
+ let headingStyleFn;
211
+ if (headingLevel === 1) {
212
+ headingStyleFn = (text) => this.theme.heading(this.theme.bold(this.theme.underline(text)));
213
+ }
214
+ else {
215
+ headingStyleFn = (text) => this.theme.heading(this.theme.bold(text));
216
+ }
217
+ const headingStyleContext = {
218
+ applyText: headingStyleFn,
219
+ stylePrefix: this.getStylePrefix(headingStyleFn),
220
+ };
221
+ const headingText = this.renderInlineTokens(token.tokens || [], headingStyleContext);
222
+ const styledHeading = headingLevel >= 3 ? headingStyleFn(headingPrefix) + headingText : headingText;
223
+ lines.push(styledHeading);
224
+ if (nextTokenType && nextTokenType !== "space") {
225
+ lines.push(""); // Add spacing after headings (unless space token follows)
226
+ }
227
+ break;
228
+ }
229
+ case "paragraph": {
230
+ const paragraphText = this.renderInlineTokens(token.tokens || [], styleContext);
231
+ lines.push(paragraphText);
232
+ // Don't add spacing if next token is space or list
233
+ if (nextTokenType && nextTokenType !== "list" && nextTokenType !== "space") {
234
+ lines.push("");
235
+ }
236
+ break;
237
+ }
238
+ case "text":
239
+ lines.push(this.renderInlineTokens([token], styleContext));
240
+ break;
241
+ case "code": {
242
+ const indent = this.theme.codeBlockIndent ?? " ";
243
+ lines.push(this.theme.codeBlockBorder(`\`\`\`${token.lang || ""}`));
244
+ if (this.theme.highlightCode) {
245
+ const highlightedLines = this.theme.highlightCode(token.text, token.lang);
246
+ for (const hlLine of highlightedLines) {
247
+ lines.push(`${indent}${hlLine}`);
248
+ }
249
+ }
250
+ else {
251
+ // Split code by newlines and style each line
252
+ const codeLines = token.text.split("\n");
253
+ for (const codeLine of codeLines) {
254
+ lines.push(`${indent}${this.theme.codeBlock(codeLine)}`);
255
+ }
256
+ }
257
+ lines.push(this.theme.codeBlockBorder("```"));
258
+ if (nextTokenType && nextTokenType !== "space") {
259
+ lines.push(""); // Add spacing after code blocks (unless space token follows)
260
+ }
261
+ break;
262
+ }
263
+ case "list": {
264
+ const listLines = this.renderList(token, 0, width, styleContext);
265
+ lines.push(...listLines);
266
+ // Don't add spacing after lists if a space token follows
267
+ // (the space token will handle it)
268
+ break;
269
+ }
270
+ case "table": {
271
+ const tableLines = this.renderTable(token, width, nextTokenType, styleContext);
272
+ lines.push(...tableLines);
273
+ break;
274
+ }
275
+ case "blockquote": {
276
+ const quoteStyle = (text) => this.theme.quote(this.theme.italic(text));
277
+ const quoteStylePrefix = this.getStylePrefix(quoteStyle);
278
+ const applyQuoteStyle = (line) => {
279
+ if (!quoteStylePrefix) {
280
+ return quoteStyle(line);
281
+ }
282
+ const lineWithReappliedStyle = line.replace(/\x1b\[0m/g, `\x1b[0m${quoteStylePrefix}`);
283
+ return quoteStyle(lineWithReappliedStyle);
284
+ };
285
+ // Calculate available width for quote content (subtract border "│ " = 2 chars)
286
+ const quoteContentWidth = Math.max(1, width - 2);
287
+ // Blockquotes contain block-level tokens (paragraph, list, code, etc.), so render
288
+ // children with renderToken() instead of renderInlineTokens().
289
+ // Default message style should not apply inside blockquotes.
290
+ const quoteInlineStyleContext = {
291
+ applyText: (text) => text,
292
+ stylePrefix: quoteStylePrefix,
293
+ };
294
+ const quoteTokens = token.tokens || [];
295
+ const renderedQuoteLines = [];
296
+ for (let i = 0; i < quoteTokens.length; i++) {
297
+ const quoteToken = quoteTokens[i];
298
+ const nextQuoteToken = quoteTokens[i + 1];
299
+ renderedQuoteLines.push(...this.renderToken(quoteToken, quoteContentWidth, nextQuoteToken?.type, quoteInlineStyleContext));
300
+ }
301
+ // Avoid rendering an extra empty quote line before the outer blockquote spacing.
302
+ while (renderedQuoteLines.length > 0 && renderedQuoteLines[renderedQuoteLines.length - 1] === "") {
303
+ renderedQuoteLines.pop();
304
+ }
305
+ for (const quoteLine of renderedQuoteLines) {
306
+ const styledLine = applyQuoteStyle(quoteLine);
307
+ const wrappedLines = wrapTextWithAnsi(styledLine, quoteContentWidth);
308
+ for (const wrappedLine of wrappedLines) {
309
+ lines.push(this.theme.quoteBorder("│ ") + wrappedLine);
310
+ }
311
+ }
312
+ if (nextTokenType && nextTokenType !== "space") {
313
+ lines.push(""); // Add spacing after blockquotes (unless space token follows)
314
+ }
315
+ break;
316
+ }
317
+ case "hr":
318
+ lines.push(this.theme.hr("─".repeat(Math.min(width, 80))));
319
+ if (nextTokenType && nextTokenType !== "space") {
320
+ lines.push(""); // Add spacing after horizontal rules (unless space token follows)
321
+ }
322
+ break;
323
+ case "html":
324
+ // Render HTML as plain text (escaped for terminal)
325
+ if ("raw" in token && typeof token.raw === "string") {
326
+ lines.push(this.applyDefaultStyle(token.raw.trim()));
327
+ }
328
+ break;
329
+ case "space":
330
+ // Space tokens represent blank lines in markdown
331
+ lines.push("");
332
+ break;
333
+ default:
334
+ // Handle any other token types as plain text
335
+ if ("text" in token && typeof token.text === "string") {
336
+ lines.push(token.text);
337
+ }
338
+ }
339
+ return lines;
340
+ }
341
+ renderInlineTokens(tokens, styleContext) {
342
+ let result = "";
343
+ const resolvedStyleContext = styleContext ?? this.getDefaultInlineStyleContext();
344
+ const { applyText, stylePrefix } = resolvedStyleContext;
345
+ const applyTextWithNewlines = (text) => {
346
+ const segments = text.split("\n");
347
+ return segments.map((segment) => applyText(segment)).join("\n");
348
+ };
349
+ for (const token of tokens) {
350
+ switch (token.type) {
351
+ case "text":
352
+ // Text tokens in list items can have nested tokens for inline formatting
353
+ if (token.tokens && token.tokens.length > 0) {
354
+ result += this.renderInlineTokens(token.tokens, resolvedStyleContext);
355
+ }
356
+ else {
357
+ result += applyTextWithNewlines(token.text);
358
+ }
359
+ break;
360
+ case "paragraph":
361
+ // Paragraph tokens contain nested inline tokens
362
+ result += this.renderInlineTokens(token.tokens || [], resolvedStyleContext);
363
+ break;
364
+ case "strong": {
365
+ const boldContent = this.renderInlineTokens(token.tokens || [], resolvedStyleContext);
366
+ result += this.theme.bold(boldContent) + stylePrefix;
367
+ break;
368
+ }
369
+ case "em": {
370
+ const italicContent = this.renderInlineTokens(token.tokens || [], resolvedStyleContext);
371
+ result += this.theme.italic(italicContent) + stylePrefix;
372
+ break;
373
+ }
374
+ case "codespan":
375
+ result += this.theme.code(token.text) + stylePrefix;
376
+ break;
377
+ case "link": {
378
+ const linkText = this.renderInlineTokens(token.tokens || [], resolvedStyleContext);
379
+ const styledLink = this.theme.link(this.theme.underline(linkText));
380
+ if (getCapabilities().hyperlinks) {
381
+ // OSC 8: render as a clickable hyperlink. The URL is not printed inline,
382
+ // so we always show only the link text regardless of whether it matches href.
383
+ result += hyperlink(styledLink, token.href) + stylePrefix;
384
+ }
385
+ else {
386
+ // Fallback: print URL in parentheses when text differs from href.
387
+ // Compare raw token.text (not styled) against href for the equality check.
388
+ // For mailto: links strip the prefix (autolinked emails use text="foo@bar.com"
389
+ // but href="mailto:foo@bar.com").
390
+ const hrefForComparison = token.href.startsWith("mailto:") ? token.href.slice(7) : token.href;
391
+ if (token.text === token.href || token.text === hrefForComparison) {
392
+ result += styledLink + stylePrefix;
393
+ }
394
+ else {
395
+ result += styledLink + this.theme.linkUrl(` (${token.href})`) + stylePrefix;
396
+ }
397
+ }
398
+ break;
399
+ }
400
+ case "br":
401
+ result += "\n";
402
+ break;
403
+ case "del": {
404
+ const delContent = this.renderInlineTokens(token.tokens || [], resolvedStyleContext);
405
+ result += this.theme.strikethrough(delContent) + stylePrefix;
406
+ break;
407
+ }
408
+ case "html":
409
+ // Render inline HTML as plain text
410
+ if ("raw" in token && typeof token.raw === "string") {
411
+ result += applyTextWithNewlines(token.raw);
412
+ }
413
+ break;
414
+ default:
415
+ // Handle any other inline token types as plain text
416
+ if ("text" in token && typeof token.text === "string") {
417
+ result += applyTextWithNewlines(token.text);
418
+ }
419
+ }
420
+ }
421
+ while (stylePrefix && result.endsWith(stylePrefix)) {
422
+ result = result.slice(0, -stylePrefix.length);
423
+ }
424
+ return result;
425
+ }
426
+ getOrderedListMarker(item) {
427
+ const match = /^(?: {0,3})(\d{1,9}[.)])[ \t]+/.exec(item.raw);
428
+ return match ? `${match[1]} ` : undefined;
429
+ }
430
+ /**
431
+ * Render a list with proper nesting support
432
+ */
433
+ renderList(token, depth, width, styleContext) {
434
+ const lines = [];
435
+ const indent = " ".repeat(depth);
436
+ // Use the list's start property (defaults to 1 for ordered lists)
437
+ const startNumber = typeof token.start === "number" ? token.start : 1;
438
+ for (let i = 0; i < token.items.length; i++) {
439
+ const item = token.items[i];
440
+ const bullet = token.ordered
441
+ ? this.options.preserveOrderedListMarkers
442
+ ? (this.getOrderedListMarker(item) ?? `${startNumber + i}. `)
443
+ : `${startNumber + i}. `
444
+ : "- ";
445
+ const taskMarker = item.task ? `[${item.checked ? "x" : " "}] ` : "";
446
+ const marker = bullet + taskMarker;
447
+ const firstPrefix = indent + this.theme.listBullet(marker);
448
+ const continuationPrefix = indent + " ".repeat(visibleWidth(marker));
449
+ const itemWidth = Math.max(1, width - visibleWidth(firstPrefix));
450
+ let renderedAnyLine = false;
451
+ for (const itemToken of item.tokens) {
452
+ if (itemToken.type === "list") {
453
+ lines.push(...this.renderList(itemToken, depth + 1, width, styleContext));
454
+ renderedAnyLine = true;
455
+ continue;
456
+ }
457
+ const itemLines = this.renderToken(itemToken, itemWidth, undefined, styleContext);
458
+ for (const line of itemLines) {
459
+ for (const wrappedLine of wrapTextWithAnsi(line, itemWidth)) {
460
+ const linePrefix = renderedAnyLine ? continuationPrefix : firstPrefix;
461
+ lines.push(linePrefix + wrappedLine);
462
+ renderedAnyLine = true;
463
+ }
464
+ }
465
+ }
466
+ if (!renderedAnyLine) {
467
+ lines.push(firstPrefix);
468
+ }
469
+ }
470
+ return lines;
471
+ }
472
+ /**
473
+ * Get the visible width of the longest word in a string.
474
+ */
475
+ getLongestWordWidth(text, maxWidth) {
476
+ const words = text.split(/\s+/).filter((word) => word.length > 0);
477
+ let longest = 0;
478
+ for (const word of words) {
479
+ longest = Math.max(longest, visibleWidth(word));
480
+ }
481
+ if (maxWidth === undefined) {
482
+ return longest;
483
+ }
484
+ return Math.min(longest, maxWidth);
485
+ }
486
+ /**
487
+ * Wrap a table cell to fit into a column.
488
+ *
489
+ * Delegates to wrapTextWithAnsi() so ANSI codes + long tokens are handled
490
+ * consistently with the rest of the renderer.
491
+ */
492
+ wrapCellText(text, maxWidth) {
493
+ return wrapTextWithAnsi(text, Math.max(1, maxWidth));
494
+ }
495
+ /**
496
+ * Render a table with width-aware cell wrapping.
497
+ * Cells that don't fit are wrapped to multiple lines.
498
+ */
499
+ renderTable(token, availableWidth, nextTokenType, styleContext) {
500
+ const lines = [];
501
+ const numCols = token.header.length;
502
+ if (numCols === 0) {
503
+ return lines;
504
+ }
505
+ // Calculate border overhead: "│ " + (n-1) * " │ " + " │"
506
+ // = 2 + (n-1) * 3 + 2 = 3n + 1
507
+ const borderOverhead = 3 * numCols + 1;
508
+ const availableForCells = availableWidth - borderOverhead;
509
+ if (availableForCells < numCols) {
510
+ // Too narrow to render a stable table. Fall back to raw markdown.
511
+ const fallbackLines = token.raw ? wrapTextWithAnsi(token.raw, availableWidth) : [];
512
+ if (nextTokenType && nextTokenType !== "space") {
513
+ fallbackLines.push("");
514
+ }
515
+ return fallbackLines;
516
+ }
517
+ const maxUnbrokenWordWidth = 30;
518
+ // Calculate natural column widths (what each column needs without constraints)
519
+ const naturalWidths = [];
520
+ const minWordWidths = [];
521
+ for (let i = 0; i < numCols; i++) {
522
+ const headerText = this.renderInlineTokens(token.header[i].tokens || [], styleContext);
523
+ naturalWidths[i] = visibleWidth(headerText);
524
+ minWordWidths[i] = Math.max(1, this.getLongestWordWidth(headerText, maxUnbrokenWordWidth));
525
+ }
526
+ for (const row of token.rows) {
527
+ for (let i = 0; i < row.length; i++) {
528
+ const cellText = this.renderInlineTokens(row[i].tokens || [], styleContext);
529
+ naturalWidths[i] = Math.max(naturalWidths[i] || 0, visibleWidth(cellText));
530
+ minWordWidths[i] = Math.max(minWordWidths[i] || 1, this.getLongestWordWidth(cellText, maxUnbrokenWordWidth));
531
+ }
532
+ }
533
+ let minColumnWidths = minWordWidths;
534
+ let minCellsWidth = minColumnWidths.reduce((a, b) => a + b, 0);
535
+ if (minCellsWidth > availableForCells) {
536
+ minColumnWidths = new Array(numCols).fill(1);
537
+ const remaining = availableForCells - numCols;
538
+ if (remaining > 0) {
539
+ const totalWeight = minWordWidths.reduce((total, width) => total + Math.max(0, width - 1), 0);
540
+ const growth = minWordWidths.map((width) => {
541
+ const weight = Math.max(0, width - 1);
542
+ return totalWeight > 0 ? Math.floor((weight / totalWeight) * remaining) : 0;
543
+ });
544
+ for (let i = 0; i < numCols; i++) {
545
+ minColumnWidths[i] += growth[i] ?? 0;
546
+ }
547
+ const allocated = growth.reduce((total, width) => total + width, 0);
548
+ let leftover = remaining - allocated;
549
+ for (let i = 0; leftover > 0 && i < numCols; i++) {
550
+ minColumnWidths[i]++;
551
+ leftover--;
552
+ }
553
+ }
554
+ minCellsWidth = minColumnWidths.reduce((a, b) => a + b, 0);
555
+ }
556
+ // Calculate column widths that fit within available width
557
+ const totalNaturalWidth = naturalWidths.reduce((a, b) => a + b, 0) + borderOverhead;
558
+ let columnWidths;
559
+ if (totalNaturalWidth <= availableWidth) {
560
+ // Everything fits naturally
561
+ columnWidths = naturalWidths.map((width, index) => Math.max(width, minColumnWidths[index]));
562
+ }
563
+ else {
564
+ // Need to shrink columns to fit
565
+ const totalGrowPotential = naturalWidths.reduce((total, width, index) => {
566
+ return total + Math.max(0, width - minColumnWidths[index]);
567
+ }, 0);
568
+ const extraWidth = Math.max(0, availableForCells - minCellsWidth);
569
+ columnWidths = minColumnWidths.map((minWidth, index) => {
570
+ const naturalWidth = naturalWidths[index];
571
+ const minWidthDelta = Math.max(0, naturalWidth - minWidth);
572
+ let grow = 0;
573
+ if (totalGrowPotential > 0) {
574
+ grow = Math.floor((minWidthDelta / totalGrowPotential) * extraWidth);
575
+ }
576
+ return minWidth + grow;
577
+ });
578
+ // Adjust for rounding errors - distribute remaining space
579
+ const allocated = columnWidths.reduce((a, b) => a + b, 0);
580
+ let remaining = availableForCells - allocated;
581
+ while (remaining > 0) {
582
+ let grew = false;
583
+ for (let i = 0; i < numCols && remaining > 0; i++) {
584
+ if (columnWidths[i] < naturalWidths[i]) {
585
+ columnWidths[i]++;
586
+ remaining--;
587
+ grew = true;
588
+ }
589
+ }
590
+ if (!grew) {
591
+ break;
592
+ }
593
+ }
594
+ }
595
+ // Render top border
596
+ const topBorderCells = columnWidths.map((w) => "─".repeat(w));
597
+ lines.push(`┌─${topBorderCells.join("─┬─")}─┐`);
598
+ // Render header with wrapping
599
+ const headerCellLines = token.header.map((cell, i) => {
600
+ const text = this.renderInlineTokens(cell.tokens || [], styleContext);
601
+ return this.wrapCellText(text, columnWidths[i]);
602
+ });
603
+ const headerLineCount = Math.max(...headerCellLines.map((c) => c.length));
604
+ for (let lineIdx = 0; lineIdx < headerLineCount; lineIdx++) {
605
+ const rowParts = headerCellLines.map((cellLines, colIdx) => {
606
+ const text = cellLines[lineIdx] || "";
607
+ const padded = text + " ".repeat(Math.max(0, columnWidths[colIdx] - visibleWidth(text)));
608
+ return this.theme.bold(padded);
609
+ });
610
+ lines.push(`│ ${rowParts.join(" │ ")} │`);
611
+ }
612
+ // Render separator
613
+ const separatorCells = columnWidths.map((w) => "─".repeat(w));
614
+ const separatorLine = `├─${separatorCells.join("─┼─")}─┤`;
615
+ lines.push(separatorLine);
616
+ // Render rows with wrapping
617
+ for (let rowIndex = 0; rowIndex < token.rows.length; rowIndex++) {
618
+ const row = token.rows[rowIndex];
619
+ const rowCellLines = row.map((cell, i) => {
620
+ const text = this.renderInlineTokens(cell.tokens || [], styleContext);
621
+ return this.wrapCellText(text, columnWidths[i]);
622
+ });
623
+ const rowLineCount = Math.max(...rowCellLines.map((c) => c.length));
624
+ for (let lineIdx = 0; lineIdx < rowLineCount; lineIdx++) {
625
+ const rowParts = rowCellLines.map((cellLines, colIdx) => {
626
+ const text = cellLines[lineIdx] || "";
627
+ return text + " ".repeat(Math.max(0, columnWidths[colIdx] - visibleWidth(text)));
628
+ });
629
+ lines.push(`│ ${rowParts.join(" │ ")} │`);
630
+ }
631
+ if (rowIndex < token.rows.length - 1) {
632
+ lines.push(separatorLine);
633
+ }
634
+ }
635
+ // Render bottom border
636
+ const bottomBorderCells = columnWidths.map((w) => "─".repeat(w));
637
+ lines.push(`└─${bottomBorderCells.join("─┴─")}─┘`);
638
+ if (nextTokenType && nextTokenType !== "space") {
639
+ lines.push(""); // Add spacing after table
640
+ }
641
+ return lines;
642
+ }
643
+ }
644
+ //# sourceMappingURL=markdown.js.map