@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,522 @@
1
+ import { getEditorKeybindings } from "../keybindings.js";
2
+ import { KillRing } from "../kill-ring.js";
3
+ import { type Component, CURSOR_MARKER, type Focusable } from "../tui.js";
4
+ import { UndoStack } from "../undo-stack.js";
5
+ import { getSegmenter, isPunctuationChar, isWhitespaceChar, visibleWidth } from "../utils.js";
6
+
7
+ const segmenter = getSegmenter();
8
+
9
+ interface InputState {
10
+ value: string;
11
+ cursor: number;
12
+ }
13
+
14
+ /**
15
+ * Input component - single-line text input with horizontal scrolling
16
+ */
17
+ export class Input implements Component, Focusable {
18
+ private value: string = "";
19
+ private cursor: number = 0; // Cursor position in the value
20
+ public onSubmit?: (value: string) => void;
21
+ public onEscape?: () => void;
22
+
23
+ /** Focusable interface - set by TUI when focus changes */
24
+ focused: boolean = false;
25
+
26
+ // Bracketed paste mode buffering
27
+ private pasteBuffer: string = "";
28
+ private isInPaste: boolean = false;
29
+
30
+ // Kill ring for Emacs-style kill/yank operations
31
+ private killRing = new KillRing();
32
+ private lastAction: "kill" | "yank" | "type-word" | null = null;
33
+
34
+ // Undo support
35
+ private undoStack = new UndoStack<InputState>();
36
+
37
+ getValue(): string {
38
+ return this.value;
39
+ }
40
+
41
+ setValue(value: string): void {
42
+ this.value = value;
43
+ this.cursor = Math.min(this.cursor, value.length);
44
+ }
45
+
46
+ handleInput(data: string): void {
47
+ // Handle bracketed paste mode
48
+ // Start of paste: \x1b[200~
49
+ // End of paste: \x1b[201~
50
+
51
+ // Check if we're starting a bracketed paste
52
+ if (data.includes("\x1b[200~")) {
53
+ this.isInPaste = true;
54
+ this.pasteBuffer = "";
55
+ data = data.replace("\x1b[200~", "");
56
+ }
57
+
58
+ // If we're in a paste, buffer the data
59
+ if (this.isInPaste) {
60
+ // Check if this chunk contains the end marker
61
+ this.pasteBuffer += data;
62
+
63
+ const endIndex = this.pasteBuffer.indexOf("\x1b[201~");
64
+ if (endIndex !== -1) {
65
+ // Extract the pasted content
66
+ const pasteContent = this.pasteBuffer.substring(0, endIndex);
67
+
68
+ // Process the complete paste
69
+ this.handlePaste(pasteContent);
70
+
71
+ // Reset paste state
72
+ this.isInPaste = false;
73
+
74
+ // Handle any remaining input after the paste marker
75
+ const remaining = this.pasteBuffer.substring(endIndex + 6); // 6 = length of \x1b[201~
76
+ this.pasteBuffer = "";
77
+ if (remaining) {
78
+ this.handleInput(remaining);
79
+ }
80
+ }
81
+ return;
82
+ }
83
+
84
+ const kb = getEditorKeybindings();
85
+
86
+ // Escape/Cancel
87
+ if (kb.matches(data, "selectCancel")) {
88
+ if (this.onEscape) this.onEscape();
89
+ return;
90
+ }
91
+
92
+ // Undo
93
+ if (kb.matches(data, "undo")) {
94
+ this.undo();
95
+ return;
96
+ }
97
+
98
+ // Submit
99
+ if (kb.matches(data, "submit") || data === "\n") {
100
+ if (this.onSubmit) this.onSubmit(this.value);
101
+ return;
102
+ }
103
+
104
+ // Deletion
105
+ if (kb.matches(data, "deleteCharBackward")) {
106
+ this.handleBackspace();
107
+ return;
108
+ }
109
+
110
+ if (kb.matches(data, "deleteCharForward")) {
111
+ this.handleForwardDelete();
112
+ return;
113
+ }
114
+
115
+ if (kb.matches(data, "deleteWordBackward")) {
116
+ this.deleteWordBackwards();
117
+ return;
118
+ }
119
+
120
+ if (kb.matches(data, "deleteWordForward")) {
121
+ this.deleteWordForward();
122
+ return;
123
+ }
124
+
125
+ if (kb.matches(data, "deleteToLineStart")) {
126
+ this.deleteToLineStart();
127
+ return;
128
+ }
129
+
130
+ if (kb.matches(data, "deleteToLineEnd")) {
131
+ this.deleteToLineEnd();
132
+ return;
133
+ }
134
+
135
+ // Kill ring actions
136
+ if (kb.matches(data, "yank")) {
137
+ this.yank();
138
+ return;
139
+ }
140
+ if (kb.matches(data, "yankPop")) {
141
+ this.yankPop();
142
+ return;
143
+ }
144
+
145
+ // Cursor movement
146
+ if (kb.matches(data, "cursorLeft")) {
147
+ this.lastAction = null;
148
+ if (this.cursor > 0) {
149
+ const beforeCursor = this.value.slice(0, this.cursor);
150
+ const graphemes = [...segmenter.segment(beforeCursor)];
151
+ const lastGrapheme = graphemes[graphemes.length - 1];
152
+ this.cursor -= lastGrapheme ? lastGrapheme.segment.length : 1;
153
+ }
154
+ return;
155
+ }
156
+
157
+ if (kb.matches(data, "cursorRight")) {
158
+ this.lastAction = null;
159
+ if (this.cursor < this.value.length) {
160
+ const afterCursor = this.value.slice(this.cursor);
161
+ const graphemes = [...segmenter.segment(afterCursor)];
162
+ const firstGrapheme = graphemes[0];
163
+ this.cursor += firstGrapheme ? firstGrapheme.segment.length : 1;
164
+ }
165
+ return;
166
+ }
167
+
168
+ if (kb.matches(data, "cursorLineStart")) {
169
+ this.lastAction = null;
170
+ this.cursor = 0;
171
+ return;
172
+ }
173
+
174
+ if (kb.matches(data, "cursorLineEnd")) {
175
+ this.lastAction = null;
176
+ this.cursor = this.value.length;
177
+ return;
178
+ }
179
+
180
+ if (kb.matches(data, "cursorWordLeft")) {
181
+ this.moveWordBackwards();
182
+ return;
183
+ }
184
+
185
+ if (kb.matches(data, "cursorWordRight")) {
186
+ this.moveWordForwards();
187
+ return;
188
+ }
189
+
190
+ // Regular character input - accept printable characters including Unicode,
191
+ // but reject control characters (C0: 0x00-0x1F, DEL: 0x7F, C1: 0x80-0x9F)
192
+ const hasControlChars = [...data].some((ch) => {
193
+ const code = ch.charCodeAt(0);
194
+ return code < 32 || code === 0x7f || (code >= 0x80 && code <= 0x9f);
195
+ });
196
+ if (!hasControlChars) {
197
+ this.insertCharacter(data);
198
+ }
199
+ }
200
+
201
+ private insertCharacter(char: string): void {
202
+ // Undo coalescing: consecutive word chars coalesce into one undo unit
203
+ if (isWhitespaceChar(char) || this.lastAction !== "type-word") {
204
+ this.pushUndo();
205
+ }
206
+ this.lastAction = "type-word";
207
+
208
+ this.value = this.value.slice(0, this.cursor) + char + this.value.slice(this.cursor);
209
+ this.cursor += char.length;
210
+ }
211
+
212
+ private handleBackspace(): void {
213
+ this.lastAction = null;
214
+ if (this.cursor > 0) {
215
+ this.pushUndo();
216
+ const beforeCursor = this.value.slice(0, this.cursor);
217
+ const graphemes = [...segmenter.segment(beforeCursor)];
218
+ const lastGrapheme = graphemes[graphemes.length - 1];
219
+ const graphemeLength = lastGrapheme ? lastGrapheme.segment.length : 1;
220
+ this.value =
221
+ this.value.slice(0, this.cursor - graphemeLength) + this.value.slice(this.cursor);
222
+ this.cursor -= graphemeLength;
223
+ }
224
+ }
225
+
226
+ private handleForwardDelete(): void {
227
+ this.lastAction = null;
228
+ if (this.cursor < this.value.length) {
229
+ this.pushUndo();
230
+ const afterCursor = this.value.slice(this.cursor);
231
+ const graphemes = [...segmenter.segment(afterCursor)];
232
+ const firstGrapheme = graphemes[0];
233
+ const graphemeLength = firstGrapheme ? firstGrapheme.segment.length : 1;
234
+ this.value =
235
+ this.value.slice(0, this.cursor) + this.value.slice(this.cursor + graphemeLength);
236
+ }
237
+ }
238
+
239
+ private deleteToLineStart(): void {
240
+ if (this.cursor === 0) return;
241
+ this.pushUndo();
242
+ const deletedText = this.value.slice(0, this.cursor);
243
+ this.killRing.push(deletedText, { prepend: true, accumulate: this.lastAction === "kill" });
244
+ this.lastAction = "kill";
245
+ this.value = this.value.slice(this.cursor);
246
+ this.cursor = 0;
247
+ }
248
+
249
+ private deleteToLineEnd(): void {
250
+ if (this.cursor >= this.value.length) return;
251
+ this.pushUndo();
252
+ const deletedText = this.value.slice(this.cursor);
253
+ this.killRing.push(deletedText, { prepend: false, accumulate: this.lastAction === "kill" });
254
+ this.lastAction = "kill";
255
+ this.value = this.value.slice(0, this.cursor);
256
+ }
257
+
258
+ private deleteWordBackwards(): void {
259
+ if (this.cursor === 0) return;
260
+
261
+ // Save lastAction before cursor movement (moveWordBackwards resets it)
262
+ const wasKill = this.lastAction === "kill";
263
+
264
+ this.pushUndo();
265
+
266
+ const oldCursor = this.cursor;
267
+ this.moveWordBackwards();
268
+ const deleteFrom = this.cursor;
269
+ this.cursor = oldCursor;
270
+
271
+ const deletedText = this.value.slice(deleteFrom, this.cursor);
272
+ this.killRing.push(deletedText, { prepend: true, accumulate: wasKill });
273
+ this.lastAction = "kill";
274
+
275
+ this.value = this.value.slice(0, deleteFrom) + this.value.slice(this.cursor);
276
+ this.cursor = deleteFrom;
277
+ }
278
+
279
+ private deleteWordForward(): void {
280
+ if (this.cursor >= this.value.length) return;
281
+
282
+ // Save lastAction before cursor movement (moveWordForwards resets it)
283
+ const wasKill = this.lastAction === "kill";
284
+
285
+ this.pushUndo();
286
+
287
+ const oldCursor = this.cursor;
288
+ this.moveWordForwards();
289
+ const deleteTo = this.cursor;
290
+ this.cursor = oldCursor;
291
+
292
+ const deletedText = this.value.slice(this.cursor, deleteTo);
293
+ this.killRing.push(deletedText, { prepend: false, accumulate: wasKill });
294
+ this.lastAction = "kill";
295
+
296
+ this.value = this.value.slice(0, this.cursor) + this.value.slice(deleteTo);
297
+ }
298
+
299
+ private yank(): void {
300
+ const text = this.killRing.peek();
301
+ if (!text) return;
302
+
303
+ this.pushUndo();
304
+
305
+ this.value = this.value.slice(0, this.cursor) + text + this.value.slice(this.cursor);
306
+ this.cursor += text.length;
307
+ this.lastAction = "yank";
308
+ }
309
+
310
+ private yankPop(): void {
311
+ if (this.lastAction !== "yank" || this.killRing.length <= 1) return;
312
+
313
+ this.pushUndo();
314
+
315
+ // Delete the previously yanked text (still at end of ring before rotation)
316
+ const prevText = this.killRing.peek() || "";
317
+ this.value = this.value.slice(0, this.cursor - prevText.length) + this.value.slice(this.cursor);
318
+ this.cursor -= prevText.length;
319
+
320
+ // Rotate and insert new entry
321
+ this.killRing.rotate();
322
+ const text = this.killRing.peek() || "";
323
+ this.value = this.value.slice(0, this.cursor) + text + this.value.slice(this.cursor);
324
+ this.cursor += text.length;
325
+ this.lastAction = "yank";
326
+ }
327
+
328
+ private pushUndo(): void {
329
+ this.undoStack.push({ value: this.value, cursor: this.cursor });
330
+ }
331
+
332
+ private undo(): void {
333
+ const snapshot = this.undoStack.pop();
334
+ if (!snapshot) return;
335
+ this.value = snapshot.value;
336
+ this.cursor = snapshot.cursor;
337
+ this.lastAction = null;
338
+ }
339
+
340
+ private moveWordBackwards(): void {
341
+ if (this.cursor === 0) {
342
+ return;
343
+ }
344
+
345
+ this.lastAction = null;
346
+ const textBeforeCursor = this.value.slice(0, this.cursor);
347
+ const graphemes = [...segmenter.segment(textBeforeCursor)];
348
+
349
+ // Skip trailing whitespace
350
+ while (
351
+ graphemes.length > 0 &&
352
+ isWhitespaceChar(graphemes[graphemes.length - 1]?.segment || "")
353
+ ) {
354
+ this.cursor -= graphemes.pop()?.segment.length || 0;
355
+ }
356
+
357
+ if (graphemes.length > 0) {
358
+ const lastGrapheme = graphemes[graphemes.length - 1]?.segment || "";
359
+ if (isPunctuationChar(lastGrapheme)) {
360
+ // Skip punctuation run
361
+ while (
362
+ graphemes.length > 0 &&
363
+ isPunctuationChar(graphemes[graphemes.length - 1]?.segment || "")
364
+ ) {
365
+ this.cursor -= graphemes.pop()?.segment.length || 0;
366
+ }
367
+ } else {
368
+ // Skip word run
369
+ while (
370
+ graphemes.length > 0 &&
371
+ !isWhitespaceChar(graphemes[graphemes.length - 1]?.segment || "") &&
372
+ !isPunctuationChar(graphemes[graphemes.length - 1]?.segment || "")
373
+ ) {
374
+ this.cursor -= graphemes.pop()?.segment.length || 0;
375
+ }
376
+ }
377
+ }
378
+ }
379
+
380
+ private moveWordForwards(): void {
381
+ if (this.cursor >= this.value.length) {
382
+ return;
383
+ }
384
+
385
+ this.lastAction = null;
386
+ const textAfterCursor = this.value.slice(this.cursor);
387
+ const segments = segmenter.segment(textAfterCursor);
388
+ const iterator = segments[Symbol.iterator]();
389
+ let next = iterator.next();
390
+
391
+ // Skip leading whitespace
392
+ while (!next.done && isWhitespaceChar(next.value.segment)) {
393
+ this.cursor += next.value.segment.length;
394
+ next = iterator.next();
395
+ }
396
+
397
+ if (!next.done) {
398
+ const firstGrapheme = next.value.segment;
399
+ if (isPunctuationChar(firstGrapheme)) {
400
+ // Skip punctuation run
401
+ while (!next.done && isPunctuationChar(next.value.segment)) {
402
+ this.cursor += next.value.segment.length;
403
+ next = iterator.next();
404
+ }
405
+ } else {
406
+ // Skip word run
407
+ while (
408
+ !next.done &&
409
+ !isWhitespaceChar(next.value.segment) &&
410
+ !isPunctuationChar(next.value.segment)
411
+ ) {
412
+ this.cursor += next.value.segment.length;
413
+ next = iterator.next();
414
+ }
415
+ }
416
+ }
417
+ }
418
+
419
+ private handlePaste(pastedText: string): void {
420
+ this.lastAction = null;
421
+ this.pushUndo();
422
+
423
+ // Clean the pasted text - remove newlines and carriage returns
424
+ const cleanText = pastedText.replace(/\r\n/g, "").replace(/\r/g, "").replace(/\n/g, "");
425
+
426
+ // Insert at cursor position
427
+ this.value = this.value.slice(0, this.cursor) + cleanText + this.value.slice(this.cursor);
428
+ this.cursor += cleanText.length;
429
+ }
430
+
431
+ invalidate(): void {
432
+ // No cached state to invalidate currently
433
+ }
434
+
435
+ render(width: number): string[] {
436
+ // Calculate visible window
437
+ const prompt = "> ";
438
+ const availableWidth = width - prompt.length;
439
+
440
+ if (availableWidth <= 0) {
441
+ return [prompt];
442
+ }
443
+
444
+ let visibleText = "";
445
+ let cursorDisplay = this.cursor;
446
+
447
+ if (this.value.length < availableWidth) {
448
+ // Everything fits (leave room for cursor at end)
449
+ visibleText = this.value;
450
+ } else {
451
+ // Need horizontal scrolling
452
+ // Reserve one character for cursor if it's at the end
453
+ const scrollWidth = this.cursor === this.value.length ? availableWidth - 1 : availableWidth;
454
+ const halfWidth = Math.floor(scrollWidth / 2);
455
+
456
+ const findValidStart = (start: number) => {
457
+ while (start < this.value.length) {
458
+ const charCode = this.value.charCodeAt(start);
459
+ // this is low surrogate, not a valid start
460
+ if (charCode >= 0xdc00 && charCode < 0xe000) {
461
+ start++;
462
+ continue;
463
+ }
464
+ break;
465
+ }
466
+ return start;
467
+ };
468
+
469
+ const findValidEnd = (end: number) => {
470
+ while (end > 0) {
471
+ const charCode = this.value.charCodeAt(end - 1);
472
+ // this is high surrogate, might be split.
473
+ if (charCode >= 0xd800 && charCode < 0xdc00) {
474
+ end--;
475
+ continue;
476
+ }
477
+ break;
478
+ }
479
+ return end;
480
+ };
481
+
482
+ if (this.cursor < halfWidth) {
483
+ // Cursor near start
484
+ visibleText = this.value.slice(0, findValidEnd(scrollWidth));
485
+ cursorDisplay = this.cursor;
486
+ } else if (this.cursor > this.value.length - halfWidth) {
487
+ // Cursor near end
488
+ const start = findValidStart(this.value.length - scrollWidth);
489
+ visibleText = this.value.slice(start);
490
+ cursorDisplay = this.cursor - start;
491
+ } else {
492
+ // Cursor in middle
493
+ const start = findValidStart(this.cursor - halfWidth);
494
+ visibleText = this.value.slice(start, findValidEnd(start + scrollWidth));
495
+ cursorDisplay = halfWidth;
496
+ }
497
+ }
498
+
499
+ // Build line with fake cursor
500
+ // Insert cursor character at cursor position
501
+ const graphemes = [...segmenter.segment(visibleText.slice(cursorDisplay))];
502
+ const cursorGrapheme = graphemes[0];
503
+
504
+ const beforeCursor = visibleText.slice(0, cursorDisplay);
505
+ const atCursor = cursorGrapheme?.segment ?? " "; // Character at cursor, or space if at end
506
+ const afterCursor = visibleText.slice(cursorDisplay + atCursor.length);
507
+
508
+ // Hardware cursor marker (zero-width, emitted before fake cursor for IME positioning)
509
+ const marker = this.focused ? CURSOR_MARKER : "";
510
+
511
+ // Use inverse video to show cursor
512
+ const cursorChar = `\x1b[7m${atCursor}\x1b[27m`; // ESC[7m = reverse video, ESC[27m = normal
513
+ const textWithCursor = beforeCursor + marker + cursorChar + afterCursor;
514
+
515
+ // Calculate visual width
516
+ const visualLength = visibleWidth(textWithCursor);
517
+ const padding = " ".repeat(Math.max(0, availableWidth - visualLength));
518
+ const line = prompt + textWithCursor + padding;
519
+
520
+ return [line];
521
+ }
522
+ }
@@ -0,0 +1,187 @@
1
+ import type { TUI } from "../tui.js";
2
+ import { Text } from "./text.js";
3
+
4
+ /** Default braille spinner frames used when no custom frames are provided. */
5
+ const DEFAULT_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
6
+
7
+ /** Default animation interval in milliseconds. */
8
+ const DEFAULT_INTERVAL_MS = 80;
9
+
10
+ /** Optional configuration for the Loader's spinner appearance and timing. */
11
+ export interface LoaderOptions {
12
+ /** Spinner animation frames (defaults to braille dots). */
13
+ frames?: string[];
14
+ /** Animation interval in ms (default: 80). */
15
+ intervalMs?: number;
16
+ }
17
+
18
+ /** Context passed to the message transform callback each tick. */
19
+ export interface MessageTransformContext {
20
+ /** The current message set on this Loader (from constructor or setMessage). */
21
+ message: string;
22
+ /** Monotonic tick counter — increments each animation frame for this instance. */
23
+ tick: number;
24
+ /** True if the message has not been changed via setMessage() since construction. */
25
+ isInitialMessage: boolean;
26
+ }
27
+
28
+ /**
29
+ * Loader component that updates with a spinning animation.
30
+ *
31
+ * @param ui - TUI instance for rendering
32
+ * @param spinnerColorFn - Color function applied to the spinner frame
33
+ * @param messageColorFn - Color function applied to the message text
34
+ * @param message - Text shown next to the spinner (default: "Loading...")
35
+ * @param options - Optional frames and interval override
36
+ */
37
+ export class Loader extends Text {
38
+ private frames: string[];
39
+ private intervalMs: number;
40
+ private currentFrame = 0;
41
+ private intervalId: NodeJS.Timeout | null = null;
42
+ private _transformIntervalId: NodeJS.Timeout | null = null;
43
+ private ui: TUI | null = null;
44
+
45
+ /** Per-instance tick counter for message transform animations. */
46
+ private _transformTick = 0;
47
+
48
+ /** Tracks whether setMessage() has been called since construction. */
49
+ private _messageChanged = false;
50
+
51
+ constructor(
52
+ ui: TUI,
53
+ private spinnerColorFn: (str: string) => string,
54
+ private messageColorFn: (str: string) => string,
55
+ private message: string = "Loading...",
56
+ options?: LoaderOptions
57
+ ) {
58
+ super("", 1, 0);
59
+ this.frames = options?.frames ?? Loader.defaultFrames ?? DEFAULT_FRAMES;
60
+ this.intervalMs = options?.intervalMs ?? Loader.defaultIntervalMs ?? DEFAULT_INTERVAL_MS;
61
+ this.ui = ui;
62
+ this.start();
63
+ }
64
+
65
+ /**
66
+ * Global default spinner frames — set once, applies to all new Loader instances.
67
+ * Extensions can set this at session_start to override the braille default.
68
+ */
69
+ static defaultFrames: string[] | undefined;
70
+
71
+ /**
72
+ * Global default interval — set once, applies to all new Loader instances.
73
+ */
74
+ static defaultIntervalMs: number | undefined;
75
+
76
+ /**
77
+ * Global message transform — called each tick to modify the displayed message.
78
+ * Extensions use this to animate or replace the loader text.
79
+ * Return the string to display. The transform is applied before messageColorFn.
80
+ */
81
+ static defaultMessageTransform?: (ctx: MessageTransformContext) => string;
82
+
83
+ /**
84
+ * Interval (ms) for the message transform tick — independent of the spinner frame rate.
85
+ * When set to a value faster than the spinner interval, a separate timer drives
86
+ * the transform tick and re-renders, giving animations higher
87
+ * frame rates without affecting the spinner animation speed.
88
+ */
89
+ static defaultTransformIntervalMs: number | undefined;
90
+
91
+ /** Whether the loader is hidden (renders as empty space). */
92
+ private hidden = false;
93
+
94
+ /**
95
+ * Hide the loader — renders nothing but keeps the interval alive.
96
+ * Call show() to restore.
97
+ */
98
+ hide() {
99
+ this.hidden = true;
100
+ this.setText("");
101
+ this.ui?.requestRender();
102
+ }
103
+
104
+ /**
105
+ * Show the loader after a hide() call.
106
+ */
107
+ show() {
108
+ this.hidden = false;
109
+ this.updateDisplay();
110
+ }
111
+
112
+ render(width: number): string[] {
113
+ if (this.hidden) return [];
114
+ return ["", ...super.render(width)];
115
+ }
116
+
117
+ start() {
118
+ this.updateDisplay();
119
+
120
+ const transformMs = Loader.defaultTransformIntervalMs;
121
+ const hasFastTransform =
122
+ Loader.defaultMessageTransform != null &&
123
+ transformMs != null &&
124
+ transformMs < this.intervalMs;
125
+
126
+ this.intervalId = setInterval(() => {
127
+ this.currentFrame = (this.currentFrame + 1) % this.frames.length;
128
+ this._transformTick++;
129
+ this.updateDisplay();
130
+ }, this.intervalMs);
131
+
132
+ // Separate faster interval for message transform re-renders only.
133
+ // Does NOT advance _transformTick — just re-rolls random visuals.
134
+ if (hasFastTransform) {
135
+ this._transformIntervalId = setInterval(() => {
136
+ this.updateDisplay();
137
+ }, transformMs);
138
+ }
139
+ }
140
+
141
+ stop() {
142
+ if (this.intervalId) {
143
+ clearInterval(this.intervalId);
144
+ this.intervalId = null;
145
+ }
146
+ if (this._transformIntervalId) {
147
+ clearInterval(this._transformIntervalId);
148
+ this._transformIntervalId = null;
149
+ }
150
+ }
151
+
152
+ /** Sentinel value — pass to setWorkingMessage() to hide the loader. */
153
+ static readonly HIDE = "\u200B";
154
+
155
+ /**
156
+ * Set the loader message. Pass Loader.HIDE to hide, any other string to show.
157
+ * @param message - Message text or Loader.HIDE sentinel
158
+ */
159
+ setMessage(message: string) {
160
+ if (message === Loader.HIDE) {
161
+ this.hide();
162
+ return;
163
+ }
164
+ if (this.hidden) this.show();
165
+ this._messageChanged = true;
166
+ this.message = message;
167
+ this.updateDisplay();
168
+ }
169
+
170
+ private updateDisplay() {
171
+ const frame = this.frames[this.currentFrame];
172
+ let displayMessage = this.message;
173
+
174
+ if (Loader.defaultMessageTransform) {
175
+ displayMessage = Loader.defaultMessageTransform({
176
+ message: this.message,
177
+ tick: this._transformTick,
178
+ isInitialMessage: !this._messageChanged,
179
+ });
180
+ }
181
+
182
+ this.setText(`${this.spinnerColorFn(frame)} ${this.messageColorFn(displayMessage)}`);
183
+ if (this.ui) {
184
+ this.ui.requestRender();
185
+ }
186
+ }
187
+ }