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