@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,350 @@
1
+ import * as fs from "node:fs";
2
+ import { setKittyProtocolActive } from "./keys.js";
3
+ import { StdinBuffer } from "./stdin-buffer.js";
4
+
5
+ /**
6
+ * Minimal terminal interface for TUI
7
+ */
8
+ export interface Terminal {
9
+ // Start the terminal with input and resize handlers
10
+ start(onInput: (data: string) => void, onResize: () => void): void;
11
+
12
+ // Stop the terminal and restore state
13
+ stop(): void;
14
+
15
+ /**
16
+ * Drain stdin before exiting to prevent Kitty key release events from
17
+ * leaking to the parent shell over slow SSH connections.
18
+ * @param maxMs - Maximum time to drain (default: 1000ms)
19
+ * @param idleMs - Exit early if no input arrives within this time (default: 50ms)
20
+ */
21
+ drainInput(maxMs?: number, idleMs?: number): Promise<void>;
22
+
23
+ // Write output to terminal
24
+ write(data: string): void;
25
+
26
+ // Get terminal dimensions
27
+ get columns(): number;
28
+ get rows(): number;
29
+
30
+ // Whether Kitty keyboard protocol is active
31
+ get kittyProtocolActive(): boolean;
32
+
33
+ // Cursor positioning (relative to current position)
34
+ moveBy(lines: number): void; // Move cursor up (negative) or down (positive) by N lines
35
+
36
+ // Cursor visibility
37
+ hideCursor(): void; // Hide the cursor
38
+ showCursor(): void; // Show the cursor
39
+
40
+ // Clear operations
41
+ clearLine(): void; // Clear current line
42
+ clearFromCursor(): void; // Clear from cursor to end of screen
43
+ clearScreen(): void; // Clear entire screen and move cursor to (0,0)
44
+
45
+ // Screen buffer mode
46
+ enterAlternateScreen(): void; // Switch to alternate screen buffer (no scrollback)
47
+ leaveAlternateScreen(): void; // Restore normal screen buffer and scrollback
48
+
49
+ // Title operations
50
+ setTitle(title: string): void; // Set terminal window title
51
+
52
+ /** Set terminal progress bar via OSC 9;4. Supported by Windows Terminal, iTerm2, ConEmu. */
53
+ setProgress(percent: number): void;
54
+ /** Clear terminal progress bar via OSC 9;4. */
55
+ clearProgress(): void;
56
+ }
57
+
58
+ /**
59
+ * Real terminal using process.stdin/stdout
60
+ */
61
+ export class ProcessTerminal implements Terminal {
62
+ private wasRaw = false;
63
+ private inputHandler?: (data: string) => void;
64
+ private resizeHandler?: () => void;
65
+ private _kittyProtocolActive = false;
66
+ private stdinBuffer?: StdinBuffer;
67
+ private stdinDataHandler?: (data: string) => void;
68
+ private writeLogPath = process.env.PI_TUI_WRITE_LOG || "";
69
+ private alternateScreenActive = false;
70
+
71
+ get kittyProtocolActive(): boolean {
72
+ return this._kittyProtocolActive;
73
+ }
74
+
75
+ start(onInput: (data: string) => void, onResize: () => void): void {
76
+ this.inputHandler = onInput;
77
+ this.resizeHandler = onResize;
78
+ this.alternateScreenActive = false;
79
+
80
+ // Save previous state and enable raw mode
81
+ this.wasRaw = process.stdin.isRaw || false;
82
+ if (process.stdin.setRawMode) {
83
+ process.stdin.setRawMode(true);
84
+ }
85
+ process.stdin.setEncoding("utf8");
86
+ process.stdin.resume();
87
+
88
+ // Enable bracketed paste mode - terminal will wrap pastes in \x1b[200~ ... \x1b[201~
89
+ process.stdout.write("\x1b[?2004h");
90
+
91
+ // Set up resize handler immediately
92
+ process.stdout.on("resize", this.resizeHandler);
93
+
94
+ // Refresh terminal dimensions - they may be stale after suspend/resume
95
+ // (SIGWINCH is lost while process is stopped). Unix only.
96
+ if (process.platform !== "win32") {
97
+ process.kill(process.pid, "SIGWINCH");
98
+ }
99
+
100
+ // Query and enable Kitty keyboard protocol
101
+ // The query handler intercepts input temporarily, then installs the user's handler
102
+ // See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/
103
+ this.queryAndEnableKittyProtocol();
104
+ }
105
+
106
+ /**
107
+ * Set up StdinBuffer to split batched input into individual sequences.
108
+ * This ensures components receive single events, making matchesKey/isKeyRelease work correctly.
109
+ *
110
+ * Also watches for Kitty protocol response and enables it when detected.
111
+ * This is done here (after stdinBuffer parsing) rather than on raw stdin
112
+ * to handle the case where the response arrives split across multiple events.
113
+ */
114
+ private setupStdinBuffer(): void {
115
+ this.stdinBuffer = new StdinBuffer({ timeout: 10 });
116
+
117
+ // Kitty protocol response pattern: \x1b[?<flags>u
118
+ const kittyResponsePattern = /^\x1b\[\?(\d+)u$/;
119
+
120
+ // Forward individual sequences to the input handler
121
+ this.stdinBuffer.on("data", (sequence) => {
122
+ // Check for Kitty protocol response (only if not already enabled)
123
+ if (!this._kittyProtocolActive) {
124
+ const match = sequence.match(kittyResponsePattern);
125
+ if (match) {
126
+ this._kittyProtocolActive = true;
127
+ setKittyProtocolActive(true);
128
+
129
+ // Enable Kitty keyboard protocol (push flags)
130
+ // Flag 1 = disambiguate escape codes
131
+ // Flag 2 = report event types (press/repeat/release)
132
+ // Flag 4 = report alternate keys (shifted key, base layout key)
133
+ // Base layout key enables shortcuts to work with non-Latin keyboard layouts
134
+ process.stdout.write("\x1b[>7u");
135
+ return; // Don't forward protocol response to TUI
136
+ }
137
+ }
138
+
139
+ if (this.inputHandler) {
140
+ this.inputHandler(sequence);
141
+ }
142
+ });
143
+
144
+ // Re-wrap paste content with bracketed paste markers for existing editor handling
145
+ this.stdinBuffer.on("paste", (content) => {
146
+ if (this.inputHandler) {
147
+ this.inputHandler(`\x1b[200~${content}\x1b[201~`);
148
+ }
149
+ });
150
+
151
+ // Handler that pipes stdin data through the buffer
152
+ this.stdinDataHandler = (data: string) => {
153
+ this.stdinBuffer?.process(data);
154
+ };
155
+ }
156
+
157
+ /**
158
+ * Query terminal for Kitty keyboard protocol support and enable if available.
159
+ *
160
+ * Sends CSI ? u to query current flags. If terminal responds with CSI ? <flags> u,
161
+ * it supports the protocol and we enable it with CSI > 1 u.
162
+ *
163
+ * The response is detected in setupStdinBuffer's data handler, which properly
164
+ * handles the case where the response arrives split across multiple stdin events.
165
+ */
166
+ private queryAndEnableKittyProtocol(): void {
167
+ this.setupStdinBuffer();
168
+ process.stdin.on("data", this.stdinDataHandler!);
169
+ process.stdout.write("\x1b[?u");
170
+ }
171
+
172
+ async drainInput(maxMs = 1000, idleMs = 50): Promise<void> {
173
+ if (this._kittyProtocolActive) {
174
+ // Disable Kitty keyboard protocol first so any late key releases
175
+ // do not generate new Kitty escape sequences.
176
+ process.stdout.write("\x1b[<u");
177
+ this._kittyProtocolActive = false;
178
+ setKittyProtocolActive(false);
179
+ }
180
+
181
+ const previousHandler = this.inputHandler;
182
+ this.inputHandler = undefined;
183
+
184
+ let lastDataTime = Date.now();
185
+ const onData = () => {
186
+ lastDataTime = Date.now();
187
+ };
188
+
189
+ process.stdin.on("data", onData);
190
+ const endTime = Date.now() + maxMs;
191
+
192
+ try {
193
+ while (true) {
194
+ const now = Date.now();
195
+ const timeLeft = endTime - now;
196
+ if (timeLeft <= 0) break;
197
+ if (now - lastDataTime >= idleMs) break;
198
+ await new Promise((resolve) => setTimeout(resolve, Math.min(idleMs, timeLeft)));
199
+ }
200
+ } finally {
201
+ process.stdin.removeListener("data", onData);
202
+ this.inputHandler = previousHandler;
203
+ }
204
+ }
205
+
206
+ stop(): void {
207
+ // Always restore normal screen buffer before exiting.
208
+ if (this.alternateScreenActive) {
209
+ this.leaveAlternateScreen();
210
+ }
211
+
212
+ // Disable bracketed paste mode
213
+ process.stdout.write("\x1b[?2004l");
214
+
215
+ // Disable Kitty keyboard protocol if not already done by drainInput()
216
+ if (this._kittyProtocolActive) {
217
+ process.stdout.write("\x1b[<u");
218
+ this._kittyProtocolActive = false;
219
+ setKittyProtocolActive(false);
220
+ }
221
+
222
+ // Clean up StdinBuffer
223
+ if (this.stdinBuffer) {
224
+ this.stdinBuffer.destroy();
225
+ this.stdinBuffer = undefined;
226
+ }
227
+
228
+ // Remove event handlers
229
+ if (this.stdinDataHandler) {
230
+ process.stdin.removeListener("data", this.stdinDataHandler);
231
+ this.stdinDataHandler = undefined;
232
+ }
233
+ this.inputHandler = undefined;
234
+ if (this.resizeHandler) {
235
+ process.stdout.removeListener("resize", this.resizeHandler);
236
+ this.resizeHandler = undefined;
237
+ }
238
+
239
+ // Pause stdin to prevent any buffered input (e.g., Ctrl+D) from being
240
+ // re-interpreted after raw mode is disabled. This fixes a race condition
241
+ // where Ctrl+D could close the parent shell over SSH.
242
+ process.stdin.pause();
243
+
244
+ // Restore raw mode state
245
+ if (process.stdin.setRawMode) {
246
+ process.stdin.setRawMode(this.wasRaw);
247
+ }
248
+ }
249
+
250
+ write(data: string): void {
251
+ process.stdout.write(data);
252
+ if (this.writeLogPath) {
253
+ try {
254
+ fs.appendFileSync(this.writeLogPath, data, { encoding: "utf8" });
255
+ } catch {
256
+ // Ignore logging errors
257
+ }
258
+ }
259
+ }
260
+
261
+ get columns(): number {
262
+ return process.stdout.columns || 80;
263
+ }
264
+
265
+ get rows(): number {
266
+ return process.stdout.rows || 24;
267
+ }
268
+
269
+ moveBy(lines: number): void {
270
+ if (lines > 0) {
271
+ // Move down
272
+ process.stdout.write(`\x1b[${lines}B`);
273
+ } else if (lines < 0) {
274
+ // Move up
275
+ process.stdout.write(`\x1b[${-lines}A`);
276
+ }
277
+ // lines === 0: no movement
278
+ }
279
+
280
+ hideCursor(): void {
281
+ process.stdout.write("\x1b[?25l");
282
+ }
283
+
284
+ showCursor(): void {
285
+ process.stdout.write("\x1b[?25h");
286
+ }
287
+
288
+ clearLine(): void {
289
+ process.stdout.write("\x1b[K");
290
+ }
291
+
292
+ clearFromCursor(): void {
293
+ process.stdout.write("\x1b[J");
294
+ }
295
+
296
+ clearScreen(): void {
297
+ process.stdout.write("\x1b[2J\x1b[H"); // Clear screen and move to home (1,1)
298
+ }
299
+
300
+ /**
301
+ * Switch to alternate screen buffer and clear it.
302
+ * @returns void
303
+ */
304
+ enterAlternateScreen(): void {
305
+ if (this.alternateScreenActive) return;
306
+ process.stdout.write("\x1b[?1049h");
307
+ this.alternateScreenActive = true;
308
+ }
309
+
310
+ /**
311
+ * Restore normal screen buffer and previous scrollback.
312
+ * @returns void
313
+ */
314
+ leaveAlternateScreen(): void {
315
+ if (!this.alternateScreenActive) return;
316
+ process.stdout.write("\x1b[?1049l");
317
+ this.alternateScreenActive = false;
318
+ }
319
+
320
+ setTitle(title: string): void {
321
+ // OSC 0;title BEL - set terminal window title
322
+ process.stdout.write(`\x1b]0;${title}\x07`);
323
+ }
324
+
325
+ private lastProgressWrite = 0;
326
+
327
+ /**
328
+ * Set terminal progress bar via OSC 9;4.
329
+ * Throttled to max 1 update per 100ms (percent=100 always passes through).
330
+ * Supported by Windows Terminal (tab indicator), iTerm2 (title bar), ConEmu (taskbar).
331
+ * Unsupported terminals gracefully ignore these sequences.
332
+ * @param percent - Progress percentage, clamped to 0-100
333
+ */
334
+ setProgress(percent: number): void {
335
+ const clamped = Math.max(0, Math.min(100, Math.round(percent)));
336
+ const now = Date.now();
337
+ if (clamped !== 100 && now - this.lastProgressWrite < 100) return;
338
+ this.lastProgressWrite = now;
339
+ process.stdout.write(`\x1b]9;4;1;${clamped}\x07`);
340
+ }
341
+
342
+ /**
343
+ * Clear terminal progress bar via OSC 9;4.
344
+ * Resets the throttle timestamp so a subsequent setProgress fires immediately.
345
+ */
346
+ clearProgress(): void {
347
+ this.lastProgressWrite = 0;
348
+ process.stdout.write("\x1b]9;4;0;0\x07");
349
+ }
350
+ }
@@ -0,0 +1,56 @@
1
+ import { resetCapabilitiesCache } from "../terminal-image.js";
2
+
3
+ const CAPABILITY_ENV_KEYS = [
4
+ "COLORTERM",
5
+ "GHOSTTY_RESOURCES_DIR",
6
+ "ITERM_SESSION_ID",
7
+ "KITTY_WINDOW_ID",
8
+ "TERM",
9
+ "TERM_PROGRAM",
10
+ "WEZTERM_PANE",
11
+ ] as const;
12
+
13
+ type CapabilityEnvOverrides = Readonly<Record<string, string | undefined>>;
14
+
15
+ /**
16
+ * Run a callback with controlled terminal capability environment variables.
17
+ *
18
+ * Resets the terminal capability cache before and after the callback so test
19
+ * assertions always reflect the requested environment.
20
+ *
21
+ * @param overrides - Temporary environment variable overrides
22
+ * @param run - Callback executed with overrides applied
23
+ * @returns Callback return value
24
+ */
25
+ export function withCapabilityEnv<T>(overrides: CapabilityEnvOverrides, run: () => T): T {
26
+ const previous: Partial<Record<(typeof CAPABILITY_ENV_KEYS)[number], string | undefined>> = {};
27
+
28
+ for (const key of CAPABILITY_ENV_KEYS) {
29
+ previous[key] = process.env[key];
30
+ if (Object.hasOwn(overrides, key)) {
31
+ const value = overrides[key];
32
+ if (value === undefined) {
33
+ delete process.env[key];
34
+ } else {
35
+ process.env[key] = value;
36
+ }
37
+ } else {
38
+ delete process.env[key];
39
+ }
40
+ }
41
+
42
+ resetCapabilitiesCache();
43
+ try {
44
+ return run();
45
+ } finally {
46
+ for (const key of CAPABILITY_ENV_KEYS) {
47
+ const value = previous[key];
48
+ if (value === undefined) {
49
+ delete process.env[key];
50
+ } else {
51
+ process.env[key] = value;
52
+ }
53
+ }
54
+ resetCapabilitiesCache();
55
+ }
56
+ }