@juliusbrussee/caveman-tui 0.65.2

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 (162) hide show
  1. package/README.md +767 -0
  2. package/dist/autocomplete.d.ts +52 -0
  3. package/dist/autocomplete.d.ts.map +1 -0
  4. package/dist/autocomplete.js +623 -0
  5. package/dist/autocomplete.js.map +1 -0
  6. package/dist/chord.d.ts +57 -0
  7. package/dist/chord.d.ts.map +1 -0
  8. package/dist/chord.js +97 -0
  9. package/dist/chord.js.map +1 -0
  10. package/dist/color-depth.d.ts +17 -0
  11. package/dist/color-depth.d.ts.map +1 -0
  12. package/dist/color-depth.js +147 -0
  13. package/dist/color-depth.js.map +1 -0
  14. package/dist/components/Chapters.d.ts +41 -0
  15. package/dist/components/Chapters.d.ts.map +1 -0
  16. package/dist/components/Chapters.js +103 -0
  17. package/dist/components/Chapters.js.map +1 -0
  18. package/dist/components/DiffView.d.ts +75 -0
  19. package/dist/components/DiffView.d.ts.map +1 -0
  20. package/dist/components/DiffView.js +170 -0
  21. package/dist/components/DiffView.js.map +1 -0
  22. package/dist/components/StatusLine.d.ts +135 -0
  23. package/dist/components/StatusLine.d.ts.map +1 -0
  24. package/dist/components/StatusLine.js +133 -0
  25. package/dist/components/StatusLine.js.map +1 -0
  26. package/dist/components/SubagentOverlay.d.ts +63 -0
  27. package/dist/components/SubagentOverlay.d.ts.map +1 -0
  28. package/dist/components/SubagentOverlay.js +124 -0
  29. package/dist/components/SubagentOverlay.js.map +1 -0
  30. package/dist/components/box.d.ts +22 -0
  31. package/dist/components/box.d.ts.map +1 -0
  32. package/dist/components/box.js +104 -0
  33. package/dist/components/box.js.map +1 -0
  34. package/dist/components/cancellable-loader.d.ts +22 -0
  35. package/dist/components/cancellable-loader.d.ts.map +1 -0
  36. package/dist/components/cancellable-loader.js +35 -0
  37. package/dist/components/cancellable-loader.js.map +1 -0
  38. package/dist/components/editor.d.ts +244 -0
  39. package/dist/components/editor.d.ts.map +1 -0
  40. package/dist/components/editor.js +1861 -0
  41. package/dist/components/editor.js.map +1 -0
  42. package/dist/components/grouped-select-list.d.ts +60 -0
  43. package/dist/components/grouped-select-list.d.ts.map +1 -0
  44. package/dist/components/grouped-select-list.js +312 -0
  45. package/dist/components/grouped-select-list.js.map +1 -0
  46. package/dist/components/image.d.ts +28 -0
  47. package/dist/components/image.d.ts.map +1 -0
  48. package/dist/components/image.js +69 -0
  49. package/dist/components/image.js.map +1 -0
  50. package/dist/components/input.d.ts +37 -0
  51. package/dist/components/input.d.ts.map +1 -0
  52. package/dist/components/input.js +426 -0
  53. package/dist/components/input.js.map +1 -0
  54. package/dist/components/loader.d.ts +26 -0
  55. package/dist/components/loader.d.ts.map +1 -0
  56. package/dist/components/loader.js +67 -0
  57. package/dist/components/loader.js.map +1 -0
  58. package/dist/components/markdown.d.ts +95 -0
  59. package/dist/components/markdown.d.ts.map +1 -0
  60. package/dist/components/markdown.js +663 -0
  61. package/dist/components/markdown.js.map +1 -0
  62. package/dist/components/select-list.d.ts +50 -0
  63. package/dist/components/select-list.d.ts.map +1 -0
  64. package/dist/components/select-list.js +159 -0
  65. package/dist/components/select-list.js.map +1 -0
  66. package/dist/components/settings-list.d.ts +50 -0
  67. package/dist/components/settings-list.d.ts.map +1 -0
  68. package/dist/components/settings-list.js +185 -0
  69. package/dist/components/settings-list.js.map +1 -0
  70. package/dist/components/spacer.d.ts +12 -0
  71. package/dist/components/spacer.d.ts.map +1 -0
  72. package/dist/components/spacer.js +23 -0
  73. package/dist/components/spacer.js.map +1 -0
  74. package/dist/components/spinner.d.ts +35 -0
  75. package/dist/components/spinner.d.ts.map +1 -0
  76. package/dist/components/spinner.js +77 -0
  77. package/dist/components/spinner.js.map +1 -0
  78. package/dist/components/streaming-markdown.d.ts +39 -0
  79. package/dist/components/streaming-markdown.d.ts.map +1 -0
  80. package/dist/components/streaming-markdown.js +137 -0
  81. package/dist/components/streaming-markdown.js.map +1 -0
  82. package/dist/components/text.d.ts +19 -0
  83. package/dist/components/text.d.ts.map +1 -0
  84. package/dist/components/text.js +89 -0
  85. package/dist/components/text.js.map +1 -0
  86. package/dist/components/truncated-text.d.ts +13 -0
  87. package/dist/components/truncated-text.d.ts.map +1 -0
  88. package/dist/components/truncated-text.js +51 -0
  89. package/dist/components/truncated-text.js.map +1 -0
  90. package/dist/editor-component.d.ts +39 -0
  91. package/dist/editor-component.d.ts.map +1 -0
  92. package/dist/editor-component.js +2 -0
  93. package/dist/editor-component.js.map +1 -0
  94. package/dist/fuzzy.d.ts +16 -0
  95. package/dist/fuzzy.d.ts.map +1 -0
  96. package/dist/fuzzy.js +107 -0
  97. package/dist/fuzzy.js.map +1 -0
  98. package/dist/index.d.ts +38 -0
  99. package/dist/index.d.ts.map +1 -0
  100. package/dist/index.js +59 -0
  101. package/dist/index.js.map +1 -0
  102. package/dist/keybindings.d.ts +193 -0
  103. package/dist/keybindings.d.ts.map +1 -0
  104. package/dist/keybindings.js +174 -0
  105. package/dist/keybindings.js.map +1 -0
  106. package/dist/keys.d.ts +170 -0
  107. package/dist/keys.d.ts.map +1 -0
  108. package/dist/keys.js +1124 -0
  109. package/dist/keys.js.map +1 -0
  110. package/dist/kill-ring.d.ts +28 -0
  111. package/dist/kill-ring.d.ts.map +1 -0
  112. package/dist/kill-ring.js +44 -0
  113. package/dist/kill-ring.js.map +1 -0
  114. package/dist/notifications.d.ts +35 -0
  115. package/dist/notifications.d.ts.map +1 -0
  116. package/dist/notifications.js +62 -0
  117. package/dist/notifications.js.map +1 -0
  118. package/dist/osc52.d.ts +28 -0
  119. package/dist/osc52.d.ts.map +1 -0
  120. package/dist/osc52.js +53 -0
  121. package/dist/osc52.js.map +1 -0
  122. package/dist/scroll-buffer.d.ts +67 -0
  123. package/dist/scroll-buffer.d.ts.map +1 -0
  124. package/dist/scroll-buffer.js +222 -0
  125. package/dist/scroll-buffer.js.map +1 -0
  126. package/dist/spinners.d.ts +26 -0
  127. package/dist/spinners.d.ts.map +1 -0
  128. package/dist/spinners.js +136 -0
  129. package/dist/spinners.js.map +1 -0
  130. package/dist/stdin-buffer.d.ts +48 -0
  131. package/dist/stdin-buffer.d.ts.map +1 -0
  132. package/dist/stdin-buffer.js +317 -0
  133. package/dist/stdin-buffer.js.map +1 -0
  134. package/dist/sync-output.d.ts +58 -0
  135. package/dist/sync-output.d.ts.map +1 -0
  136. package/dist/sync-output.js +79 -0
  137. package/dist/sync-output.js.map +1 -0
  138. package/dist/terminal-detect.d.ts +66 -0
  139. package/dist/terminal-detect.d.ts.map +1 -0
  140. package/dist/terminal-detect.js +315 -0
  141. package/dist/terminal-detect.js.map +1 -0
  142. package/dist/terminal-image.d.ts +68 -0
  143. package/dist/terminal-image.d.ts.map +1 -0
  144. package/dist/terminal-image.js +288 -0
  145. package/dist/terminal-image.js.map +1 -0
  146. package/dist/terminal.d.ts +105 -0
  147. package/dist/terminal.d.ts.map +1 -0
  148. package/dist/terminal.js +427 -0
  149. package/dist/terminal.js.map +1 -0
  150. package/dist/tui.d.ts +268 -0
  151. package/dist/tui.d.ts.map +1 -0
  152. package/dist/tui.js +1161 -0
  153. package/dist/tui.js.map +1 -0
  154. package/dist/undo-stack.d.ts +17 -0
  155. package/dist/undo-stack.d.ts.map +1 -0
  156. package/dist/undo-stack.js +25 -0
  157. package/dist/undo-stack.js.map +1 -0
  158. package/dist/utils.d.ts +78 -0
  159. package/dist/utils.d.ts.map +1 -0
  160. package/dist/utils.js +960 -0
  161. package/dist/utils.js.map +1 -0
  162. package/package.json +59 -0
@@ -0,0 +1,427 @@
1
+ import * as fs from "node:fs";
2
+ import { createRequire } from "node:module";
3
+ import * as path from "node:path";
4
+ import { setKittyProtocolActive } from "./keys.js";
5
+ import { StdinBuffer } from "./stdin-buffer.js";
6
+ const cjsRequire = createRequire(import.meta.url);
7
+ /**
8
+ * Real terminal using process.stdin/stdout
9
+ */
10
+ export class ProcessTerminal {
11
+ wasRaw = false;
12
+ inputHandler;
13
+ resizeHandler;
14
+ _kittyProtocolActive = false;
15
+ _modifyOtherKeysActive = false;
16
+ _altScreenActive = false;
17
+ _mouseTrackingActive = false;
18
+ stdinBuffer;
19
+ stdinDataHandler;
20
+ oscListeners = [];
21
+ signalHandlers = [];
22
+ uncaughtExceptionHandler;
23
+ unhandledRejectionHandler;
24
+ writeLogPath = (() => {
25
+ const env = process.env.PI_TUI_WRITE_LOG || "";
26
+ if (!env)
27
+ return "";
28
+ try {
29
+ if (fs.statSync(env).isDirectory()) {
30
+ const now = new Date();
31
+ const ts = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}_${String(now.getHours()).padStart(2, "0")}-${String(now.getMinutes()).padStart(2, "0")}-${String(now.getSeconds()).padStart(2, "0")}`;
32
+ return path.join(env, `tui-${ts}-${process.pid}.log`);
33
+ }
34
+ }
35
+ catch {
36
+ // Not an existing directory - use as-is (file path)
37
+ }
38
+ return env;
39
+ })();
40
+ get kittyProtocolActive() {
41
+ return this._kittyProtocolActive;
42
+ }
43
+ isTTY() {
44
+ return Boolean(process.stdout.isTTY);
45
+ }
46
+ enterAltScreen() {
47
+ if (this._altScreenActive || !this.isTTY())
48
+ return;
49
+ // CSI ?1049h: save primary screen, enter alt screen, position cursor at 1,1
50
+ // CSI 2J: clear visible area
51
+ // CSI H: move cursor home
52
+ // CSI ?25l: hide cursor
53
+ process.stdout.write("\x1b[?1049h\x1b[2J\x1b[H\x1b[?25l");
54
+ this._altScreenActive = true;
55
+ }
56
+ leaveAltScreen() {
57
+ if (!this._altScreenActive)
58
+ return;
59
+ // CSI ?25h: show cursor
60
+ // CSI ?1049l: leave alt screen, restore primary
61
+ process.stdout.write("\x1b[?25h\x1b[?1049l");
62
+ this._altScreenActive = false;
63
+ }
64
+ enableMouseTracking() {
65
+ if (this._mouseTrackingActive || !this.isTTY())
66
+ return;
67
+ // CSI ?1000h: send mouse press/release events
68
+ // CSI ?1006h: SGR-encoded mouse events (wider coordinate range)
69
+ process.stdout.write("\x1b[?1000h\x1b[?1006h");
70
+ this._mouseTrackingActive = true;
71
+ }
72
+ disableMouseTracking() {
73
+ if (!this._mouseTrackingActive)
74
+ return;
75
+ process.stdout.write("\x1b[?1006l\x1b[?1000l");
76
+ this._mouseTrackingActive = false;
77
+ }
78
+ async queryOsc(sequence, responsePrefix, timeoutMs) {
79
+ if (!this.isTTY())
80
+ return null;
81
+ return new Promise((resolve) => {
82
+ const timer = setTimeout(() => {
83
+ const idx = this.oscListeners.findIndex((l) => l.resolve === resolve);
84
+ if (idx !== -1)
85
+ this.oscListeners.splice(idx, 1);
86
+ resolve(null);
87
+ }, timeoutMs);
88
+ this.oscListeners.push({ prefix: responsePrefix, resolve, timer });
89
+ process.stdout.write(sequence);
90
+ });
91
+ }
92
+ handleOscResponse(data) {
93
+ for (let i = 0; i < this.oscListeners.length; i++) {
94
+ if (data.includes(this.oscListeners[i].prefix)) {
95
+ const { resolve, timer } = this.oscListeners[i];
96
+ clearTimeout(timer);
97
+ this.oscListeners.splice(i, 1);
98
+ resolve(data);
99
+ return true;
100
+ }
101
+ }
102
+ return false;
103
+ }
104
+ installSignalHandlers() {
105
+ const teardown = (exitCode) => {
106
+ try {
107
+ this.stop();
108
+ }
109
+ catch {
110
+ // best-effort
111
+ }
112
+ if (exitCode !== null) {
113
+ process.exit(exitCode);
114
+ }
115
+ };
116
+ const signals = ["SIGINT", "SIGTERM", "SIGHUP"];
117
+ const signalCodes = { SIGINT: 130, SIGTERM: 143, SIGHUP: 129 };
118
+ for (const sig of signals) {
119
+ const handler = () => teardown(signalCodes[sig] ?? 128);
120
+ process.on(sig, handler);
121
+ this.signalHandlers.push({ signal: sig, handler });
122
+ }
123
+ this.uncaughtExceptionHandler = (err) => {
124
+ try {
125
+ this.stop();
126
+ }
127
+ catch { }
128
+ // Log after teardown so it actually shows.
129
+ process.stderr.write(`\nUncaught exception: ${err?.stack ?? err}\n`);
130
+ process.exit(1);
131
+ };
132
+ this.unhandledRejectionHandler = (reason) => {
133
+ try {
134
+ this.stop();
135
+ }
136
+ catch { }
137
+ const msg = reason instanceof Error ? (reason.stack ?? reason.message) : String(reason);
138
+ process.stderr.write(`\nUnhandled rejection: ${msg}\n`);
139
+ process.exit(1);
140
+ };
141
+ process.on("uncaughtException", this.uncaughtExceptionHandler);
142
+ process.on("unhandledRejection", this.unhandledRejectionHandler);
143
+ }
144
+ removeSignalHandlers() {
145
+ for (const { signal, handler } of this.signalHandlers) {
146
+ process.removeListener(signal, handler);
147
+ }
148
+ this.signalHandlers = [];
149
+ if (this.uncaughtExceptionHandler) {
150
+ process.removeListener("uncaughtException", this.uncaughtExceptionHandler);
151
+ this.uncaughtExceptionHandler = undefined;
152
+ }
153
+ if (this.unhandledRejectionHandler) {
154
+ process.removeListener("unhandledRejection", this.unhandledRejectionHandler);
155
+ this.unhandledRejectionHandler = undefined;
156
+ }
157
+ }
158
+ start(onInput, onResize) {
159
+ this.inputHandler = onInput;
160
+ this.resizeHandler = onResize;
161
+ // Save previous state and enable raw mode
162
+ this.wasRaw = process.stdin.isRaw || false;
163
+ if (process.stdin.setRawMode) {
164
+ process.stdin.setRawMode(true);
165
+ }
166
+ process.stdin.setEncoding("utf8");
167
+ process.stdin.resume();
168
+ // Enable bracketed paste mode - terminal will wrap pastes in \x1b[200~ ... \x1b[201~
169
+ process.stdout.write("\x1b[?2004h");
170
+ // Set up resize handler immediately
171
+ process.stdout.on("resize", this.resizeHandler);
172
+ // Refresh terminal dimensions - they may be stale after suspend/resume
173
+ // (SIGWINCH is lost while process is stopped). Unix only.
174
+ if (process.platform !== "win32") {
175
+ process.kill(process.pid, "SIGWINCH");
176
+ }
177
+ // On Windows, enable ENABLE_VIRTUAL_TERMINAL_INPUT so the console sends
178
+ // VT escape sequences (e.g. \x1b[Z for Shift+Tab) instead of raw console
179
+ // events that lose modifier information. Must run AFTER setRawMode(true)
180
+ // since that resets console mode flags.
181
+ this.enableWindowsVTInput();
182
+ // Query and enable Kitty keyboard protocol
183
+ // The query handler intercepts input temporarily, then installs the user's handler
184
+ // See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/
185
+ this.queryAndEnableKittyProtocol();
186
+ // Install signal + uncaught handlers so we always restore terminal state on exit.
187
+ this.installSignalHandlers();
188
+ }
189
+ /**
190
+ * Set up StdinBuffer to split batched input into individual sequences.
191
+ * This ensures components receive single events, making matchesKey/isKeyRelease work correctly.
192
+ *
193
+ * Also watches for Kitty protocol response and enables it when detected.
194
+ * This is done here (after stdinBuffer parsing) rather than on raw stdin
195
+ * to handle the case where the response arrives split across multiple events.
196
+ */
197
+ setupStdinBuffer() {
198
+ this.stdinBuffer = new StdinBuffer({ timeout: 10 });
199
+ // Kitty protocol response pattern: \x1b[?<flags>u
200
+ const kittyResponsePattern = /^\x1b\[\?(\d+)u$/;
201
+ // Forward individual sequences to the input handler
202
+ this.stdinBuffer.on("data", (sequence) => {
203
+ // Check for Kitty protocol response (only if not already enabled)
204
+ if (!this._kittyProtocolActive) {
205
+ const match = sequence.match(kittyResponsePattern);
206
+ if (match) {
207
+ this._kittyProtocolActive = true;
208
+ setKittyProtocolActive(true);
209
+ // Enable Kitty keyboard protocol (push flags)
210
+ // Flag 1 = disambiguate escape codes
211
+ // Flag 2 = report event types (press/repeat/release)
212
+ // Flag 4 = report alternate keys (shifted key, base layout key)
213
+ // Base layout key enables shortcuts to work with non-Latin keyboard layouts
214
+ process.stdout.write("\x1b[>7u");
215
+ return; // Don't forward protocol response to TUI
216
+ }
217
+ }
218
+ // Intercept OSC responses (e.g. background color query)
219
+ if (this.oscListeners.length > 0 && this.handleOscResponse(sequence)) {
220
+ return;
221
+ }
222
+ if (this.inputHandler) {
223
+ this.inputHandler(sequence);
224
+ }
225
+ });
226
+ // Re-wrap paste content with bracketed paste markers for existing editor handling
227
+ this.stdinBuffer.on("paste", (content) => {
228
+ if (this.inputHandler) {
229
+ this.inputHandler(`\x1b[200~${content}\x1b[201~`);
230
+ }
231
+ });
232
+ // Handler that pipes stdin data through the buffer
233
+ this.stdinDataHandler = (data) => {
234
+ this.stdinBuffer.process(data);
235
+ };
236
+ }
237
+ /**
238
+ * Query terminal for Kitty keyboard protocol support and enable if available.
239
+ *
240
+ * Sends CSI ? u to query current flags. If terminal responds with CSI ? <flags> u,
241
+ * it supports the protocol and we enable it with CSI > 1 u.
242
+ *
243
+ * If no Kitty response arrives shortly after startup, fall back to enabling
244
+ * xterm modifyOtherKeys mode 2. This is needed for tmux, which can forward
245
+ * modified enter keys as CSI-u when extended-keys is enabled, but may not
246
+ * answer the Kitty protocol query.
247
+ *
248
+ * The response is detected in setupStdinBuffer's data handler, which properly
249
+ * handles the case where the response arrives split across multiple stdin events.
250
+ */
251
+ queryAndEnableKittyProtocol() {
252
+ this.setupStdinBuffer();
253
+ process.stdin.on("data", this.stdinDataHandler);
254
+ process.stdout.write("\x1b[?u");
255
+ setTimeout(() => {
256
+ if (!this._kittyProtocolActive && !this._modifyOtherKeysActive) {
257
+ process.stdout.write("\x1b[>4;2m");
258
+ this._modifyOtherKeysActive = true;
259
+ }
260
+ }, 150);
261
+ }
262
+ /**
263
+ * On Windows, add ENABLE_VIRTUAL_TERMINAL_INPUT (0x0200) to the stdin
264
+ * console handle so the terminal sends VT sequences for modified keys
265
+ * (e.g. \x1b[Z for Shift+Tab). Without this, libuv's ReadConsoleInputW
266
+ * discards modifier state and Shift+Tab arrives as plain \t.
267
+ */
268
+ enableWindowsVTInput() {
269
+ if (process.platform !== "win32")
270
+ return;
271
+ try {
272
+ // Dynamic require to avoid bundling koffi's 74MB of cross-platform
273
+ // native binaries into every compiled binary. Koffi is only needed
274
+ // on Windows for VT input support.
275
+ const koffi = cjsRequire("koffi");
276
+ const k32 = koffi.load("kernel32.dll");
277
+ const GetStdHandle = k32.func("void* __stdcall GetStdHandle(int)");
278
+ const GetConsoleMode = k32.func("bool __stdcall GetConsoleMode(void*, _Out_ uint32_t*)");
279
+ const SetConsoleMode = k32.func("bool __stdcall SetConsoleMode(void*, uint32_t)");
280
+ const STD_INPUT_HANDLE = -10;
281
+ const ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200;
282
+ const handle = GetStdHandle(STD_INPUT_HANDLE);
283
+ const mode = new Uint32Array(1);
284
+ GetConsoleMode(handle, mode);
285
+ SetConsoleMode(handle, mode[0] | ENABLE_VIRTUAL_TERMINAL_INPUT);
286
+ }
287
+ catch {
288
+ // koffi not available — Shift+Tab won't be distinguishable from Tab
289
+ }
290
+ }
291
+ async drainInput(maxMs = 1000, idleMs = 50) {
292
+ if (this._kittyProtocolActive) {
293
+ // Disable Kitty keyboard protocol first so any late key releases
294
+ // do not generate new Kitty escape sequences.
295
+ process.stdout.write("\x1b[<u");
296
+ this._kittyProtocolActive = false;
297
+ setKittyProtocolActive(false);
298
+ }
299
+ if (this._modifyOtherKeysActive) {
300
+ process.stdout.write("\x1b[>4;0m");
301
+ this._modifyOtherKeysActive = false;
302
+ }
303
+ const previousHandler = this.inputHandler;
304
+ this.inputHandler = undefined;
305
+ let lastDataTime = Date.now();
306
+ const onData = () => {
307
+ lastDataTime = Date.now();
308
+ };
309
+ process.stdin.on("data", onData);
310
+ const endTime = Date.now() + maxMs;
311
+ try {
312
+ while (true) {
313
+ const now = Date.now();
314
+ const timeLeft = endTime - now;
315
+ if (timeLeft <= 0)
316
+ break;
317
+ if (now - lastDataTime >= idleMs)
318
+ break;
319
+ await new Promise((resolve) => setTimeout(resolve, Math.min(idleMs, timeLeft)));
320
+ }
321
+ }
322
+ finally {
323
+ process.stdin.removeListener("data", onData);
324
+ this.inputHandler = previousHandler;
325
+ }
326
+ }
327
+ stop() {
328
+ // Cancel any pending OSC listeners
329
+ for (const listener of this.oscListeners) {
330
+ clearTimeout(listener.timer);
331
+ listener.resolve(null);
332
+ }
333
+ this.oscListeners = [];
334
+ // Disable mouse tracking
335
+ this.disableMouseTracking();
336
+ // Disable bracketed paste mode
337
+ process.stdout.write("\x1b[?2004l");
338
+ // Disable Kitty keyboard protocol if not already done by drainInput()
339
+ if (this._kittyProtocolActive) {
340
+ process.stdout.write("\x1b[<u");
341
+ this._kittyProtocolActive = false;
342
+ setKittyProtocolActive(false);
343
+ }
344
+ if (this._modifyOtherKeysActive) {
345
+ process.stdout.write("\x1b[>4;0m");
346
+ this._modifyOtherKeysActive = false;
347
+ }
348
+ // Clean up StdinBuffer
349
+ if (this.stdinBuffer) {
350
+ this.stdinBuffer.destroy();
351
+ this.stdinBuffer = undefined;
352
+ }
353
+ // Remove event handlers
354
+ if (this.stdinDataHandler) {
355
+ process.stdin.removeListener("data", this.stdinDataHandler);
356
+ this.stdinDataHandler = undefined;
357
+ }
358
+ this.inputHandler = undefined;
359
+ if (this.resizeHandler) {
360
+ process.stdout.removeListener("resize", this.resizeHandler);
361
+ this.resizeHandler = undefined;
362
+ }
363
+ // Pause stdin to prevent any buffered input (e.g., Ctrl+D) from being
364
+ // re-interpreted after raw mode is disabled. This fixes a race condition
365
+ // where Ctrl+D could close the parent shell over SSH.
366
+ process.stdin.pause();
367
+ // Restore raw mode state
368
+ if (process.stdin.setRawMode) {
369
+ process.stdin.setRawMode(this.wasRaw);
370
+ }
371
+ // Leave alt screen AFTER restoring stdin so terminal state transitions atomically.
372
+ this.leaveAltScreen();
373
+ // Reset any leftover SGR state in the restored primary buffer.
374
+ if (this.isTTY()) {
375
+ process.stdout.write("\x1b[0m");
376
+ }
377
+ this.removeSignalHandlers();
378
+ }
379
+ write(data) {
380
+ process.stdout.write(data);
381
+ if (this.writeLogPath) {
382
+ try {
383
+ fs.appendFileSync(this.writeLogPath, data, { encoding: "utf8" });
384
+ }
385
+ catch {
386
+ // Ignore logging errors
387
+ }
388
+ }
389
+ }
390
+ get columns() {
391
+ return process.stdout.columns || 80;
392
+ }
393
+ get rows() {
394
+ return process.stdout.rows || 24;
395
+ }
396
+ moveBy(lines) {
397
+ if (lines > 0) {
398
+ // Move down
399
+ process.stdout.write(`\x1b[${lines}B`);
400
+ }
401
+ else if (lines < 0) {
402
+ // Move up
403
+ process.stdout.write(`\x1b[${-lines}A`);
404
+ }
405
+ // lines === 0: no movement
406
+ }
407
+ hideCursor() {
408
+ process.stdout.write("\x1b[?25l");
409
+ }
410
+ showCursor() {
411
+ process.stdout.write("\x1b[?25h");
412
+ }
413
+ clearLine() {
414
+ process.stdout.write("\x1b[K");
415
+ }
416
+ clearFromCursor() {
417
+ process.stdout.write("\x1b[J");
418
+ }
419
+ clearScreen() {
420
+ process.stdout.write("\x1b[2J\x1b[H"); // Clear screen and move to home (1,1)
421
+ }
422
+ setTitle(title) {
423
+ // OSC 0;title BEL - set terminal window title
424
+ process.stdout.write(`\x1b]0;${title}\x07`);
425
+ }
426
+ }
427
+ //# sourceMappingURL=terminal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"terminal.js","sourceRoot":"","sources":["../src/terminal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,MAAM,UAAU,GAAG,aAAa,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC;AAwDlD;;GAEG;AACH,MAAM,OAAO,eAAe;IACnB,MAAM,GAAG,KAAK,CAAC;IACf,YAAY,CAA0B;IACtC,aAAa,CAAc;IAC3B,oBAAoB,GAAG,KAAK,CAAC;IAC7B,sBAAsB,GAAG,KAAK,CAAC;IAC/B,gBAAgB,GAAG,KAAK,CAAC;IACzB,oBAAoB,GAAG,KAAK,CAAC;IAC7B,WAAW,CAAe;IAC1B,gBAAgB,CAA0B;IAC1C,YAAY,GAId,EAAE,CAAC;IACD,cAAc,GAGhB,EAAE,CAAC;IACD,wBAAwB,CAAwB;IAChD,yBAAyB,CAA6B;IACtD,YAAY,GAAG,CAAC,GAAG,EAAE,CAAC;QAC7B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C3B,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;YACf,YAAY;YACZ,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,KAAK,GAAG,CAAC,CAAC;QACxC,CAAC;aAAM,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;YACtB,UAAU;YACV,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,GAAG,CAAC,CAAC;QACzC,CAAC;QACD,2BAA2B;IAD1B,CAED;IAED,UAAU,GAAS;QAClB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;IAAA,CAClC;IAED,UAAU,GAAS;QAClB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;IAAA,CAClC;IAED,SAAS,GAAS;QACjB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAAA,CAC/B;IAED,eAAe,GAAS;QACvB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAAA,CAC/B;IAED,WAAW,GAAS;QACnB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC,CAAC,sCAAsC;IAAvC,CACtC;IAED,QAAQ,CAAC,KAAa,EAAQ;QAC7B,8CAA8C;QAC9C,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,KAAK,MAAM,CAAC,CAAC;IAAA,CAC5C;CACD","sourcesContent":["import * as fs from \"node:fs\";\nimport { createRequire } from \"node:module\";\nimport * as path from \"node:path\";\nimport { setKittyProtocolActive } from \"./keys.js\";\nimport { StdinBuffer } from \"./stdin-buffer.js\";\n\nconst cjsRequire = createRequire(import.meta.url);\n\n/**\n * Minimal terminal interface for TUI\n */\nexport interface Terminal {\n\t// Start the terminal with input and resize handlers\n\tstart(onInput: (data: string) => void, onResize: () => void): void;\n\n\t// Stop the terminal and restore state\n\tstop(): void;\n\n\t/**\n\t * Drain stdin before exiting to prevent Kitty key release events from\n\t * leaking to the parent shell over slow SSH connections.\n\t * @param maxMs - Maximum time to drain (default: 1000ms)\n\t * @param idleMs - Exit early if no input arrives within this time (default: 50ms)\n\t */\n\tdrainInput(maxMs?: number, idleMs?: number): Promise<void>;\n\n\t// Write output to terminal\n\twrite(data: string): void;\n\n\t// Get terminal dimensions\n\tget columns(): number;\n\tget rows(): number;\n\n\t// Whether Kitty keyboard protocol is active\n\tget kittyProtocolActive(): boolean;\n\n\t// Cursor positioning (relative to current position)\n\tmoveBy(lines: number): void; // Move cursor up (negative) or down (positive) by N lines\n\n\t// Cursor visibility\n\thideCursor(): void; // Hide the cursor\n\tshowCursor(): void; // Show the cursor\n\n\t// Clear operations\n\tclearLine(): void; // Clear current line\n\tclearFromCursor(): void; // Clear from cursor to end of screen\n\tclearScreen(): void; // Clear entire screen and move cursor to (0,0)\n\n\t// Title operations\n\tsetTitle(title: string): void; // Set terminal window title\n\n\t// Alt-screen + mouse (optional, no-op on non-TTY implementations)\n\tenterAltScreen(): void;\n\tleaveAltScreen(): void;\n\tenableMouseTracking(): void;\n\tdisableMouseTracking(): void;\n\tisTTY(): boolean;\n\n\t// Query an OSC sequence and read the response within a timeout.\n\tqueryOsc(sequence: string, responsePrefix: string, timeoutMs: number): Promise<string | null>;\n}\n\n/**\n * Real terminal using process.stdin/stdout\n */\nexport class ProcessTerminal implements Terminal {\n\tprivate wasRaw = false;\n\tprivate inputHandler?: (data: string) => void;\n\tprivate resizeHandler?: () => void;\n\tprivate _kittyProtocolActive = false;\n\tprivate _modifyOtherKeysActive = false;\n\tprivate _altScreenActive = false;\n\tprivate _mouseTrackingActive = false;\n\tprivate stdinBuffer?: StdinBuffer;\n\tprivate stdinDataHandler?: (data: string) => void;\n\tprivate oscListeners: {\n\t\tprefix: string;\n\t\tresolve: (value: string | null) => void;\n\t\ttimer: NodeJS.Timeout;\n\t}[] = [];\n\tprivate signalHandlers: {\n\t\tsignal: \"SIGINT\" | \"SIGTERM\" | \"SIGHUP\";\n\t\thandler: () => void;\n\t}[] = [];\n\tprivate uncaughtExceptionHandler?: (err: Error) => void;\n\tprivate unhandledRejectionHandler?: (reason: unknown) => void;\n\tprivate writeLogPath = (() => {\n\t\tconst env = process.env.PI_TUI_WRITE_LOG || \"\";\n\t\tif (!env) return \"\";\n\t\ttry {\n\t\t\tif (fs.statSync(env).isDirectory()) {\n\t\t\t\tconst now = new Date();\n\t\t\t\tconst ts = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, \"0\")}-${String(now.getDate()).padStart(2, \"0\")}_${String(now.getHours()).padStart(2, \"0\")}-${String(now.getMinutes()).padStart(2, \"0\")}-${String(now.getSeconds()).padStart(2, \"0\")}`;\n\t\t\t\treturn path.join(env, `tui-${ts}-${process.pid}.log`);\n\t\t\t}\n\t\t} catch {\n\t\t\t// Not an existing directory - use as-is (file path)\n\t\t}\n\t\treturn env;\n\t})();\n\n\tget kittyProtocolActive(): boolean {\n\t\treturn this._kittyProtocolActive;\n\t}\n\n\tisTTY(): boolean {\n\t\treturn Boolean(process.stdout.isTTY);\n\t}\n\n\tenterAltScreen(): void {\n\t\tif (this._altScreenActive || !this.isTTY()) return;\n\t\t// CSI ?1049h: save primary screen, enter alt screen, position cursor at 1,1\n\t\t// CSI 2J: clear visible area\n\t\t// CSI H: move cursor home\n\t\t// CSI ?25l: hide cursor\n\t\tprocess.stdout.write(\"\\x1b[?1049h\\x1b[2J\\x1b[H\\x1b[?25l\");\n\t\tthis._altScreenActive = true;\n\t}\n\n\tleaveAltScreen(): void {\n\t\tif (!this._altScreenActive) return;\n\t\t// CSI ?25h: show cursor\n\t\t// CSI ?1049l: leave alt screen, restore primary\n\t\tprocess.stdout.write(\"\\x1b[?25h\\x1b[?1049l\");\n\t\tthis._altScreenActive = false;\n\t}\n\n\tenableMouseTracking(): void {\n\t\tif (this._mouseTrackingActive || !this.isTTY()) return;\n\t\t// CSI ?1000h: send mouse press/release events\n\t\t// CSI ?1006h: SGR-encoded mouse events (wider coordinate range)\n\t\tprocess.stdout.write(\"\\x1b[?1000h\\x1b[?1006h\");\n\t\tthis._mouseTrackingActive = true;\n\t}\n\n\tdisableMouseTracking(): void {\n\t\tif (!this._mouseTrackingActive) return;\n\t\tprocess.stdout.write(\"\\x1b[?1006l\\x1b[?1000l\");\n\t\tthis._mouseTrackingActive = false;\n\t}\n\n\tasync queryOsc(sequence: string, responsePrefix: string, timeoutMs: number): Promise<string | null> {\n\t\tif (!this.isTTY()) return null;\n\t\treturn new Promise<string | null>((resolve) => {\n\t\t\tconst timer = setTimeout(() => {\n\t\t\t\tconst idx = this.oscListeners.findIndex((l) => l.resolve === resolve);\n\t\t\t\tif (idx !== -1) this.oscListeners.splice(idx, 1);\n\t\t\t\tresolve(null);\n\t\t\t}, timeoutMs);\n\t\t\tthis.oscListeners.push({ prefix: responsePrefix, resolve, timer });\n\t\t\tprocess.stdout.write(sequence);\n\t\t});\n\t}\n\n\tprivate handleOscResponse(data: string): boolean {\n\t\tfor (let i = 0; i < this.oscListeners.length; i++) {\n\t\t\tif (data.includes(this.oscListeners[i].prefix)) {\n\t\t\t\tconst { resolve, timer } = this.oscListeners[i];\n\t\t\t\tclearTimeout(timer);\n\t\t\t\tthis.oscListeners.splice(i, 1);\n\t\t\t\tresolve(data);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n\n\tprivate installSignalHandlers(): void {\n\t\tconst teardown = (exitCode: number | null) => {\n\t\t\ttry {\n\t\t\t\tthis.stop();\n\t\t\t} catch {\n\t\t\t\t// best-effort\n\t\t\t}\n\t\t\tif (exitCode !== null) {\n\t\t\t\tprocess.exit(exitCode);\n\t\t\t}\n\t\t};\n\t\tconst signals: Array<\"SIGINT\" | \"SIGTERM\" | \"SIGHUP\"> = [\"SIGINT\", \"SIGTERM\", \"SIGHUP\"];\n\t\tconst signalCodes: Record<string, number> = { SIGINT: 130, SIGTERM: 143, SIGHUP: 129 };\n\t\tfor (const sig of signals) {\n\t\t\tconst handler = () => teardown(signalCodes[sig] ?? 128);\n\t\t\tprocess.on(sig, handler);\n\t\t\tthis.signalHandlers.push({ signal: sig, handler });\n\t\t}\n\t\tthis.uncaughtExceptionHandler = (err: Error) => {\n\t\t\ttry {\n\t\t\t\tthis.stop();\n\t\t\t} catch {}\n\t\t\t// Log after teardown so it actually shows.\n\t\t\tprocess.stderr.write(`\\nUncaught exception: ${err?.stack ?? err}\\n`);\n\t\t\tprocess.exit(1);\n\t\t};\n\t\tthis.unhandledRejectionHandler = (reason: unknown) => {\n\t\t\ttry {\n\t\t\t\tthis.stop();\n\t\t\t} catch {}\n\t\t\tconst msg = reason instanceof Error ? (reason.stack ?? reason.message) : String(reason);\n\t\t\tprocess.stderr.write(`\\nUnhandled rejection: ${msg}\\n`);\n\t\t\tprocess.exit(1);\n\t\t};\n\t\tprocess.on(\"uncaughtException\", this.uncaughtExceptionHandler);\n\t\tprocess.on(\"unhandledRejection\", this.unhandledRejectionHandler);\n\t}\n\n\tprivate removeSignalHandlers(): void {\n\t\tfor (const { signal, handler } of this.signalHandlers) {\n\t\t\tprocess.removeListener(signal, handler);\n\t\t}\n\t\tthis.signalHandlers = [];\n\t\tif (this.uncaughtExceptionHandler) {\n\t\t\tprocess.removeListener(\"uncaughtException\", this.uncaughtExceptionHandler);\n\t\t\tthis.uncaughtExceptionHandler = undefined;\n\t\t}\n\t\tif (this.unhandledRejectionHandler) {\n\t\t\tprocess.removeListener(\"unhandledRejection\", this.unhandledRejectionHandler);\n\t\t\tthis.unhandledRejectionHandler = undefined;\n\t\t}\n\t}\n\n\tstart(onInput: (data: string) => void, onResize: () => void): void {\n\t\tthis.inputHandler = onInput;\n\t\tthis.resizeHandler = onResize;\n\n\t\t// Save previous state and enable raw mode\n\t\tthis.wasRaw = process.stdin.isRaw || false;\n\t\tif (process.stdin.setRawMode) {\n\t\t\tprocess.stdin.setRawMode(true);\n\t\t}\n\t\tprocess.stdin.setEncoding(\"utf8\");\n\t\tprocess.stdin.resume();\n\n\t\t// Enable bracketed paste mode - terminal will wrap pastes in \\x1b[200~ ... \\x1b[201~\n\t\tprocess.stdout.write(\"\\x1b[?2004h\");\n\n\t\t// Set up resize handler immediately\n\t\tprocess.stdout.on(\"resize\", this.resizeHandler);\n\n\t\t// Refresh terminal dimensions - they may be stale after suspend/resume\n\t\t// (SIGWINCH is lost while process is stopped). Unix only.\n\t\tif (process.platform !== \"win32\") {\n\t\t\tprocess.kill(process.pid, \"SIGWINCH\");\n\t\t}\n\n\t\t// On Windows, enable ENABLE_VIRTUAL_TERMINAL_INPUT so the console sends\n\t\t// VT escape sequences (e.g. \\x1b[Z for Shift+Tab) instead of raw console\n\t\t// events that lose modifier information. Must run AFTER setRawMode(true)\n\t\t// since that resets console mode flags.\n\t\tthis.enableWindowsVTInput();\n\n\t\t// Query and enable Kitty keyboard protocol\n\t\t// The query handler intercepts input temporarily, then installs the user's handler\n\t\t// See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/\n\t\tthis.queryAndEnableKittyProtocol();\n\n\t\t// Install signal + uncaught handlers so we always restore terminal state on exit.\n\t\tthis.installSignalHandlers();\n\t}\n\n\t/**\n\t * Set up StdinBuffer to split batched input into individual sequences.\n\t * This ensures components receive single events, making matchesKey/isKeyRelease work correctly.\n\t *\n\t * Also watches for Kitty protocol response and enables it when detected.\n\t * This is done here (after stdinBuffer parsing) rather than on raw stdin\n\t * to handle the case where the response arrives split across multiple events.\n\t */\n\tprivate setupStdinBuffer(): void {\n\t\tthis.stdinBuffer = new StdinBuffer({ timeout: 10 });\n\n\t\t// Kitty protocol response pattern: \\x1b[?<flags>u\n\t\tconst kittyResponsePattern = /^\\x1b\\[\\?(\\d+)u$/;\n\n\t\t// Forward individual sequences to the input handler\n\t\tthis.stdinBuffer.on(\"data\", (sequence) => {\n\t\t\t// Check for Kitty protocol response (only if not already enabled)\n\t\t\tif (!this._kittyProtocolActive) {\n\t\t\t\tconst match = sequence.match(kittyResponsePattern);\n\t\t\t\tif (match) {\n\t\t\t\t\tthis._kittyProtocolActive = true;\n\t\t\t\t\tsetKittyProtocolActive(true);\n\n\t\t\t\t\t// Enable Kitty keyboard protocol (push flags)\n\t\t\t\t\t// Flag 1 = disambiguate escape codes\n\t\t\t\t\t// Flag 2 = report event types (press/repeat/release)\n\t\t\t\t\t// Flag 4 = report alternate keys (shifted key, base layout key)\n\t\t\t\t\t// Base layout key enables shortcuts to work with non-Latin keyboard layouts\n\t\t\t\t\tprocess.stdout.write(\"\\x1b[>7u\");\n\t\t\t\t\treturn; // Don't forward protocol response to TUI\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Intercept OSC responses (e.g. background color query)\n\t\t\tif (this.oscListeners.length > 0 && this.handleOscResponse(sequence)) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (this.inputHandler) {\n\t\t\t\tthis.inputHandler(sequence);\n\t\t\t}\n\t\t});\n\n\t\t// Re-wrap paste content with bracketed paste markers for existing editor handling\n\t\tthis.stdinBuffer.on(\"paste\", (content) => {\n\t\t\tif (this.inputHandler) {\n\t\t\t\tthis.inputHandler(`\\x1b[200~${content}\\x1b[201~`);\n\t\t\t}\n\t\t});\n\n\t\t// Handler that pipes stdin data through the buffer\n\t\tthis.stdinDataHandler = (data: string) => {\n\t\t\tthis.stdinBuffer!.process(data);\n\t\t};\n\t}\n\n\t/**\n\t * Query terminal for Kitty keyboard protocol support and enable if available.\n\t *\n\t * Sends CSI ? u to query current flags. If terminal responds with CSI ? <flags> u,\n\t * it supports the protocol and we enable it with CSI > 1 u.\n\t *\n\t * If no Kitty response arrives shortly after startup, fall back to enabling\n\t * xterm modifyOtherKeys mode 2. This is needed for tmux, which can forward\n\t * modified enter keys as CSI-u when extended-keys is enabled, but may not\n\t * answer the Kitty protocol query.\n\t *\n\t * The response is detected in setupStdinBuffer's data handler, which properly\n\t * handles the case where the response arrives split across multiple stdin events.\n\t */\n\tprivate queryAndEnableKittyProtocol(): void {\n\t\tthis.setupStdinBuffer();\n\t\tprocess.stdin.on(\"data\", this.stdinDataHandler!);\n\t\tprocess.stdout.write(\"\\x1b[?u\");\n\t\tsetTimeout(() => {\n\t\t\tif (!this._kittyProtocolActive && !this._modifyOtherKeysActive) {\n\t\t\t\tprocess.stdout.write(\"\\x1b[>4;2m\");\n\t\t\t\tthis._modifyOtherKeysActive = true;\n\t\t\t}\n\t\t}, 150);\n\t}\n\n\t/**\n\t * On Windows, add ENABLE_VIRTUAL_TERMINAL_INPUT (0x0200) to the stdin\n\t * console handle so the terminal sends VT sequences for modified keys\n\t * (e.g. \\x1b[Z for Shift+Tab). Without this, libuv's ReadConsoleInputW\n\t * discards modifier state and Shift+Tab arrives as plain \\t.\n\t */\n\tprivate enableWindowsVTInput(): void {\n\t\tif (process.platform !== \"win32\") return;\n\t\ttry {\n\t\t\t// Dynamic require to avoid bundling koffi's 74MB of cross-platform\n\t\t\t// native binaries into every compiled binary. Koffi is only needed\n\t\t\t// on Windows for VT input support.\n\t\t\tconst koffi = cjsRequire(\"koffi\");\n\t\t\tconst k32 = koffi.load(\"kernel32.dll\");\n\t\t\tconst GetStdHandle = k32.func(\"void* __stdcall GetStdHandle(int)\");\n\t\t\tconst GetConsoleMode = k32.func(\"bool __stdcall GetConsoleMode(void*, _Out_ uint32_t*)\");\n\t\t\tconst SetConsoleMode = k32.func(\"bool __stdcall SetConsoleMode(void*, uint32_t)\");\n\n\t\t\tconst STD_INPUT_HANDLE = -10;\n\t\t\tconst ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200;\n\t\t\tconst handle = GetStdHandle(STD_INPUT_HANDLE);\n\t\t\tconst mode = new Uint32Array(1);\n\t\t\tGetConsoleMode(handle, mode);\n\t\t\tSetConsoleMode(handle, mode[0]! | ENABLE_VIRTUAL_TERMINAL_INPUT);\n\t\t} catch {\n\t\t\t// koffi not available — Shift+Tab won't be distinguishable from Tab\n\t\t}\n\t}\n\n\tasync drainInput(maxMs = 1000, idleMs = 50): Promise<void> {\n\t\tif (this._kittyProtocolActive) {\n\t\t\t// Disable Kitty keyboard protocol first so any late key releases\n\t\t\t// do not generate new Kitty escape sequences.\n\t\t\tprocess.stdout.write(\"\\x1b[<u\");\n\t\t\tthis._kittyProtocolActive = false;\n\t\t\tsetKittyProtocolActive(false);\n\t\t}\n\t\tif (this._modifyOtherKeysActive) {\n\t\t\tprocess.stdout.write(\"\\x1b[>4;0m\");\n\t\t\tthis._modifyOtherKeysActive = false;\n\t\t}\n\n\t\tconst previousHandler = this.inputHandler;\n\t\tthis.inputHandler = undefined;\n\n\t\tlet lastDataTime = Date.now();\n\t\tconst onData = () => {\n\t\t\tlastDataTime = Date.now();\n\t\t};\n\n\t\tprocess.stdin.on(\"data\", onData);\n\t\tconst endTime = Date.now() + maxMs;\n\n\t\ttry {\n\t\t\twhile (true) {\n\t\t\t\tconst now = Date.now();\n\t\t\t\tconst timeLeft = endTime - now;\n\t\t\t\tif (timeLeft <= 0) break;\n\t\t\t\tif (now - lastDataTime >= idleMs) break;\n\t\t\t\tawait new Promise((resolve) => setTimeout(resolve, Math.min(idleMs, timeLeft)));\n\t\t\t}\n\t\t} finally {\n\t\t\tprocess.stdin.removeListener(\"data\", onData);\n\t\t\tthis.inputHandler = previousHandler;\n\t\t}\n\t}\n\n\tstop(): void {\n\t\t// Cancel any pending OSC listeners\n\t\tfor (const listener of this.oscListeners) {\n\t\t\tclearTimeout(listener.timer);\n\t\t\tlistener.resolve(null);\n\t\t}\n\t\tthis.oscListeners = [];\n\n\t\t// Disable mouse tracking\n\t\tthis.disableMouseTracking();\n\n\t\t// Disable bracketed paste mode\n\t\tprocess.stdout.write(\"\\x1b[?2004l\");\n\n\t\t// Disable Kitty keyboard protocol if not already done by drainInput()\n\t\tif (this._kittyProtocolActive) {\n\t\t\tprocess.stdout.write(\"\\x1b[<u\");\n\t\t\tthis._kittyProtocolActive = false;\n\t\t\tsetKittyProtocolActive(false);\n\t\t}\n\t\tif (this._modifyOtherKeysActive) {\n\t\t\tprocess.stdout.write(\"\\x1b[>4;0m\");\n\t\t\tthis._modifyOtherKeysActive = false;\n\t\t}\n\n\t\t// Clean up StdinBuffer\n\t\tif (this.stdinBuffer) {\n\t\t\tthis.stdinBuffer.destroy();\n\t\t\tthis.stdinBuffer = undefined;\n\t\t}\n\n\t\t// Remove event handlers\n\t\tif (this.stdinDataHandler) {\n\t\t\tprocess.stdin.removeListener(\"data\", this.stdinDataHandler);\n\t\t\tthis.stdinDataHandler = undefined;\n\t\t}\n\t\tthis.inputHandler = undefined;\n\t\tif (this.resizeHandler) {\n\t\t\tprocess.stdout.removeListener(\"resize\", this.resizeHandler);\n\t\t\tthis.resizeHandler = undefined;\n\t\t}\n\n\t\t// Pause stdin to prevent any buffered input (e.g., Ctrl+D) from being\n\t\t// re-interpreted after raw mode is disabled. This fixes a race condition\n\t\t// where Ctrl+D could close the parent shell over SSH.\n\t\tprocess.stdin.pause();\n\n\t\t// Restore raw mode state\n\t\tif (process.stdin.setRawMode) {\n\t\t\tprocess.stdin.setRawMode(this.wasRaw);\n\t\t}\n\n\t\t// Leave alt screen AFTER restoring stdin so terminal state transitions atomically.\n\t\tthis.leaveAltScreen();\n\n\t\t// Reset any leftover SGR state in the restored primary buffer.\n\t\tif (this.isTTY()) {\n\t\t\tprocess.stdout.write(\"\\x1b[0m\");\n\t\t}\n\n\t\tthis.removeSignalHandlers();\n\t}\n\n\twrite(data: string): void {\n\t\tprocess.stdout.write(data);\n\t\tif (this.writeLogPath) {\n\t\t\ttry {\n\t\t\t\tfs.appendFileSync(this.writeLogPath, data, { encoding: \"utf8\" });\n\t\t\t} catch {\n\t\t\t\t// Ignore logging errors\n\t\t\t}\n\t\t}\n\t}\n\n\tget columns(): number {\n\t\treturn process.stdout.columns || 80;\n\t}\n\n\tget rows(): number {\n\t\treturn process.stdout.rows || 24;\n\t}\n\n\tmoveBy(lines: number): void {\n\t\tif (lines > 0) {\n\t\t\t// Move down\n\t\t\tprocess.stdout.write(`\\x1b[${lines}B`);\n\t\t} else if (lines < 0) {\n\t\t\t// Move up\n\t\t\tprocess.stdout.write(`\\x1b[${-lines}A`);\n\t\t}\n\t\t// lines === 0: no movement\n\t}\n\n\thideCursor(): void {\n\t\tprocess.stdout.write(\"\\x1b[?25l\");\n\t}\n\n\tshowCursor(): void {\n\t\tprocess.stdout.write(\"\\x1b[?25h\");\n\t}\n\n\tclearLine(): void {\n\t\tprocess.stdout.write(\"\\x1b[K\");\n\t}\n\n\tclearFromCursor(): void {\n\t\tprocess.stdout.write(\"\\x1b[J\");\n\t}\n\n\tclearScreen(): void {\n\t\tprocess.stdout.write(\"\\x1b[2J\\x1b[H\"); // Clear screen and move to home (1,1)\n\t}\n\n\tsetTitle(title: string): void {\n\t\t// OSC 0;title BEL - set terminal window title\n\t\tprocess.stdout.write(`\\x1b]0;${title}\\x07`);\n\t}\n}\n"]}