@gajae-code/tui 0.1.1

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 (59) hide show
  1. package/CHANGELOG.md +818 -0
  2. package/README.md +704 -0
  3. package/dist/types/autocomplete.d.ts +76 -0
  4. package/dist/types/bracketed-paste.d.ts +26 -0
  5. package/dist/types/components/box.d.ts +15 -0
  6. package/dist/types/components/cancellable-loader.d.ts +21 -0
  7. package/dist/types/components/editor.d.ts +101 -0
  8. package/dist/types/components/image.d.ts +16 -0
  9. package/dist/types/components/input.d.ts +16 -0
  10. package/dist/types/components/loader.d.ts +13 -0
  11. package/dist/types/components/markdown.d.ts +61 -0
  12. package/dist/types/components/select-list.d.ts +46 -0
  13. package/dist/types/components/settings-list.d.ts +39 -0
  14. package/dist/types/components/spacer.d.ts +11 -0
  15. package/dist/types/components/tab-bar.d.ts +56 -0
  16. package/dist/types/components/text.d.ts +13 -0
  17. package/dist/types/components/truncated-text.d.ts +10 -0
  18. package/dist/types/editor-component.d.ts +36 -0
  19. package/dist/types/fuzzy.d.ts +15 -0
  20. package/dist/types/index.d.ts +25 -0
  21. package/dist/types/keybindings.d.ts +189 -0
  22. package/dist/types/keys.d.ts +208 -0
  23. package/dist/types/kill-ring.d.ts +27 -0
  24. package/dist/types/stdin-buffer.d.ts +43 -0
  25. package/dist/types/symbols.d.ts +23 -0
  26. package/dist/types/terminal-capabilities.d.ts +75 -0
  27. package/dist/types/terminal.d.ts +61 -0
  28. package/dist/types/ttyid.d.ts +9 -0
  29. package/dist/types/tui.d.ts +161 -0
  30. package/dist/types/utils.d.ts +74 -0
  31. package/package.json +73 -0
  32. package/src/autocomplete.ts +836 -0
  33. package/src/bracketed-paste.ts +47 -0
  34. package/src/components/box.ts +144 -0
  35. package/src/components/cancellable-loader.ts +40 -0
  36. package/src/components/editor.ts +2664 -0
  37. package/src/components/image.ts +90 -0
  38. package/src/components/input.ts +465 -0
  39. package/src/components/loader.ts +86 -0
  40. package/src/components/markdown.ts +1009 -0
  41. package/src/components/select-list.ts +249 -0
  42. package/src/components/settings-list.ts +211 -0
  43. package/src/components/spacer.ts +28 -0
  44. package/src/components/tab-bar.ts +175 -0
  45. package/src/components/text.ts +110 -0
  46. package/src/components/truncated-text.ts +61 -0
  47. package/src/editor-component.ts +71 -0
  48. package/src/fuzzy.ts +143 -0
  49. package/src/index.ts +39 -0
  50. package/src/keybindings.ts +279 -0
  51. package/src/keys.ts +537 -0
  52. package/src/kill-ring.ts +46 -0
  53. package/src/stdin-buffer.ts +410 -0
  54. package/src/symbols.ts +24 -0
  55. package/src/terminal-capabilities.ts +537 -0
  56. package/src/terminal.ts +716 -0
  57. package/src/ttyid.ts +66 -0
  58. package/src/tui.ts +1481 -0
  59. package/src/utils.ts +359 -0
@@ -0,0 +1,716 @@
1
+ import { dlopen, FFIType, ptr } from "bun:ffi";
2
+ import * as fs from "node:fs";
3
+ import { $env, logger } from "@gajae-code/utils";
4
+ import { setKittyProtocolActive } from "./keys";
5
+ import { StdinBuffer } from "./stdin-buffer";
6
+
7
+ const TERMINAL_PROGRESS_KEEPALIVE_MS = 1000;
8
+ const TERMINAL_PROGRESS_ACTIVE_SEQUENCE = "\x1b]9;4;3\x07";
9
+ const TERMINAL_PROGRESS_CLEAR_SEQUENCE = "\x1b]9;4;0;\x07";
10
+
11
+ /**
12
+ * Minimal terminal interface for TUI
13
+ */
14
+
15
+ // Track active terminal for emergency cleanup on crash
16
+ let activeTerminal: ProcessTerminal | null = null;
17
+ // Track if a terminal was ever started (for emergency restore logic)
18
+ let terminalEverStarted = false;
19
+
20
+ const STD_INPUT_HANDLE = -10;
21
+ const ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200;
22
+ /**
23
+ * Emergency terminal restore - call this from signal/crash handlers
24
+ * Resets terminal state without requiring access to the ProcessTerminal instance
25
+ */
26
+ export function emergencyTerminalRestore(): void {
27
+ try {
28
+ const terminal = activeTerminal;
29
+ if (terminal) {
30
+ terminal.stop();
31
+ terminal.showCursor();
32
+ } else if (terminalEverStarted) {
33
+ // Blind restore only if we know a terminal was started but lost track of it
34
+ // This avoids writing escape sequences for non-TUI commands (grep, commit, etc.)
35
+ process.stdout.write(
36
+ "\x1b[?2004l" + // Disable bracketed paste
37
+ "\x1b[?2031l" + // Disable Mode 2031 appearance notifications
38
+ "\x1b[<u" + // Pop kitty keyboard protocol
39
+ "\x1b[>4;0m" + // Disable modifyOtherKeys fallback
40
+ "\x1b[?25h", // Show cursor
41
+ );
42
+ if (process.stdin.setRawMode) {
43
+ process.stdin.setRawMode(false);
44
+ }
45
+ }
46
+ } catch {
47
+ // Terminal may already be dead during crash cleanup - ignore errors
48
+ }
49
+ }
50
+ /** Terminal-reported appearance (dark/light mode). */
51
+ export type TerminalAppearance = "dark" | "light";
52
+ export interface Terminal {
53
+ // Start the terminal with input and resize handlers
54
+ start(onInput: (data: string) => void, onResize: () => void): void;
55
+
56
+ // Stop the terminal and restore state
57
+ stop(): void;
58
+
59
+ /**
60
+ * Drain stdin before exiting to prevent Kitty key release events from
61
+ * leaking to the parent shell over slow SSH connections.
62
+ * @param maxMs - Maximum time to drain (default: 1000ms)
63
+ * @param idleMs - Exit early if no input arrives within this time (default: 50ms)
64
+ */
65
+ drainInput(maxMs?: number, idleMs?: number): Promise<void>;
66
+
67
+ // Write output to terminal
68
+ write(data: string): void;
69
+
70
+ // Get terminal dimensions
71
+ get columns(): number;
72
+ get rows(): number;
73
+
74
+ // Whether Kitty keyboard protocol is active
75
+ get kittyProtocolActive(): boolean;
76
+
77
+ // Cursor positioning (relative to current position)
78
+ moveBy(lines: number): void; // Move cursor up (negative) or down (positive) by N lines
79
+
80
+ // Cursor visibility
81
+ hideCursor(): void; // Hide the cursor
82
+ showCursor(): void; // Show the cursor
83
+
84
+ // Clear operations
85
+ clearLine(): void; // Clear current line
86
+ clearFromCursor(): void; // Clear from cursor to end of screen
87
+ clearScreen(): void; // Clear entire screen and move cursor to (0,0)
88
+
89
+ // Title operations
90
+ setTitle(title: string): void; // Set terminal window title
91
+
92
+ // Progress indicator (OSC 9;4)
93
+ setProgress(active: boolean): void;
94
+
95
+ /**
96
+ * Register a callback for terminal appearance (dark/light) changes.
97
+ * Detection uses OSC 11 background color query with Mode 2031 as a change trigger.
98
+ * Fires when the detected appearance changes, including the initial detection.
99
+ */
100
+ onAppearanceChange(callback: (appearance: TerminalAppearance) => void): void;
101
+
102
+ /** The last detected terminal appearance, or undefined if not yet known. */
103
+ get appearance(): TerminalAppearance | undefined;
104
+ }
105
+
106
+ function isWindowsSubsystemForLinux(): boolean {
107
+ return process.platform === "linux" && (!!$env.WSL_DISTRO_NAME || !!$env.WSL_INTEROP);
108
+ }
109
+
110
+ /**
111
+ * Real terminal using process.stdin/stdout
112
+ */
113
+ export class ProcessTerminal implements Terminal {
114
+ #wasRaw = false;
115
+ #inputHandler?: (data: string) => void;
116
+ #resizeHandler?: () => void;
117
+ #kittyProtocolActive = false;
118
+ #modifyOtherKeysActive = false;
119
+ #modifyOtherKeysTimeout?: Timer;
120
+ #stdinBuffer?: StdinBuffer;
121
+ #stdinDataHandler?: (data: string) => void;
122
+ #dead = false;
123
+ #writeLogPath = $env.PI_TUI_WRITE_LOG || "";
124
+ #windowsVTInputRestore?: () => void;
125
+ #appearanceCallbacks: Array<(appearance: TerminalAppearance) => void> = [];
126
+ #appearance: TerminalAppearance | undefined;
127
+ #osc11Pending = false;
128
+ #osc11QueryQueued = false;
129
+ #osc11ResponseBuffer = "";
130
+ #privateCsiResponseBuffer = "";
131
+ #pendingDa1Sentinels = 0;
132
+ #osc11PollTimer?: Timer;
133
+ #mode2031DebounceTimer?: Timer;
134
+ #progressTimer?: ReturnType<typeof setInterval>;
135
+
136
+ get kittyProtocolActive(): boolean {
137
+ return this.#kittyProtocolActive;
138
+ }
139
+
140
+ get appearance(): TerminalAppearance | undefined {
141
+ return this.#appearance;
142
+ }
143
+
144
+ onAppearanceChange(callback: (appearance: TerminalAppearance) => void): void {
145
+ this.#appearanceCallbacks.push(callback);
146
+ }
147
+
148
+ start(onInput: (data: string) => void, onResize: () => void): void {
149
+ this.#inputHandler = onInput;
150
+ this.#resizeHandler = onResize;
151
+
152
+ // Register for emergency cleanup
153
+ activeTerminal = this;
154
+ terminalEverStarted = true;
155
+
156
+ // Save previous state and enable raw mode
157
+ this.#wasRaw = process.stdin.isRaw || false;
158
+ if (process.stdin.setRawMode) {
159
+ process.stdin.setRawMode(true);
160
+ }
161
+ process.stdin.setEncoding("utf8");
162
+ process.stdin.resume();
163
+
164
+ // Enable bracketed paste mode - terminal will wrap pastes in \x1b[200~ ... \x1b[201~
165
+ this.#safeWrite("\x1b[?2004h");
166
+
167
+ // Set up resize handler immediately
168
+ process.stdout.on("resize", this.#resizeHandler);
169
+
170
+ // Refresh terminal dimensions - they may be stale after suspend/resume
171
+ // (SIGWINCH is lost while process is stopped). Unix only.
172
+ if (process.platform !== "win32") {
173
+ process.kill(process.pid, "SIGWINCH");
174
+ }
175
+
176
+ // On Windows, enable ENABLE_VIRTUAL_TERMINAL_INPUT so the console sends
177
+ // VT escape sequences (e.g. \x1b[Z for Shift+Tab) instead of raw console
178
+ // events that lose modifier information. Must run after setRawMode(true)
179
+ // since that resets console mode flags.
180
+ this.#enableWindowsVTInput();
181
+ // Query and enable Kitty keyboard protocol
182
+ // The query handler intercepts input temporarily, then installs the user's handler
183
+ // See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/
184
+ this.#queryAndEnableKittyProtocol();
185
+
186
+ // Query terminal background color via OSC 11 for dark/light detection.
187
+ // Uses DA1 (Primary Device Attributes) as a sentinel: terminals process
188
+ // sequences in order, so if DA1 arrives before OSC 11 response,
189
+ // the terminal does not support OSC 11. This avoids indefinite hangs.
190
+ // Technique used by Neovim, bat, fish, and terminal-colorsaurus.
191
+ this.#queryBackgroundColor();
192
+
193
+ // Subscribe to Mode 2031 appearance change notifications.
194
+ // When the terminal reports a change, we re-query OSC 11 to get the
195
+ // actual background color (following Neovim convention) with 100ms debounce.
196
+ this.#safeWrite("\x1b[?2031h");
197
+
198
+ // Start periodic OSC 11 re-query for terminals without Mode 2031
199
+ // (Warp, Alacritty, WezTerm, iTerm2). Self-disables once Mode 2031 fires.
200
+ // Windows Terminal under WSL has been observed to close the hosting tab
201
+ // after repeated OSC 11/DA1 probes. Keep the initial/event-driven probes,
202
+ // but avoid background polling there.
203
+ if (!isWindowsSubsystemForLinux()) {
204
+ this.#startOsc11Poll();
205
+ }
206
+ }
207
+
208
+ /**
209
+ * On Windows, add ENABLE_VIRTUAL_TERMINAL_INPUT to the stdin console mode
210
+ * so modified keys (for example Shift+Tab) arrive as VT escape sequences.
211
+ */
212
+ #enableWindowsVTInput(): void {
213
+ if (process.platform !== "win32") return;
214
+ this.#restoreWindowsVTInput();
215
+ try {
216
+ const kernel32 = dlopen("kernel32.dll", {
217
+ GetStdHandle: { args: [FFIType.i32], returns: FFIType.ptr },
218
+ GetConsoleMode: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.bool },
219
+ SetConsoleMode: { args: [FFIType.ptr, FFIType.u32], returns: FFIType.bool },
220
+ });
221
+ const handle = kernel32.symbols.GetStdHandle(STD_INPUT_HANDLE);
222
+ const mode = new Uint32Array(1);
223
+ const modePtr = ptr(mode);
224
+ if (!modePtr || !kernel32.symbols.GetConsoleMode(handle, modePtr)) {
225
+ kernel32.close();
226
+ return;
227
+ }
228
+ const originalMode = mode[0]!;
229
+ const vtMode = originalMode | ENABLE_VIRTUAL_TERMINAL_INPUT;
230
+ if (vtMode !== originalMode && !kernel32.symbols.SetConsoleMode(handle, vtMode)) {
231
+ kernel32.close();
232
+ return;
233
+ }
234
+ this.#windowsVTInputRestore = () => {
235
+ try {
236
+ kernel32.symbols.SetConsoleMode(handle, originalMode);
237
+ } finally {
238
+ kernel32.close();
239
+ }
240
+ };
241
+ } catch {
242
+ // bun:ffi unavailable or console API unsupported; keep startup non-fatal.
243
+ }
244
+ }
245
+
246
+ #restoreWindowsVTInput(): void {
247
+ if (process.platform !== "win32") return;
248
+ const restore = this.#windowsVTInputRestore;
249
+ this.#windowsVTInputRestore = undefined;
250
+ if (!restore) return;
251
+ try {
252
+ restore();
253
+ } catch {
254
+ // Ignore restore errors during terminal teardown.
255
+ }
256
+ }
257
+
258
+ /**
259
+ * Set up StdinBuffer to split batched input into individual sequences.
260
+ * This ensures components receive single events, making matchesKey/isKeyRelease work correctly.
261
+ *
262
+ * Also watches for Kitty protocol response and enables it when detected.
263
+ * This is done here (after stdinBuffer parsing) rather than on raw stdin
264
+ * to handle the case where the response arrives split across multiple events.
265
+ */
266
+ #setupStdinBuffer(): void {
267
+ this.#stdinBuffer = new StdinBuffer({ timeout: 10 });
268
+
269
+ // Kitty protocol response pattern: \x1b[?<flags>u
270
+ const kittyResponsePattern = /^\x1b\[\?(\d+)u$/;
271
+
272
+ // Mode 2031 DSR response: \x1b[?997;{1=dark,2=light}n
273
+ const appearanceDsrPattern = /^\x1b\[\?997;([12])n$/;
274
+
275
+ // OSC 11 response: \x1b]11;rgb:RR/GG/BB or rgba:RR/GG/BB, terminated by BEL or ST.
276
+ const osc11ResponsePattern =
277
+ /^\x1b\]11;rgba?:([0-9a-fA-F]{1,4})\/([0-9a-fA-F]{1,4})\/([0-9a-fA-F]{1,4})(?:\x07|\x1b\\)$/;
278
+
279
+ // DA1 (Primary Device Attributes) response: \x1b[?...c
280
+ const da1ResponsePattern = /^\x1b\[\?[\d;]*c$/;
281
+
282
+ // Private CSI partial: \x1b[?<digits/semicolons>... — incomplete probe response
283
+ // that the StdinBuffer flushed before the terminator arrived (split across
284
+ // stdin reads). Used to reassemble DA1, kitty, and Mode 2031 replies.
285
+ const privateCsiPartialPattern = /^\x1b\[\?[\d;]*$/;
286
+
287
+ // Forward individual sequences to the input handler
288
+ this.#stdinBuffer.on("data", (sequence: string) => {
289
+ // Reassemble split private CSI responses (DA1, kitty keyboard, Mode 2031).
290
+ // When the terminal writes the response slowly enough that the StdinBuffer's
291
+ // flush timeout elapses mid-sequence, the prefix `\x1b[?<digits>` arrives as
292
+ // one event and the tail `;...<terminator>` arrives as individual character
293
+ // events that would otherwise leak into the prompt as keystrokes. See #1238.
294
+ if (
295
+ this.#privateCsiResponseBuffer ||
296
+ (privateCsiPartialPattern.test(sequence) && this.#pendingDa1Sentinels > 0)
297
+ ) {
298
+ if (this.#privateCsiResponseBuffer && sequence.startsWith("\x1b")) {
299
+ // New escape arrived mid-reassembly — abandon partial and re-process the new sequence.
300
+ this.#privateCsiResponseBuffer = "";
301
+ } else {
302
+ this.#privateCsiResponseBuffer += sequence;
303
+ // Cap accumulator to defend against runaway partials if the terminator never arrives.
304
+ if (this.#privateCsiResponseBuffer.length > 256) {
305
+ this.#privateCsiResponseBuffer = "";
306
+ return;
307
+ }
308
+ const lastChar = this.#privateCsiResponseBuffer.at(-1)!;
309
+ const lastCode = lastChar.charCodeAt(0);
310
+ if (lastCode >= 0x40 && lastCode <= 0x7e) {
311
+ // Terminator byte arrived. Fall through to the pattern checks with the
312
+ // reassembled sequence so the existing DA1/kitty/Mode 2031 handlers run.
313
+ sequence = this.#privateCsiResponseBuffer;
314
+ this.#privateCsiResponseBuffer = "";
315
+ } else if (!privateCsiPartialPattern.test(this.#privateCsiResponseBuffer)) {
316
+ // Diverged from a valid private CSI prefix (unexpected byte). Drop the
317
+ // probe noise we ate; do not forward to the input handler.
318
+ this.#privateCsiResponseBuffer = "";
319
+ return;
320
+ } else {
321
+ // Still accumulating.
322
+ return;
323
+ }
324
+ }
325
+ }
326
+
327
+ // Check for Kitty protocol response (only if not already enabled)
328
+ if (!this.#kittyProtocolActive) {
329
+ const match = sequence.match(kittyResponsePattern);
330
+ if (match) {
331
+ if (this.#modifyOtherKeysTimeout) {
332
+ clearTimeout(this.#modifyOtherKeysTimeout);
333
+ this.#modifyOtherKeysTimeout = undefined;
334
+ }
335
+ this.#kittyProtocolActive = true;
336
+ setKittyProtocolActive(true);
337
+
338
+ // Enable Kitty keyboard protocol (push flags)
339
+ // Flag 1 = disambiguate escape codes
340
+ // Flag 2 = report event types (press/repeat/release)
341
+ // Flag 4 = report alternate keys
342
+ this.#safeWrite("\x1b[>7u");
343
+ return; // Don't forward protocol response to TUI
344
+ }
345
+ }
346
+
347
+ // DA1 response: swallow our sentinel reply regardless of whether OSC 11
348
+ // already succeeded. Other terminal probes should never see these replies.
349
+ if (da1ResponsePattern.test(sequence) && this.#pendingDa1Sentinels > 0) {
350
+ this.#pendingDa1Sentinels--;
351
+ if (this.#osc11Pending) {
352
+ // DA1 arrived before OSC 11 response: terminal does not support
353
+ // OSC 11. Clear the pending state without starting a queued query
354
+ // (queued query is started below, after sentinel is consumed).
355
+ this.#osc11Pending = false;
356
+ this.#osc11ResponseBuffer = "";
357
+ }
358
+ // Now that this DA1 cycle is complete, start any queued query.
359
+ if (this.#osc11QueryQueued && !this.#dead) {
360
+ this.#osc11QueryQueued = false;
361
+ this.#startOsc11Query();
362
+ }
363
+ return;
364
+ }
365
+
366
+ // OSC 11 replies can be split if the stdin buffer flushes a partial sequence.
367
+ // Accumulate fragments until the BEL/ST terminator arrives, then parse once.
368
+ // If a new escape sequence arrives (not the ST terminator), abort buffering
369
+ // and forward it as normal input so user keystrokes are never swallowed.
370
+ if (this.#osc11Pending && (this.#osc11ResponseBuffer || sequence.startsWith("\x1b]11;"))) {
371
+ if (this.#osc11ResponseBuffer && sequence.startsWith("\x1b") && sequence !== "\x1b\\") {
372
+ // New escape sequence arrived mid-buffer — not an OSC 11 continuation.
373
+ this.#osc11ResponseBuffer = "";
374
+ // Fall through to normal input handling below.
375
+ } else {
376
+ this.#osc11ResponseBuffer += sequence;
377
+ const osc11Match = this.#osc11ResponseBuffer.match(osc11ResponsePattern);
378
+ if (!osc11Match) return;
379
+ const [, rHex, gHex, bHex] = osc11Match;
380
+ this.#osc11Pending = false;
381
+ this.#osc11ResponseBuffer = "";
382
+ this.#handleOsc11Response(rHex!, gHex!, bHex!);
383
+ return;
384
+ }
385
+ }
386
+
387
+ // Mode 2031 change notification: re-query OSC 11 with 100ms debounce
388
+ // (Neovim convention — coalesces rapid notifications during transitions)
389
+ const appearanceMatch = sequence.match(appearanceDsrPattern);
390
+ if (appearanceMatch) {
391
+ this.#stopOsc11Poll();
392
+ if (this.#mode2031DebounceTimer) clearTimeout(this.#mode2031DebounceTimer);
393
+ this.#mode2031DebounceTimer = setTimeout(() => {
394
+ this.#mode2031DebounceTimer = undefined;
395
+ this.#queryBackgroundColor();
396
+ }, 100);
397
+ return;
398
+ }
399
+ if (this.#inputHandler) {
400
+ this.#inputHandler(sequence);
401
+ }
402
+ });
403
+
404
+ // Re-wrap paste content with bracketed paste markers for existing editor handling
405
+ this.#stdinBuffer.on("paste", (content: string) => {
406
+ if (this.#inputHandler) {
407
+ this.#inputHandler(`\x1b[200~${content}\x1b[201~`);
408
+ }
409
+ });
410
+
411
+ // Handler that pipes stdin data through the buffer
412
+ this.#stdinDataHandler = (data: string) => {
413
+ this.#stdinBuffer!.process(data);
414
+ };
415
+ }
416
+
417
+ /**
418
+ * Send OSC 11 background color query followed by DA1 sentinel.
419
+ * DA1 avoids indefinite hangs: if DA1 response arrives before OSC 11,
420
+ * the terminal does not support OSC 11.
421
+ */
422
+ #queryBackgroundColor(): void {
423
+ if (this.#dead) return;
424
+ // Queue if an OSC 11 query is in flight or its DA1 sentinel hasn't been
425
+ // consumed yet. Starting a new query while a DA1 is outstanding would
426
+ // increment the sentinel counter, and the old DA1 arrival would then
427
+ // prematurely clear the new query's pending state.
428
+ if (this.#osc11Pending || this.#pendingDa1Sentinels > 0) {
429
+ this.#osc11QueryQueued = true;
430
+ return;
431
+ }
432
+ this.#startOsc11Query();
433
+ }
434
+
435
+ #startOsc11Query(): void {
436
+ this.#osc11Pending = true;
437
+ this.#osc11ResponseBuffer = "";
438
+ this.#pendingDa1Sentinels++;
439
+ this.#safeWrite("\x1b]11;?\x07"); // OSC 11 query (BEL terminated)
440
+ this.#safeWrite("\x1b[c"); // DA1 sentinel
441
+ }
442
+ /**
443
+ * Parse an OSC 11 background color response and compute BT.601 luminance.
444
+ * Handles 1-, 2-, 3-, and 4-digit XParseColor hex components.
445
+ */
446
+ #handleOsc11Response(rHex: string, gHex: string, bHex: string): void {
447
+ const normalize = (hex: string): number => {
448
+ const value = parseInt(hex, 16);
449
+ if (Number.isNaN(value)) return 0;
450
+ const max = 16 ** hex.length - 1;
451
+ return max > 0 ? value / max : 0;
452
+ };
453
+ const luminance = 0.299 * normalize(rHex) + 0.587 * normalize(gHex) + 0.114 * normalize(bHex);
454
+ const mode: TerminalAppearance = luminance < 0.5 ? "dark" : "light";
455
+ if (mode === this.#appearance) return;
456
+ this.#appearance = mode;
457
+ for (const cb of this.#appearanceCallbacks) {
458
+ try {
459
+ cb(mode);
460
+ } catch {
461
+ /* ignore callback errors */
462
+ }
463
+ }
464
+ }
465
+
466
+ /**
467
+ * Start periodic OSC 11 re-queries for terminals without Mode 2031 (Warp, Alacritty, WezTerm).
468
+ * Self-disables once Mode 2031 fires (push-based is better than polling).
469
+ */
470
+ #startOsc11Poll(): void {
471
+ this.#stopOsc11Poll();
472
+ this.#osc11PollTimer = setInterval(() => {
473
+ if (this.#dead) {
474
+ this.#stopOsc11Poll();
475
+ return;
476
+ }
477
+ this.#queryBackgroundColor();
478
+ }, 2_000);
479
+ this.#osc11PollTimer.unref();
480
+ }
481
+
482
+ #stopOsc11Poll(): void {
483
+ if (this.#osc11PollTimer) {
484
+ clearInterval(this.#osc11PollTimer);
485
+ this.#osc11PollTimer = undefined;
486
+ }
487
+ }
488
+
489
+ /**
490
+ * Query terminal for Kitty keyboard protocol support and enable if available.
491
+ *
492
+ * Sends CSI ? u to query current flags. If terminal responds with CSI ? <flags> u,
493
+ * it supports the protocol and we enable it with CSI > 1 u.
494
+ *
495
+ * The response is detected in setupStdinBuffer's data handler, which properly
496
+ * handles the case where the response arrives split across multiple stdin events.
497
+ */
498
+ #queryAndEnableKittyProtocol(): void {
499
+ this.#setupStdinBuffer();
500
+ process.stdin.on("data", this.#stdinDataHandler!);
501
+ this.#safeWrite("\x1b[?u");
502
+ this.#modifyOtherKeysTimeout = setTimeout(() => {
503
+ this.#modifyOtherKeysTimeout = undefined;
504
+ if (this.#kittyProtocolActive || this.#modifyOtherKeysActive) {
505
+ return;
506
+ }
507
+ this.#safeWrite("\x1b[>4;2m");
508
+ this.#modifyOtherKeysActive = true;
509
+ }, 150);
510
+ }
511
+
512
+ async drainInput(maxMs = 1000, idleMs = 50): Promise<void> {
513
+ if (this.#kittyProtocolActive) {
514
+ // Disable Kitty keyboard protocol first so any late key releases
515
+ // do not generate new Kitty escape sequences.
516
+ this.#safeWrite("\x1b[<u");
517
+ this.#kittyProtocolActive = false;
518
+ setKittyProtocolActive(false);
519
+ }
520
+ if (this.#modifyOtherKeysTimeout) {
521
+ clearTimeout(this.#modifyOtherKeysTimeout);
522
+ this.#modifyOtherKeysTimeout = undefined;
523
+ }
524
+ if (this.#modifyOtherKeysActive) {
525
+ this.#safeWrite("\x1b[>4;0m");
526
+ this.#modifyOtherKeysActive = false;
527
+ }
528
+
529
+ const previousHandler = this.#inputHandler;
530
+ this.#inputHandler = undefined;
531
+
532
+ let lastDataTime = Date.now();
533
+ const onData = () => {
534
+ lastDataTime = Date.now();
535
+ };
536
+
537
+ process.stdin.on("data", onData);
538
+ const endTime = Date.now() + maxMs;
539
+
540
+ try {
541
+ while (true) {
542
+ const now = Date.now();
543
+ const timeLeft = endTime - now;
544
+ if (timeLeft <= 0) break;
545
+ if (now - lastDataTime >= idleMs) break;
546
+ await new Promise(resolve => setTimeout(resolve, Math.min(idleMs, timeLeft)));
547
+ }
548
+ } finally {
549
+ process.stdin.removeListener("data", onData);
550
+ this.#inputHandler = previousHandler;
551
+ }
552
+ }
553
+
554
+ stop(): void {
555
+ // Unregister from emergency cleanup
556
+ if (activeTerminal === this) {
557
+ activeTerminal = null;
558
+ }
559
+
560
+ if (this.#clearProgressTimer()) {
561
+ this.#safeWrite(TERMINAL_PROGRESS_CLEAR_SEQUENCE);
562
+ }
563
+
564
+ // Disable bracketed paste mode
565
+ this.#safeWrite("\x1b[?2004l");
566
+
567
+ // Disable Mode 2031 appearance change notifications
568
+ this.#safeWrite("\x1b[?2031l");
569
+ this.#stopOsc11Poll();
570
+ if (this.#mode2031DebounceTimer) {
571
+ clearTimeout(this.#mode2031DebounceTimer);
572
+ this.#mode2031DebounceTimer = undefined;
573
+ }
574
+ this.#appearanceCallbacks = [];
575
+ this.#osc11Pending = false;
576
+ this.#osc11QueryQueued = false;
577
+ this.#osc11ResponseBuffer = "";
578
+ this.#privateCsiResponseBuffer = "";
579
+ this.#pendingDa1Sentinels = 0;
580
+
581
+ // Disable Kitty keyboard protocol if not already done by drainInput()
582
+ if (this.#kittyProtocolActive) {
583
+ this.#safeWrite("\x1b[<u");
584
+ this.#kittyProtocolActive = false;
585
+ setKittyProtocolActive(false);
586
+ }
587
+ if (this.#modifyOtherKeysTimeout) {
588
+ clearTimeout(this.#modifyOtherKeysTimeout);
589
+ this.#modifyOtherKeysTimeout = undefined;
590
+ }
591
+ if (this.#modifyOtherKeysActive) {
592
+ this.#safeWrite("\x1b[>4;0m");
593
+ this.#modifyOtherKeysActive = false;
594
+ }
595
+
596
+ this.#restoreWindowsVTInput();
597
+ // Clean up StdinBuffer
598
+ if (this.#stdinBuffer) {
599
+ this.#stdinBuffer.destroy();
600
+ this.#stdinBuffer = undefined;
601
+ }
602
+
603
+ // Remove event handlers
604
+ if (this.#stdinDataHandler) {
605
+ process.stdin.removeListener("data", this.#stdinDataHandler);
606
+ this.#stdinDataHandler = undefined;
607
+ }
608
+ this.#inputHandler = undefined;
609
+ this.#appearance = undefined;
610
+ if (this.#resizeHandler) {
611
+ process.stdout.removeListener("resize", this.#resizeHandler);
612
+ this.#resizeHandler = undefined;
613
+ }
614
+
615
+ // Pause stdin to prevent any buffered input (e.g., Ctrl+D) from being
616
+ // re-interpreted after raw mode is disabled. This fixes a race condition
617
+ // where Ctrl+D could close the parent shell over SSH.
618
+ process.stdin.pause();
619
+
620
+ // Restore raw mode state
621
+ if (process.stdin.setRawMode) {
622
+ process.stdin.setRawMode(this.#wasRaw);
623
+ }
624
+ }
625
+
626
+ write(data: string): void {
627
+ this.#safeWrite(data);
628
+ if (this.#writeLogPath) {
629
+ try {
630
+ fs.appendFileSync(this.#writeLogPath, data, { encoding: "utf8" });
631
+ } catch {
632
+ // Ignore logging errors
633
+ }
634
+ }
635
+ }
636
+
637
+ #safeWrite(data: string): void {
638
+ if (this.#dead) return;
639
+ // Skip control sequences when stdout isn't a TTY (piped output, tests, log
640
+ // files). They serve no purpose there and would surface as visible noise.
641
+ if (!process.stdout.isTTY) return;
642
+ try {
643
+ process.stdout.write(data);
644
+ } catch (err) {
645
+ // Any write failure means terminal is dead - no recovery possible
646
+ this.#dead = true;
647
+ logger.warn("terminal is dead - no recovery possible", { error: err, data });
648
+ }
649
+ }
650
+
651
+ get columns(): number {
652
+ return process.stdout.columns || Number(Bun.env.COLUMNS) || 80;
653
+ }
654
+
655
+ get rows(): number {
656
+ return process.stdout.rows || Number(Bun.env.LINES) || 24;
657
+ }
658
+
659
+ moveBy(lines: number): void {
660
+ if (lines > 0) {
661
+ // Move down
662
+ this.#safeWrite(`\x1b[${lines}B`);
663
+ } else if (lines < 0) {
664
+ // Move up
665
+ this.#safeWrite(`\x1b[${-lines}A`);
666
+ }
667
+ // lines === 0: no movement
668
+ }
669
+
670
+ hideCursor(): void {
671
+ this.#safeWrite("\x1b[?25l");
672
+ }
673
+
674
+ showCursor(): void {
675
+ this.#safeWrite("\x1b[?25h");
676
+ }
677
+
678
+ clearLine(): void {
679
+ this.#safeWrite("\x1b[K");
680
+ }
681
+
682
+ clearFromCursor(): void {
683
+ this.#safeWrite("\x1b[J");
684
+ }
685
+
686
+ clearScreen(): void {
687
+ this.#safeWrite("\x1b[H\x1b[0J"); // Move to home (1,1) and clear from cursor to end
688
+ }
689
+
690
+ setTitle(title: string): void {
691
+ // OSC 0;title BEL - set terminal window title
692
+ this.#safeWrite(`\x1b]0;${title}\x07`);
693
+ }
694
+
695
+ setProgress(active: boolean): void {
696
+ if (active) {
697
+ this.#safeWrite(TERMINAL_PROGRESS_ACTIVE_SEQUENCE);
698
+ if (!this.#progressTimer) {
699
+ this.#progressTimer = setInterval(() => {
700
+ this.#safeWrite(TERMINAL_PROGRESS_ACTIVE_SEQUENCE);
701
+ }, TERMINAL_PROGRESS_KEEPALIVE_MS);
702
+ this.#progressTimer.unref?.();
703
+ }
704
+ } else {
705
+ this.#clearProgressTimer();
706
+ this.#safeWrite(TERMINAL_PROGRESS_CLEAR_SEQUENCE);
707
+ }
708
+ }
709
+
710
+ #clearProgressTimer(): boolean {
711
+ if (!this.#progressTimer) return false;
712
+ clearInterval(this.#progressTimer);
713
+ this.#progressTimer = undefined;
714
+ return true;
715
+ }
716
+ }