@dungle-scrubs/tallow 0.8.21 → 0.8.23

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 (217) hide show
  1. package/dist/cli.js +35 -4
  2. package/dist/cli.js.map +1 -1
  3. package/dist/config.d.ts +1 -1
  4. package/dist/config.js +1 -1
  5. package/dist/interactive-mode-patch.d.ts +2 -0
  6. package/dist/interactive-mode-patch.d.ts.map +1 -1
  7. package/dist/interactive-mode-patch.js +82 -0
  8. package/dist/interactive-mode-patch.js.map +1 -1
  9. package/dist/sdk.d.ts +17 -0
  10. package/dist/sdk.d.ts.map +1 -1
  11. package/dist/sdk.js +68 -1
  12. package/dist/sdk.js.map +1 -1
  13. package/dist/workspace-transition-relay.d.ts +40 -7
  14. package/dist/workspace-transition-relay.d.ts.map +1 -1
  15. package/dist/workspace-transition-relay.js +81 -16
  16. package/dist/workspace-transition-relay.js.map +1 -1
  17. package/extensions/__integration__/background-task-widget-ownership.test.ts +216 -0
  18. package/extensions/__integration__/claude-hooks-compat.test.ts +156 -0
  19. package/extensions/__integration__/slash-command-bridge.test.ts +169 -23
  20. package/extensions/_shared/atomic-write.ts +1 -1
  21. package/extensions/_shared/bordered-box.ts +102 -0
  22. package/extensions/_shared/interop-events.ts +5 -0
  23. package/extensions/_shared/pid-registry.ts +1 -1
  24. package/extensions/agent-commands-tool/index.ts +4 -1
  25. package/extensions/background-task-tool/__tests__/lifecycle.test.ts +50 -25
  26. package/extensions/background-task-tool/index.ts +139 -221
  27. package/extensions/bash-tool-enhanced/index.ts +1 -75
  28. package/extensions/cd-tool/index.ts +2 -2
  29. package/extensions/context-fork/spawn.ts +4 -1
  30. package/extensions/health/index.ts +6 -6
  31. package/extensions/hooks/__tests__/claude-compat.test.ts +35 -0
  32. package/extensions/hooks/__tests__/subprocess-hardening.test.ts +73 -0
  33. package/extensions/hooks/index.ts +27 -4
  34. package/extensions/loop/__tests__/loop.test.ts +168 -4
  35. package/extensions/loop/extension.json +6 -5
  36. package/extensions/loop/index.ts +242 -31
  37. package/extensions/plan-mode-tool/__tests__/agent-end-execution.test.ts +373 -0
  38. package/extensions/plan-mode-tool/index.ts +103 -41
  39. package/extensions/prompt-suggestions/__tests__/editor-compatibility.test.ts +42 -0
  40. package/extensions/prompt-suggestions/index.ts +41 -6
  41. package/extensions/slash-command-bridge/__tests__/slash-command-bridge.test.ts +267 -671
  42. package/extensions/slash-command-bridge/extension.json +1 -1
  43. package/extensions/slash-command-bridge/index.ts +230 -116
  44. package/extensions/subagent-tool/index.ts +2 -2
  45. package/extensions/subagent-tool/process.ts +4 -5
  46. package/extensions/tasks/commands/register-tasks-extension.ts +41 -0
  47. package/extensions/teams-tool/__tests__/peer-messaging.test.ts +29 -24
  48. package/extensions/teams-tool/dashboard.ts +3 -5
  49. package/extensions/teams-tool/dispatch/auto-dispatch.ts +18 -1
  50. package/extensions/teams-tool/tools/teammate-tools.ts +9 -6
  51. package/extensions/wezterm-pane-control/__tests__/index.test.ts +88 -4
  52. package/extensions/wezterm-pane-control/index.ts +113 -8
  53. package/package.json +6 -4
  54. package/packages/tallow-tui/README.md +51 -0
  55. package/packages/tallow-tui/dist/autocomplete.d.ts +48 -0
  56. package/packages/tallow-tui/dist/autocomplete.d.ts.map +1 -0
  57. package/packages/tallow-tui/dist/autocomplete.js +564 -0
  58. package/packages/tallow-tui/dist/autocomplete.js.map +1 -0
  59. package/packages/tallow-tui/dist/border-styles.d.ts +32 -0
  60. package/packages/tallow-tui/dist/border-styles.d.ts.map +1 -0
  61. package/packages/tallow-tui/dist/border-styles.js +46 -0
  62. package/packages/tallow-tui/dist/border-styles.js.map +1 -0
  63. package/packages/tallow-tui/dist/components/bordered-box.d.ts +52 -0
  64. package/packages/tallow-tui/dist/components/bordered-box.d.ts.map +1 -0
  65. package/packages/tallow-tui/dist/components/bordered-box.js +89 -0
  66. package/packages/tallow-tui/dist/components/bordered-box.js.map +1 -0
  67. package/packages/tallow-tui/dist/components/box.d.ts +22 -0
  68. package/packages/tallow-tui/dist/components/box.d.ts.map +1 -0
  69. package/packages/tallow-tui/dist/components/box.js +104 -0
  70. package/packages/tallow-tui/dist/components/box.js.map +1 -0
  71. package/packages/tallow-tui/dist/components/cancellable-loader.d.ts +22 -0
  72. package/packages/tallow-tui/dist/components/cancellable-loader.d.ts.map +1 -0
  73. package/packages/tallow-tui/dist/components/cancellable-loader.js +35 -0
  74. package/packages/tallow-tui/dist/components/cancellable-loader.js.map +1 -0
  75. package/packages/tallow-tui/dist/components/editor.d.ts +240 -0
  76. package/packages/tallow-tui/dist/components/editor.d.ts.map +1 -0
  77. package/packages/tallow-tui/dist/components/editor.js +1766 -0
  78. package/packages/tallow-tui/dist/components/editor.js.map +1 -0
  79. package/packages/tallow-tui/dist/components/image.d.ts +126 -0
  80. package/packages/tallow-tui/dist/components/image.d.ts.map +1 -0
  81. package/packages/tallow-tui/dist/components/image.js +245 -0
  82. package/packages/tallow-tui/dist/components/image.js.map +1 -0
  83. package/packages/tallow-tui/dist/components/input.d.ts +37 -0
  84. package/packages/tallow-tui/dist/components/input.d.ts.map +1 -0
  85. package/packages/tallow-tui/dist/components/input.js +439 -0
  86. package/packages/tallow-tui/dist/components/input.js.map +1 -0
  87. package/packages/tallow-tui/dist/components/loader.d.ts +88 -0
  88. package/packages/tallow-tui/dist/components/loader.d.ts.map +1 -0
  89. package/packages/tallow-tui/dist/components/loader.js +146 -0
  90. package/packages/tallow-tui/dist/components/loader.js.map +1 -0
  91. package/packages/tallow-tui/dist/components/markdown.d.ts +95 -0
  92. package/packages/tallow-tui/dist/components/markdown.d.ts.map +1 -0
  93. package/packages/tallow-tui/dist/components/markdown.js +633 -0
  94. package/packages/tallow-tui/dist/components/markdown.js.map +1 -0
  95. package/packages/tallow-tui/dist/components/select-list.d.ts +32 -0
  96. package/packages/tallow-tui/dist/components/select-list.d.ts.map +1 -0
  97. package/packages/tallow-tui/dist/components/select-list.js +156 -0
  98. package/packages/tallow-tui/dist/components/select-list.js.map +1 -0
  99. package/packages/tallow-tui/dist/components/settings-list.d.ts +50 -0
  100. package/packages/tallow-tui/dist/components/settings-list.d.ts.map +1 -0
  101. package/packages/tallow-tui/dist/components/settings-list.js +189 -0
  102. package/packages/tallow-tui/dist/components/settings-list.js.map +1 -0
  103. package/packages/tallow-tui/dist/components/spacer.d.ts +12 -0
  104. package/packages/tallow-tui/dist/components/spacer.d.ts.map +1 -0
  105. package/packages/tallow-tui/dist/components/spacer.js +23 -0
  106. package/packages/tallow-tui/dist/components/spacer.js.map +1 -0
  107. package/packages/tallow-tui/dist/components/text.d.ts +19 -0
  108. package/packages/tallow-tui/dist/components/text.d.ts.map +1 -0
  109. package/packages/tallow-tui/dist/components/text.js +91 -0
  110. package/packages/tallow-tui/dist/components/text.js.map +1 -0
  111. package/packages/tallow-tui/dist/components/truncated-text.d.ts +13 -0
  112. package/packages/tallow-tui/dist/components/truncated-text.d.ts.map +1 -0
  113. package/packages/tallow-tui/dist/components/truncated-text.js +51 -0
  114. package/packages/tallow-tui/dist/components/truncated-text.js.map +1 -0
  115. package/packages/tallow-tui/dist/editor-component.d.ts +50 -0
  116. package/packages/tallow-tui/dist/editor-component.d.ts.map +1 -0
  117. package/packages/tallow-tui/dist/editor-component.js +2 -0
  118. package/packages/tallow-tui/dist/editor-component.js.map +1 -0
  119. package/packages/tallow-tui/dist/fuzzy.d.ts +16 -0
  120. package/packages/tallow-tui/dist/fuzzy.d.ts.map +1 -0
  121. package/packages/tallow-tui/dist/fuzzy.js +107 -0
  122. package/packages/tallow-tui/dist/fuzzy.js.map +1 -0
  123. package/packages/tallow-tui/dist/index.d.ts +25 -0
  124. package/packages/tallow-tui/dist/index.d.ts.map +1 -0
  125. package/packages/tallow-tui/dist/index.js +35 -0
  126. package/packages/tallow-tui/dist/index.js.map +1 -0
  127. package/packages/tallow-tui/dist/keybindings.d.ts +39 -0
  128. package/packages/tallow-tui/dist/keybindings.d.ts.map +1 -0
  129. package/packages/tallow-tui/dist/keybindings.js +114 -0
  130. package/packages/tallow-tui/dist/keybindings.js.map +1 -0
  131. package/packages/tallow-tui/dist/keys.d.ts +168 -0
  132. package/packages/tallow-tui/dist/keys.d.ts.map +1 -0
  133. package/packages/tallow-tui/dist/keys.js +971 -0
  134. package/packages/tallow-tui/dist/keys.js.map +1 -0
  135. package/packages/tallow-tui/dist/kill-ring.d.ts +28 -0
  136. package/packages/tallow-tui/dist/kill-ring.d.ts.map +1 -0
  137. package/packages/tallow-tui/dist/kill-ring.js +44 -0
  138. package/packages/tallow-tui/dist/kill-ring.js.map +1 -0
  139. package/packages/tallow-tui/dist/stdin-buffer.d.ts +48 -0
  140. package/packages/tallow-tui/dist/stdin-buffer.d.ts.map +1 -0
  141. package/packages/tallow-tui/dist/stdin-buffer.js +317 -0
  142. package/packages/tallow-tui/dist/stdin-buffer.js.map +1 -0
  143. package/packages/tallow-tui/dist/terminal-image.d.ts +161 -0
  144. package/packages/tallow-tui/dist/terminal-image.d.ts.map +1 -0
  145. package/packages/tallow-tui/dist/terminal-image.js +460 -0
  146. package/packages/tallow-tui/dist/terminal-image.js.map +1 -0
  147. package/packages/tallow-tui/dist/terminal.d.ts +102 -0
  148. package/packages/tallow-tui/dist/terminal.d.ts.map +1 -0
  149. package/packages/tallow-tui/dist/terminal.js +263 -0
  150. package/packages/tallow-tui/dist/terminal.js.map +1 -0
  151. package/packages/tallow-tui/dist/test-utils/capability-env.d.ts +14 -0
  152. package/packages/tallow-tui/dist/test-utils/capability-env.d.ts.map +1 -0
  153. package/packages/tallow-tui/dist/test-utils/capability-env.js +55 -0
  154. package/packages/tallow-tui/dist/test-utils/capability-env.js.map +1 -0
  155. package/packages/tallow-tui/dist/tui.d.ts +239 -0
  156. package/packages/tallow-tui/dist/tui.d.ts.map +1 -0
  157. package/packages/tallow-tui/dist/tui.js +1058 -0
  158. package/packages/tallow-tui/dist/tui.js.map +1 -0
  159. package/packages/tallow-tui/dist/undo-stack.d.ts +17 -0
  160. package/packages/tallow-tui/dist/undo-stack.d.ts.map +1 -0
  161. package/packages/tallow-tui/dist/undo-stack.js +25 -0
  162. package/packages/tallow-tui/dist/undo-stack.js.map +1 -0
  163. package/packages/tallow-tui/dist/utils.d.ts +96 -0
  164. package/packages/tallow-tui/dist/utils.d.ts.map +1 -0
  165. package/packages/tallow-tui/dist/utils.js +843 -0
  166. package/packages/tallow-tui/dist/utils.js.map +1 -0
  167. package/packages/tallow-tui/package.json +24 -0
  168. package/packages/tallow-tui/src/__tests__/__snapshots__/render.test.ts.snap +121 -0
  169. package/packages/tallow-tui/src/__tests__/editor-border.test.ts +72 -0
  170. package/packages/tallow-tui/src/__tests__/editor-change-listener.test.ts +121 -0
  171. package/packages/tallow-tui/src/__tests__/editor-ghost-text.test.ts +112 -0
  172. package/packages/tallow-tui/src/__tests__/fuzzy.test.ts +91 -0
  173. package/packages/tallow-tui/src/__tests__/image-component.test.ts +113 -0
  174. package/packages/tallow-tui/src/__tests__/keys.test.ts +141 -0
  175. package/packages/tallow-tui/src/__tests__/render.test.ts +179 -0
  176. package/packages/tallow-tui/src/__tests__/stdin-buffer.test.ts +82 -0
  177. package/packages/tallow-tui/src/__tests__/terminal-image.test.ts +363 -0
  178. package/packages/tallow-tui/src/__tests__/tui-diff-regression.test.ts +454 -0
  179. package/packages/tallow-tui/src/__tests__/tui-render-scheduling.test.ts +256 -0
  180. package/packages/tallow-tui/src/__tests__/utils.test.ts +259 -0
  181. package/packages/tallow-tui/src/autocomplete.ts +716 -0
  182. package/packages/tallow-tui/src/border-styles.ts +60 -0
  183. package/packages/tallow-tui/src/components/bordered-box.ts +113 -0
  184. package/packages/tallow-tui/src/components/box.ts +137 -0
  185. package/packages/tallow-tui/src/components/cancellable-loader.ts +40 -0
  186. package/packages/tallow-tui/src/components/editor.ts +2143 -0
  187. package/packages/tallow-tui/src/components/image.ts +315 -0
  188. package/packages/tallow-tui/src/components/input.ts +522 -0
  189. package/packages/tallow-tui/src/components/loader.ts +187 -0
  190. package/packages/tallow-tui/src/components/markdown.ts +780 -0
  191. package/packages/tallow-tui/src/components/select-list.ts +197 -0
  192. package/packages/tallow-tui/src/components/settings-list.ts +264 -0
  193. package/packages/tallow-tui/src/components/spacer.ts +28 -0
  194. package/packages/tallow-tui/src/components/text.ts +113 -0
  195. package/packages/tallow-tui/src/components/truncated-text.ts +65 -0
  196. package/packages/tallow-tui/src/editor-component.ts +92 -0
  197. package/packages/tallow-tui/src/fuzzy.ts +133 -0
  198. package/packages/tallow-tui/src/index.ts +118 -0
  199. package/packages/tallow-tui/src/keybindings.ts +183 -0
  200. package/packages/tallow-tui/src/keys.ts +1189 -0
  201. package/packages/tallow-tui/src/kill-ring.ts +46 -0
  202. package/packages/tallow-tui/src/stdin-buffer.ts +386 -0
  203. package/packages/tallow-tui/src/terminal-image.ts +619 -0
  204. package/packages/tallow-tui/src/terminal.ts +350 -0
  205. package/packages/tallow-tui/src/test-utils/capability-env.ts +56 -0
  206. package/packages/tallow-tui/src/tui.ts +1336 -0
  207. package/packages/tallow-tui/src/undo-stack.ts +28 -0
  208. package/packages/tallow-tui/src/utils.ts +948 -0
  209. package/packages/tallow-tui/tsconfig.build.json +21 -0
  210. package/runtime/agent-runner.ts +20 -0
  211. package/runtime/atomic-write.ts +8 -0
  212. package/runtime/otel.ts +12 -0
  213. package/runtime/resolve-module.ts +23 -0
  214. package/runtime/runtime-path-provider.ts +12 -0
  215. package/runtime/runtime-provenance.ts +17 -0
  216. package/runtime/workspace-transition-relay.ts +21 -0
  217. package/runtime/workspace-transition.ts +29 -0
@@ -0,0 +1,780 @@
1
+ import { marked, type Token } from "marked";
2
+ import { isImageLine } from "../terminal-image.js";
3
+ import type { Component } from "../tui.js";
4
+ import { applyBackgroundToLine, visibleWidth, wrapTextWithAnsi } from "../utils.js";
5
+
6
+ /**
7
+ * Default text styling for markdown content.
8
+ * Applied to all text unless overridden by markdown formatting.
9
+ */
10
+ export interface DefaultTextStyle {
11
+ /** Foreground color function */
12
+ color?: (text: string) => string;
13
+ /** Background color function */
14
+ bgColor?: (text: string) => string;
15
+ /** Bold text */
16
+ bold?: boolean;
17
+ /** Italic text */
18
+ italic?: boolean;
19
+ /** Strikethrough text */
20
+ strikethrough?: boolean;
21
+ /** Underline text */
22
+ underline?: boolean;
23
+ }
24
+
25
+ /**
26
+ * Theme functions for markdown elements.
27
+ * Each function takes text and returns styled text with ANSI codes.
28
+ */
29
+ export interface MarkdownTheme {
30
+ heading: (text: string) => string;
31
+ link: (text: string) => string;
32
+ linkUrl: (text: string) => string;
33
+ code: (text: string) => string;
34
+ codeBlock: (text: string) => string;
35
+ codeBlockBorder: (text: string) => string;
36
+ quote: (text: string) => string;
37
+ quoteBorder: (text: string) => string;
38
+ hr: (text: string) => string;
39
+ listBullet: (text: string) => string;
40
+ bold: (text: string) => string;
41
+ italic: (text: string) => string;
42
+ strikethrough: (text: string) => string;
43
+ underline: (text: string) => string;
44
+ highlightCode?: (code: string, lang?: string) => string[];
45
+ /** Prefix applied to each rendered code block line (default: " ") */
46
+ codeBlockIndent?: string;
47
+ }
48
+
49
+ interface InlineStyleContext {
50
+ applyText: (text: string) => string;
51
+ stylePrefix: string;
52
+ }
53
+
54
+ export class Markdown implements Component {
55
+ private text: string;
56
+ private paddingX: number; // Left/right padding
57
+ private paddingY: number; // Top/bottom padding
58
+ private defaultTextStyle?: DefaultTextStyle;
59
+ private theme: MarkdownTheme;
60
+ private defaultStylePrefix?: string;
61
+
62
+ // Cache for rendered output
63
+ private cachedText?: string;
64
+ private cachedWidth?: number;
65
+ private cachedLines?: string[];
66
+
67
+ constructor(
68
+ text: string,
69
+ paddingX: number,
70
+ paddingY: number,
71
+ theme: MarkdownTheme,
72
+ defaultTextStyle?: DefaultTextStyle
73
+ ) {
74
+ this.text = text;
75
+ this.paddingX = paddingX;
76
+ this.paddingY = paddingY;
77
+ this.theme = theme;
78
+ this.defaultTextStyle = defaultTextStyle;
79
+ }
80
+
81
+ setText(text: string): void {
82
+ this.text = text;
83
+ this.invalidate();
84
+ }
85
+
86
+ invalidate(): void {
87
+ this.cachedText = undefined;
88
+ this.cachedWidth = undefined;
89
+ this.cachedLines = undefined;
90
+ }
91
+
92
+ render(width: number): string[] {
93
+ // Check cache
94
+ if (this.cachedLines && this.cachedText === this.text && this.cachedWidth === width) {
95
+ return this.cachedLines;
96
+ }
97
+
98
+ // Calculate available width for content (subtract horizontal padding)
99
+ const contentWidth = Math.max(1, width - this.paddingX * 2);
100
+
101
+ // Don't render anything if there's no actual text
102
+ if (!this.text || this.text.trim() === "") {
103
+ const result: string[] = [];
104
+ // Update cache
105
+ this.cachedText = this.text;
106
+ this.cachedWidth = width;
107
+ this.cachedLines = result;
108
+ return result;
109
+ }
110
+
111
+ // Replace tabs with 3 spaces for consistent rendering
112
+ const normalizedText = this.text.replace(/\t/g, " ");
113
+
114
+ // Parse markdown to HTML-like tokens
115
+ const tokens = marked.lexer(normalizedText);
116
+
117
+ // Convert tokens to styled terminal output
118
+ const renderedLines: string[] = [];
119
+
120
+ for (let i = 0; i < tokens.length; i++) {
121
+ const token = tokens[i];
122
+ const nextToken = tokens[i + 1];
123
+ const tokenLines = this.renderToken(token, contentWidth, nextToken?.type);
124
+ renderedLines.push(...tokenLines);
125
+ }
126
+
127
+ // Wrap lines (NO padding, NO background yet)
128
+ const wrappedLines: string[] = [];
129
+ for (const line of renderedLines) {
130
+ if (isImageLine(line)) {
131
+ wrappedLines.push(line);
132
+ } else {
133
+ wrappedLines.push(...wrapTextWithAnsi(line, contentWidth));
134
+ }
135
+ }
136
+
137
+ // Add margins and background to each wrapped line
138
+ const leftMargin = " ".repeat(this.paddingX);
139
+ const rightMargin = " ".repeat(this.paddingX);
140
+ const bgFn = this.defaultTextStyle?.bgColor;
141
+ const contentLines: string[] = [];
142
+
143
+ for (const line of wrappedLines) {
144
+ if (isImageLine(line)) {
145
+ contentLines.push(line);
146
+ continue;
147
+ }
148
+
149
+ const lineWithMargins = leftMargin + line + rightMargin;
150
+
151
+ if (bgFn) {
152
+ contentLines.push(applyBackgroundToLine(lineWithMargins, width, bgFn));
153
+ } else {
154
+ // No background - just pad to width
155
+ const visibleLen = visibleWidth(lineWithMargins);
156
+ const paddingNeeded = Math.max(0, width - visibleLen);
157
+ contentLines.push(lineWithMargins + " ".repeat(paddingNeeded));
158
+ }
159
+ }
160
+
161
+ // Add top/bottom padding (empty lines)
162
+ const emptyLine = " ".repeat(width);
163
+ const emptyLines: string[] = [];
164
+ for (let i = 0; i < this.paddingY; i++) {
165
+ const line = bgFn ? applyBackgroundToLine(emptyLine, width, bgFn) : emptyLine;
166
+ emptyLines.push(line);
167
+ }
168
+
169
+ // Combine top padding, content, and bottom padding
170
+ const result = [...emptyLines, ...contentLines, ...emptyLines];
171
+
172
+ // Update cache
173
+ this.cachedText = this.text;
174
+ this.cachedWidth = width;
175
+ this.cachedLines = result;
176
+
177
+ return result.length > 0 ? result : [""];
178
+ }
179
+
180
+ /**
181
+ * Apply default text style to a string.
182
+ * This is the base styling applied to all text content.
183
+ * NOTE: Background color is NOT applied here - it's applied at the padding stage
184
+ * to ensure it extends to the full line width.
185
+ */
186
+ private applyDefaultStyle(text: string): string {
187
+ if (!this.defaultTextStyle) {
188
+ return text;
189
+ }
190
+
191
+ let styled = text;
192
+
193
+ // Apply foreground color (NOT background - that's applied at padding stage)
194
+ if (this.defaultTextStyle.color) {
195
+ styled = this.defaultTextStyle.color(styled);
196
+ }
197
+
198
+ // Apply text decorations using this.theme
199
+ if (this.defaultTextStyle.bold) {
200
+ styled = this.theme.bold(styled);
201
+ }
202
+ if (this.defaultTextStyle.italic) {
203
+ styled = this.theme.italic(styled);
204
+ }
205
+ if (this.defaultTextStyle.strikethrough) {
206
+ styled = this.theme.strikethrough(styled);
207
+ }
208
+ if (this.defaultTextStyle.underline) {
209
+ styled = this.theme.underline(styled);
210
+ }
211
+
212
+ return styled;
213
+ }
214
+
215
+ private getDefaultStylePrefix(): string {
216
+ if (!this.defaultTextStyle) {
217
+ return "";
218
+ }
219
+
220
+ if (this.defaultStylePrefix !== undefined) {
221
+ return this.defaultStylePrefix;
222
+ }
223
+
224
+ const sentinel = "\u0000";
225
+ let styled = sentinel;
226
+
227
+ if (this.defaultTextStyle.color) {
228
+ styled = this.defaultTextStyle.color(styled);
229
+ }
230
+
231
+ if (this.defaultTextStyle.bold) {
232
+ styled = this.theme.bold(styled);
233
+ }
234
+ if (this.defaultTextStyle.italic) {
235
+ styled = this.theme.italic(styled);
236
+ }
237
+ if (this.defaultTextStyle.strikethrough) {
238
+ styled = this.theme.strikethrough(styled);
239
+ }
240
+ if (this.defaultTextStyle.underline) {
241
+ styled = this.theme.underline(styled);
242
+ }
243
+
244
+ const sentinelIndex = styled.indexOf(sentinel);
245
+ this.defaultStylePrefix = sentinelIndex >= 0 ? styled.slice(0, sentinelIndex) : "";
246
+ return this.defaultStylePrefix;
247
+ }
248
+
249
+ private getStylePrefix(styleFn: (text: string) => string): string {
250
+ const sentinel = "\u0000";
251
+ const styled = styleFn(sentinel);
252
+ const sentinelIndex = styled.indexOf(sentinel);
253
+ return sentinelIndex >= 0 ? styled.slice(0, sentinelIndex) : "";
254
+ }
255
+
256
+ private getDefaultInlineStyleContext(): InlineStyleContext {
257
+ return {
258
+ applyText: (text: string) => this.applyDefaultStyle(text),
259
+ stylePrefix: this.getDefaultStylePrefix(),
260
+ };
261
+ }
262
+
263
+ private renderToken(token: Token, width: number, nextTokenType?: string): string[] {
264
+ const lines: string[] = [];
265
+
266
+ switch (token.type) {
267
+ case "heading": {
268
+ const headingLevel = token.depth;
269
+ const headingPrefix = `${"#".repeat(headingLevel)} `;
270
+ const headingText = this.renderInlineTokens(token.tokens || []);
271
+ let styledHeading: string;
272
+ if (headingLevel === 1) {
273
+ styledHeading = this.theme.heading(this.theme.bold(this.theme.underline(headingText)));
274
+ } else if (headingLevel === 2) {
275
+ styledHeading = this.theme.heading(this.theme.bold(headingText));
276
+ } else {
277
+ styledHeading = this.theme.heading(this.theme.bold(headingPrefix + headingText));
278
+ }
279
+ lines.push(styledHeading);
280
+ if (nextTokenType !== "space") {
281
+ lines.push(""); // Add spacing after headings (unless space token follows)
282
+ }
283
+ break;
284
+ }
285
+
286
+ case "paragraph": {
287
+ const paragraphText = this.renderInlineTokens(token.tokens || []);
288
+ lines.push(paragraphText);
289
+ // Don't add spacing if next token is space or list
290
+ if (nextTokenType && nextTokenType !== "list" && nextTokenType !== "space") {
291
+ lines.push("");
292
+ }
293
+ break;
294
+ }
295
+
296
+ case "code": {
297
+ const indent = this.theme.codeBlockIndent ?? " ";
298
+ lines.push(this.theme.codeBlockBorder(`\`\`\`${token.lang || ""}`));
299
+ if (this.theme.highlightCode) {
300
+ const highlightedLines = this.theme.highlightCode(token.text, token.lang);
301
+ for (const hlLine of highlightedLines) {
302
+ lines.push(`${indent}${hlLine}`);
303
+ }
304
+ } else {
305
+ // Split code by newlines and style each line
306
+ const codeLines = token.text.split("\n");
307
+ for (const codeLine of codeLines) {
308
+ lines.push(`${indent}${this.theme.codeBlock(codeLine)}`);
309
+ }
310
+ }
311
+ lines.push(this.theme.codeBlockBorder("```"));
312
+ if (nextTokenType !== "space") {
313
+ lines.push(""); // Add spacing after code blocks (unless space token follows)
314
+ }
315
+ break;
316
+ }
317
+
318
+ case "list": {
319
+ const listLines = this.renderList(token as any, 0);
320
+ lines.push(...listLines);
321
+ // Don't add spacing after lists if a space token follows
322
+ // (the space token will handle it)
323
+ break;
324
+ }
325
+
326
+ case "table": {
327
+ const tableLines = this.renderTable(token as any, width);
328
+ lines.push(...tableLines);
329
+ break;
330
+ }
331
+
332
+ case "blockquote": {
333
+ const quoteStyle = (text: string) => this.theme.quote(this.theme.italic(text));
334
+ const quoteStyleContext: InlineStyleContext = {
335
+ applyText: quoteStyle,
336
+ stylePrefix: this.getStylePrefix(quoteStyle),
337
+ };
338
+ const quoteText = this.renderInlineTokens(token.tokens || [], quoteStyleContext);
339
+ const quoteLines = quoteText.split("\n");
340
+
341
+ // Calculate available width for quote content (subtract border "│ " = 2 chars)
342
+ const quoteContentWidth = Math.max(1, width - 2);
343
+
344
+ for (const quoteLine of quoteLines) {
345
+ // Wrap the styled line, then add border to each wrapped line
346
+ const wrappedLines = wrapTextWithAnsi(quoteLine, quoteContentWidth);
347
+ for (const wrappedLine of wrappedLines) {
348
+ lines.push(this.theme.quoteBorder("│ ") + wrappedLine);
349
+ }
350
+ }
351
+ if (nextTokenType !== "space") {
352
+ lines.push(""); // Add spacing after blockquotes (unless space token follows)
353
+ }
354
+ break;
355
+ }
356
+
357
+ case "hr":
358
+ lines.push(this.theme.hr("─".repeat(Math.min(width, 80))));
359
+ if (nextTokenType !== "space") {
360
+ lines.push(""); // Add spacing after horizontal rules (unless space token follows)
361
+ }
362
+ break;
363
+
364
+ case "html":
365
+ // Render HTML as plain text (escaped for terminal)
366
+ if ("raw" in token && typeof token.raw === "string") {
367
+ lines.push(this.applyDefaultStyle(token.raw.trim()));
368
+ }
369
+ break;
370
+
371
+ case "space":
372
+ // Space tokens represent blank lines in markdown
373
+ lines.push("");
374
+ break;
375
+
376
+ default:
377
+ // Handle any other token types as plain text
378
+ if ("text" in token && typeof token.text === "string") {
379
+ lines.push(token.text);
380
+ }
381
+ }
382
+
383
+ return lines;
384
+ }
385
+
386
+ private renderInlineTokens(tokens: Token[], styleContext?: InlineStyleContext): string {
387
+ let result = "";
388
+ const resolvedStyleContext = styleContext ?? this.getDefaultInlineStyleContext();
389
+ const { applyText, stylePrefix } = resolvedStyleContext;
390
+ const applyTextWithNewlines = (text: string): string => {
391
+ const segments: string[] = text.split("\n");
392
+ return segments.map((segment: string) => applyText(segment)).join("\n");
393
+ };
394
+
395
+ for (const token of tokens) {
396
+ switch (token.type) {
397
+ case "text":
398
+ // Text tokens in list items can have nested tokens for inline formatting
399
+ if (token.tokens && token.tokens.length > 0) {
400
+ result += this.renderInlineTokens(token.tokens, resolvedStyleContext);
401
+ } else {
402
+ result += applyTextWithNewlines(token.text);
403
+ }
404
+ break;
405
+
406
+ case "paragraph":
407
+ // Paragraph tokens contain nested inline tokens
408
+ result += this.renderInlineTokens(token.tokens || [], resolvedStyleContext);
409
+ break;
410
+
411
+ case "strong": {
412
+ const boldContent = this.renderInlineTokens(token.tokens || [], resolvedStyleContext);
413
+ result += this.theme.bold(boldContent) + stylePrefix;
414
+ break;
415
+ }
416
+
417
+ case "em": {
418
+ const italicContent = this.renderInlineTokens(token.tokens || [], resolvedStyleContext);
419
+ result += this.theme.italic(italicContent) + stylePrefix;
420
+ break;
421
+ }
422
+
423
+ case "codespan":
424
+ result += this.theme.code(token.text) + stylePrefix;
425
+ break;
426
+
427
+ case "link": {
428
+ const linkText = this.renderInlineTokens(token.tokens || [], resolvedStyleContext);
429
+ // If link text matches href, only show the link once
430
+ // Compare raw text (token.text) not styled text (linkText) since linkText has ANSI codes
431
+ // For mailto: links, strip the prefix before comparing (autolinked emails have
432
+ // text="foo@bar.com" but href="mailto:foo@bar.com")
433
+ const hrefForComparison = token.href.startsWith("mailto:")
434
+ ? token.href.slice(7)
435
+ : token.href;
436
+ if (token.text === token.href || token.text === hrefForComparison) {
437
+ result += this.theme.link(this.theme.underline(linkText)) + stylePrefix;
438
+ } else {
439
+ result +=
440
+ this.theme.link(this.theme.underline(linkText)) +
441
+ this.theme.linkUrl(` (${token.href})`) +
442
+ stylePrefix;
443
+ }
444
+ break;
445
+ }
446
+
447
+ case "br":
448
+ result += "\n";
449
+ break;
450
+
451
+ case "del": {
452
+ const delContent = this.renderInlineTokens(token.tokens || [], resolvedStyleContext);
453
+ result += this.theme.strikethrough(delContent) + stylePrefix;
454
+ break;
455
+ }
456
+
457
+ case "html":
458
+ // Render inline HTML as plain text
459
+ if ("raw" in token && typeof token.raw === "string") {
460
+ result += applyTextWithNewlines(token.raw);
461
+ }
462
+ break;
463
+
464
+ default:
465
+ // Handle any other inline token types as plain text
466
+ if ("text" in token && typeof token.text === "string") {
467
+ result += applyTextWithNewlines(token.text);
468
+ }
469
+ }
470
+ }
471
+
472
+ return result;
473
+ }
474
+
475
+ /**
476
+ * Render a list with proper nesting support
477
+ */
478
+ private renderList(
479
+ token: Token & { items: any[]; ordered: boolean; start?: number },
480
+ depth: number
481
+ ): string[] {
482
+ const lines: string[] = [];
483
+ const indent = " ".repeat(depth);
484
+ // Use the list's start property (defaults to 1 for ordered lists)
485
+ const startNumber = token.start ?? 1;
486
+
487
+ for (let i = 0; i < token.items.length; i++) {
488
+ const item = token.items[i];
489
+ const bullet = token.ordered ? `${startNumber + i}. ` : "- ";
490
+
491
+ // Process item tokens to handle nested lists
492
+ const itemLines = this.renderListItem(item.tokens || [], depth);
493
+
494
+ if (itemLines.length > 0) {
495
+ // First line - check if it's a nested list
496
+ // A nested list will start with indent (spaces) followed by cyan bullet
497
+ const firstLine = itemLines[0];
498
+ const isNestedList = /^\s+\x1b\[36m[-\d]/.test(firstLine); // starts with spaces + cyan + bullet char
499
+
500
+ if (isNestedList) {
501
+ // This is a nested list, just add it as-is (already has full indent)
502
+ lines.push(firstLine);
503
+ } else {
504
+ // Regular text content - add indent and bullet
505
+ lines.push(indent + this.theme.listBullet(bullet) + firstLine);
506
+ }
507
+
508
+ // Rest of the lines
509
+ for (let j = 1; j < itemLines.length; j++) {
510
+ const line = itemLines[j];
511
+ const isNestedListLine = /^\s+\x1b\[36m[-\d]/.test(line); // starts with spaces + cyan + bullet char
512
+
513
+ if (isNestedListLine) {
514
+ // Nested list line - already has full indent
515
+ lines.push(line);
516
+ } else {
517
+ // Regular content - add parent indent + 2 spaces for continuation
518
+ lines.push(`${indent} ${line}`);
519
+ }
520
+ }
521
+ } else {
522
+ lines.push(indent + this.theme.listBullet(bullet));
523
+ }
524
+ }
525
+
526
+ return lines;
527
+ }
528
+
529
+ /**
530
+ * Render list item tokens, handling nested lists
531
+ * Returns lines WITHOUT the parent indent (renderList will add it)
532
+ */
533
+ private renderListItem(tokens: Token[], parentDepth: number): string[] {
534
+ const lines: string[] = [];
535
+
536
+ for (const token of tokens) {
537
+ if (token.type === "list") {
538
+ // Nested list - render with one additional indent level
539
+ // These lines will have their own indent, so we just add them as-is
540
+ const nestedLines = this.renderList(token as any, parentDepth + 1);
541
+ lines.push(...nestedLines);
542
+ } else if (token.type === "text") {
543
+ // Text content (may have inline tokens)
544
+ const text =
545
+ token.tokens && token.tokens.length > 0
546
+ ? this.renderInlineTokens(token.tokens)
547
+ : token.text || "";
548
+ lines.push(text);
549
+ } else if (token.type === "paragraph") {
550
+ // Paragraph in list item
551
+ const text = this.renderInlineTokens(token.tokens || []);
552
+ lines.push(text);
553
+ } else if (token.type === "code") {
554
+ // Code block in list item
555
+ const indent = this.theme.codeBlockIndent ?? " ";
556
+ lines.push(this.theme.codeBlockBorder(`\`\`\`${token.lang || ""}`));
557
+ if (this.theme.highlightCode) {
558
+ const highlightedLines = this.theme.highlightCode(token.text, token.lang);
559
+ for (const hlLine of highlightedLines) {
560
+ lines.push(`${indent}${hlLine}`);
561
+ }
562
+ } else {
563
+ const codeLines = token.text.split("\n");
564
+ for (const codeLine of codeLines) {
565
+ lines.push(`${indent}${this.theme.codeBlock(codeLine)}`);
566
+ }
567
+ }
568
+ lines.push(this.theme.codeBlockBorder("```"));
569
+ } else {
570
+ // Other token types - try to render as inline
571
+ const text = this.renderInlineTokens([token]);
572
+ if (text) {
573
+ lines.push(text);
574
+ }
575
+ }
576
+ }
577
+
578
+ return lines;
579
+ }
580
+
581
+ /**
582
+ * Get the visible width of the longest word in a string.
583
+ */
584
+ private getLongestWordWidth(text: string, maxWidth?: number): number {
585
+ const words = text.split(/\s+/).filter((word) => word.length > 0);
586
+ let longest = 0;
587
+ for (const word of words) {
588
+ longest = Math.max(longest, visibleWidth(word));
589
+ }
590
+ if (maxWidth === undefined) {
591
+ return longest;
592
+ }
593
+ return Math.min(longest, maxWidth);
594
+ }
595
+
596
+ /**
597
+ * Wrap a table cell to fit into a column.
598
+ *
599
+ * Delegates to wrapTextWithAnsi() so ANSI codes + long tokens are handled
600
+ * consistently with the rest of the renderer.
601
+ */
602
+ private wrapCellText(text: string, maxWidth: number): string[] {
603
+ return wrapTextWithAnsi(text, Math.max(1, maxWidth));
604
+ }
605
+
606
+ /**
607
+ * Render a table with width-aware cell wrapping.
608
+ * Cells that don't fit are wrapped to multiple lines.
609
+ */
610
+ private renderTable(
611
+ token: Token & { header: any[]; rows: any[][]; raw?: string },
612
+ availableWidth: number
613
+ ): string[] {
614
+ const lines: string[] = [];
615
+ const numCols = token.header.length;
616
+
617
+ if (numCols === 0) {
618
+ return lines;
619
+ }
620
+
621
+ // Calculate border overhead: "│ " + (n-1) * " │ " + " │"
622
+ // = 2 + (n-1) * 3 + 2 = 3n + 1
623
+ const borderOverhead = 3 * numCols + 1;
624
+ const availableForCells = availableWidth - borderOverhead;
625
+ if (availableForCells < numCols) {
626
+ // Too narrow to render a stable table. Fall back to raw markdown.
627
+ const fallbackLines = token.raw ? wrapTextWithAnsi(token.raw, availableWidth) : [];
628
+ fallbackLines.push("");
629
+ return fallbackLines;
630
+ }
631
+
632
+ const maxUnbrokenWordWidth = 30;
633
+
634
+ // Calculate natural column widths (what each column needs without constraints)
635
+ const naturalWidths: number[] = [];
636
+ const minWordWidths: number[] = [];
637
+ for (let i = 0; i < numCols; i++) {
638
+ const headerText = this.renderInlineTokens(token.header[i].tokens || []);
639
+ naturalWidths[i] = visibleWidth(headerText);
640
+ minWordWidths[i] = Math.max(1, this.getLongestWordWidth(headerText, maxUnbrokenWordWidth));
641
+ }
642
+ for (const row of token.rows) {
643
+ for (let i = 0; i < row.length; i++) {
644
+ const cellText = this.renderInlineTokens(row[i].tokens || []);
645
+ naturalWidths[i] = Math.max(naturalWidths[i] || 0, visibleWidth(cellText));
646
+ minWordWidths[i] = Math.max(
647
+ minWordWidths[i] || 1,
648
+ this.getLongestWordWidth(cellText, maxUnbrokenWordWidth)
649
+ );
650
+ }
651
+ }
652
+
653
+ let minColumnWidths = minWordWidths;
654
+ let minCellsWidth = minColumnWidths.reduce((a, b) => a + b, 0);
655
+
656
+ if (minCellsWidth > availableForCells) {
657
+ minColumnWidths = new Array(numCols).fill(1);
658
+ const remaining = availableForCells - numCols;
659
+
660
+ if (remaining > 0) {
661
+ const totalWeight = minWordWidths.reduce(
662
+ (total, width) => total + Math.max(0, width - 1),
663
+ 0
664
+ );
665
+ const growth = minWordWidths.map((width) => {
666
+ const weight = Math.max(0, width - 1);
667
+ return totalWeight > 0 ? Math.floor((weight / totalWeight) * remaining) : 0;
668
+ });
669
+
670
+ for (let i = 0; i < numCols; i++) {
671
+ minColumnWidths[i] += growth[i] ?? 0;
672
+ }
673
+
674
+ const allocated = growth.reduce((total, width) => total + width, 0);
675
+ let leftover = remaining - allocated;
676
+ for (let i = 0; leftover > 0 && i < numCols; i++) {
677
+ minColumnWidths[i]++;
678
+ leftover--;
679
+ }
680
+ }
681
+
682
+ minCellsWidth = minColumnWidths.reduce((a, b) => a + b, 0);
683
+ }
684
+
685
+ // Calculate column widths that fit within available width
686
+ const totalNaturalWidth = naturalWidths.reduce((a, b) => a + b, 0) + borderOverhead;
687
+ let columnWidths: number[];
688
+
689
+ if (totalNaturalWidth <= availableWidth) {
690
+ // Everything fits naturally
691
+ columnWidths = naturalWidths.map((width, index) => Math.max(width, minColumnWidths[index]));
692
+ } else {
693
+ // Need to shrink columns to fit
694
+ const totalGrowPotential = naturalWidths.reduce((total, width, index) => {
695
+ return total + Math.max(0, width - minColumnWidths[index]);
696
+ }, 0);
697
+ const extraWidth = Math.max(0, availableForCells - minCellsWidth);
698
+ columnWidths = minColumnWidths.map((minWidth, index) => {
699
+ const naturalWidth = naturalWidths[index];
700
+ const minWidthDelta = Math.max(0, naturalWidth - minWidth);
701
+ let grow = 0;
702
+ if (totalGrowPotential > 0) {
703
+ grow = Math.floor((minWidthDelta / totalGrowPotential) * extraWidth);
704
+ }
705
+ return minWidth + grow;
706
+ });
707
+
708
+ // Adjust for rounding errors - distribute remaining space
709
+ const allocated = columnWidths.reduce((a, b) => a + b, 0);
710
+ let remaining = availableForCells - allocated;
711
+ while (remaining > 0) {
712
+ let grew = false;
713
+ for (let i = 0; i < numCols && remaining > 0; i++) {
714
+ if (columnWidths[i] < naturalWidths[i]) {
715
+ columnWidths[i]++;
716
+ remaining--;
717
+ grew = true;
718
+ }
719
+ }
720
+ if (!grew) {
721
+ break;
722
+ }
723
+ }
724
+ }
725
+
726
+ // Render top border
727
+ const topBorderCells = columnWidths.map((w) => "─".repeat(w));
728
+ lines.push(`┌─${topBorderCells.join("─┬─")}─┐`);
729
+
730
+ // Render header with wrapping
731
+ const headerCellLines: string[][] = token.header.map((cell, i) => {
732
+ const text = this.renderInlineTokens(cell.tokens || []);
733
+ return this.wrapCellText(text, columnWidths[i]);
734
+ });
735
+ const headerLineCount = Math.max(...headerCellLines.map((c) => c.length));
736
+
737
+ for (let lineIdx = 0; lineIdx < headerLineCount; lineIdx++) {
738
+ const rowParts = headerCellLines.map((cellLines, colIdx) => {
739
+ const text = cellLines[lineIdx] || "";
740
+ const padded = text + " ".repeat(Math.max(0, columnWidths[colIdx] - visibleWidth(text)));
741
+ return this.theme.bold(padded);
742
+ });
743
+ lines.push(`│ ${rowParts.join(" │ ")} │`);
744
+ }
745
+
746
+ // Render separator
747
+ const separatorCells = columnWidths.map((w) => "─".repeat(w));
748
+ const separatorLine = `├─${separatorCells.join("─┼─")}─┤`;
749
+ lines.push(separatorLine);
750
+
751
+ // Render rows with wrapping
752
+ for (let rowIndex = 0; rowIndex < token.rows.length; rowIndex++) {
753
+ const row = token.rows[rowIndex];
754
+ const rowCellLines: string[][] = row.map((cell, i) => {
755
+ const text = this.renderInlineTokens(cell.tokens || []);
756
+ return this.wrapCellText(text, columnWidths[i]);
757
+ });
758
+ const rowLineCount = Math.max(...rowCellLines.map((c) => c.length));
759
+
760
+ for (let lineIdx = 0; lineIdx < rowLineCount; lineIdx++) {
761
+ const rowParts = rowCellLines.map((cellLines, colIdx) => {
762
+ const text = cellLines[lineIdx] || "";
763
+ return text + " ".repeat(Math.max(0, columnWidths[colIdx] - visibleWidth(text)));
764
+ });
765
+ lines.push(`│ ${rowParts.join(" │ ")} │`);
766
+ }
767
+
768
+ if (rowIndex < token.rows.length - 1) {
769
+ lines.push(separatorLine);
770
+ }
771
+ }
772
+
773
+ // Render bottom border
774
+ const bottomBorderCells = columnWidths.map((w) => "─".repeat(w));
775
+ lines.push(`└─${bottomBorderCells.join("─┴─")}─┘`);
776
+
777
+ lines.push(""); // Add spacing after table
778
+ return lines;
779
+ }
780
+ }