@pencil-agent/nano-pencil 2.0.0-beta.2 → 2.0.0-beta.3

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