@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
package/src/tui.ts ADDED
@@ -0,0 +1,1481 @@
1
+ /**
2
+ * Minimal TUI implementation with differential rendering
3
+ */
4
+ import * as fs from "node:fs";
5
+ import * as path from "node:path";
6
+ import { performance } from "node:perf_hooks";
7
+ import { $flag, getDebugLogPath } from "@gajae-code/utils";
8
+ import { isKeyRelease, matchesKey } from "./keys";
9
+ import type { Terminal } from "./terminal";
10
+ import { ImageProtocol, setCellDimensions, setTerminalImageProtocol, TERMINAL } from "./terminal-capabilities";
11
+ import {
12
+ Ellipsis,
13
+ extractSegments,
14
+ normalizeTerminalOutput,
15
+ sliceByColumn,
16
+ sliceWithWidth,
17
+ truncateToWidth,
18
+ visibleWidth,
19
+ } from "./utils";
20
+
21
+ const SEGMENT_RESET = "\x1b[0m";
22
+ /**
23
+ * Per-line terminator written at the end of every non-image line. Closes both
24
+ * SGR state and any in-flight OSC 8 hyperlink so styles/links cannot bleed
25
+ * across lines in scrollback. Applied by {@link TUI.#applyLineResets} before
26
+ * diffing so `#previousLines` mirrors what was actually written.
27
+ */
28
+ const LINE_TERMINATOR = "\x1b[0m\x1b]8;;\x07";
29
+
30
+ type InputListenerResult = { consume?: boolean; data?: string } | undefined;
31
+ type InputListener = (data: string) => InputListenerResult;
32
+
33
+ /**
34
+ * Component interface - all components must implement this
35
+ */
36
+ export interface Component {
37
+ /**
38
+ * Render the component to lines for the given viewport width
39
+ * @param width - Current viewport width
40
+ * @returns Array of strings, each representing a line
41
+ */
42
+ render(width: number): string[];
43
+
44
+ /**
45
+ * Optional handler for keyboard input when component has focus
46
+ */
47
+ handleInput?(data: string): void;
48
+
49
+ /**
50
+ * If true, component receives key release events (Kitty protocol).
51
+ * Default is false - release events are filtered out.
52
+ */
53
+ wantsKeyRelease?: boolean;
54
+
55
+ /**
56
+ * Invalidate any cached rendering state.
57
+ * Called when theme changes or when component needs to re-render from scratch.
58
+ */
59
+ invalidate(): void;
60
+ }
61
+
62
+ /**
63
+ * Interface for components that can receive focus and display a hardware cursor.
64
+ * When focused, the component should emit CURSOR_MARKER at the cursor position
65
+ * in its render output. TUI will find this marker and position the hardware
66
+ * cursor there for proper IME candidate window positioning.
67
+ */
68
+ export interface Focusable {
69
+ /** Set by TUI when focus changes. Component should emit CURSOR_MARKER when true. */
70
+ focused: boolean;
71
+ }
72
+
73
+ /** Type guard to check if a component implements Focusable */
74
+ export function isFocusable(component: Component | null): component is Component & Focusable {
75
+ return component !== null && "focused" in component;
76
+ }
77
+
78
+ /**
79
+ * Cursor position marker - APC (Application Program Command) sequence.
80
+ * This is a zero-width escape sequence that terminals ignore.
81
+ * Components emit this at the cursor position when focused.
82
+ * TUI finds and strips this marker, then positions the hardware cursor there.
83
+ */
84
+ export const CURSOR_MARKER = "\x1b_pi:c\x07";
85
+
86
+ export { visibleWidth };
87
+
88
+ /**
89
+ * Anchor position for overlays
90
+ */
91
+ export type OverlayAnchor =
92
+ | "center"
93
+ | "top-left"
94
+ | "top-right"
95
+ | "bottom-left"
96
+ | "bottom-right"
97
+ | "top-center"
98
+ | "bottom-center"
99
+ | "left-center"
100
+ | "right-center";
101
+
102
+ /**
103
+ * Margin configuration for overlays
104
+ */
105
+ export interface OverlayMargin {
106
+ top?: number;
107
+ right?: number;
108
+ bottom?: number;
109
+ left?: number;
110
+ }
111
+
112
+ /** Value that can be absolute (number) or percentage (string like "50%") */
113
+ export type SizeValue = number | `${number}%`;
114
+
115
+ /** Parse a SizeValue into absolute value given a reference size */
116
+ function parseSizeValue(value: SizeValue | undefined, referenceSize: number): number | undefined {
117
+ if (value === undefined) return undefined;
118
+ if (typeof value === "number") return value;
119
+ // Parse percentage string like "50%"
120
+ const match = value.match(/^(\d+(?:\.\d+)?)%$/);
121
+ if (match) {
122
+ return Math.floor((referenceSize * parseFloat(match[1])) / 100);
123
+ }
124
+ return undefined;
125
+ }
126
+
127
+ function isTermuxSession(): boolean {
128
+ return Boolean(process.env.TERMUX_VERSION);
129
+ }
130
+
131
+ /** Detect terminal multiplexers where scrollback clearing and height-change redraws are hostile. */
132
+ function isMultiplexerSession(): boolean {
133
+ return Boolean(Bun.env.TMUX || Bun.env.STY || Bun.env.ZELLIJ);
134
+ }
135
+
136
+ function useLegacyMultiplexerFullRender(): boolean {
137
+ return $flag("PI_TUI_LEGACY_MULTIPLEXER_FULL_RENDER");
138
+ }
139
+
140
+ /**
141
+ * Options for overlay positioning and sizing.
142
+ * Values can be absolute numbers or percentage strings (e.g., "50%").
143
+ */
144
+ export interface OverlayOptions {
145
+ // === Sizing ===
146
+ /** Width in columns, or percentage of terminal width (e.g., "50%") */
147
+ width?: SizeValue;
148
+ /** Minimum width in columns */
149
+ minWidth?: number;
150
+ /** Maximum height in rows, or percentage of terminal height (e.g., "50%") */
151
+ maxHeight?: SizeValue;
152
+
153
+ // === Positioning - anchor-based ===
154
+ /** Anchor point for positioning (default: 'center') */
155
+ anchor?: OverlayAnchor;
156
+ /** Horizontal offset from anchor position (positive = right) */
157
+ offsetX?: number;
158
+ /** Vertical offset from anchor position (positive = down) */
159
+ offsetY?: number;
160
+
161
+ // === Positioning - percentage or absolute ===
162
+ /** Row position: absolute number, or percentage (e.g., "25%" = 25% from top) */
163
+ row?: SizeValue;
164
+ /** Column position: absolute number, or percentage (e.g., "50%" = centered horizontally) */
165
+ col?: SizeValue;
166
+
167
+ // === Margin from terminal edges ===
168
+ /** Margin from terminal edges. Number applies to all sides. */
169
+ margin?: OverlayMargin | number;
170
+
171
+ // === Visibility ===
172
+ /**
173
+ * Control overlay visibility based on terminal dimensions.
174
+ * If provided, overlay is only rendered when this returns true.
175
+ * Called each render cycle with current terminal dimensions.
176
+ */
177
+ visible?: (termWidth: number, termHeight: number) => boolean;
178
+ }
179
+
180
+ /**
181
+ * Handle returned by showOverlay for controlling the overlay
182
+ */
183
+ export interface OverlayHandle {
184
+ /** Permanently remove the overlay (cannot be shown again) */
185
+ hide(): void;
186
+ /** Temporarily hide or show the overlay */
187
+ setHidden(hidden: boolean): void;
188
+ /** Check if overlay is temporarily hidden */
189
+ isHidden(): boolean;
190
+ }
191
+
192
+ /**
193
+ * Container - a component that contains other components
194
+ */
195
+ export class Container implements Component {
196
+ children: Component[] = [];
197
+
198
+ addChild(component: Component): void {
199
+ this.children.push(component);
200
+ }
201
+
202
+ removeChild(component: Component): void {
203
+ const index = this.children.indexOf(component);
204
+ if (index !== -1) {
205
+ this.children.splice(index, 1);
206
+ }
207
+ }
208
+
209
+ clear(): void {
210
+ this.children = [];
211
+ }
212
+
213
+ invalidate(): void {
214
+ for (const child of this.children) {
215
+ child.invalidate?.();
216
+ }
217
+ }
218
+
219
+ render(width: number): string[] {
220
+ width = Math.max(1, width);
221
+ const lines: string[] = [];
222
+ for (const child of this.children) {
223
+ lines.push(...child.render(width));
224
+ }
225
+ return lines;
226
+ }
227
+ }
228
+
229
+ /**
230
+ * TUI - Main class for managing terminal UI with differential rendering
231
+ */
232
+ export class TUI extends Container {
233
+ terminal: Terminal;
234
+ #previousLines: string[] = [];
235
+ #previousWidth = 0;
236
+ #previousHeight = 0;
237
+ #focusedComponent: Component | null = null;
238
+ #inputListeners = new Set<InputListener>();
239
+
240
+ /** Global callback for debug key (Shift+Ctrl+D). Called before input is forwarded to focused component. */
241
+ onDebug?: () => void;
242
+ #renderRequested = false;
243
+ #renderTimer: NodeJS.Timeout | undefined;
244
+ #lastRenderAt = 0;
245
+ static readonly #MIN_RENDER_INTERVAL_MS = 16;
246
+ #cursorRow = 0; // Logical cursor row (end of rendered content)
247
+ #hardwareCursorRow = 0; // Actual terminal cursor row (may differ due to IME positioning)
248
+ #viewportTopRow = 0; // Content row currently mapped to screen row 0
249
+ #sixelProbePendingDa = false;
250
+ #sixelProbePendingGraphics = false;
251
+ #sixelProbeBuffer = "";
252
+ #sixelProbeTimeout?: NodeJS.Timeout;
253
+ #sixelProbeUnsubscribe?: () => void;
254
+ #showHardwareCursor = $flag("PI_HARDWARE_CURSOR");
255
+ #clearOnShrink = $flag("PI_CLEAR_ON_SHRINK"); // Clear empty rows when content shrinks (default: off)
256
+ #maxLinesRendered = 0; // Line count from last render, used for viewport calculation
257
+ #fullRedrawCount = 0;
258
+ #stopped = false;
259
+
260
+ // Overlay stack for modal components rendered on top of base content
261
+ overlayStack: {
262
+ component: Component;
263
+ options?: OverlayOptions;
264
+ preFocus: Component | null;
265
+ hidden: boolean;
266
+ }[] = [];
267
+
268
+ constructor(terminal: Terminal, showHardwareCursor?: boolean) {
269
+ super();
270
+ this.terminal = terminal;
271
+ if (showHardwareCursor !== undefined) {
272
+ this.#showHardwareCursor = showHardwareCursor;
273
+ }
274
+ }
275
+
276
+ get fullRedraws(): number {
277
+ return this.#fullRedrawCount;
278
+ }
279
+
280
+ getShowHardwareCursor(): boolean {
281
+ return this.#showHardwareCursor;
282
+ }
283
+
284
+ setShowHardwareCursor(enabled: boolean): void {
285
+ if (this.#showHardwareCursor === enabled) return;
286
+ this.#showHardwareCursor = enabled;
287
+ if (!enabled) {
288
+ this.terminal.hideCursor();
289
+ }
290
+ this.requestRender();
291
+ }
292
+
293
+ getClearOnShrink(): boolean {
294
+ return this.#clearOnShrink;
295
+ }
296
+
297
+ /**
298
+ * Set whether to trigger full re-render when content shrinks.
299
+ * When true (default), empty rows are cleared when content shrinks.
300
+ * When false, empty rows remain (reduces redraws on slower terminals).
301
+ */
302
+ setClearOnShrink(enabled: boolean): void {
303
+ this.#clearOnShrink = enabled;
304
+ }
305
+
306
+ setFocus(component: Component | null): void {
307
+ // Clear focused flag on old component
308
+ if (isFocusable(this.#focusedComponent)) {
309
+ this.#focusedComponent.focused = false;
310
+ }
311
+
312
+ this.#focusedComponent = component;
313
+
314
+ // Set focused flag on new component
315
+ if (isFocusable(component)) {
316
+ component.focused = true;
317
+ }
318
+ }
319
+
320
+ /**
321
+ * Show an overlay component with configurable positioning and sizing.
322
+ * Returns a handle to control the overlay's visibility.
323
+ */
324
+ showOverlay(component: Component, options?: OverlayOptions): OverlayHandle {
325
+ const entry = { component, options, preFocus: this.#focusedComponent, hidden: false };
326
+ this.overlayStack.push(entry);
327
+ // Only focus if overlay is actually visible
328
+ if (this.#isOverlayVisible(entry)) {
329
+ this.setFocus(component);
330
+ }
331
+ this.terminal.hideCursor();
332
+ this.requestRender();
333
+
334
+ // Return handle for controlling this overlay
335
+ return {
336
+ hide: () => {
337
+ const index = this.overlayStack.indexOf(entry);
338
+ if (index !== -1) {
339
+ this.overlayStack.splice(index, 1);
340
+ // Restore focus if this overlay had focus
341
+ if (this.#focusedComponent === component) {
342
+ const topVisible = this.#getTopmostVisibleOverlay();
343
+ this.setFocus(topVisible?.component ?? entry.preFocus);
344
+ }
345
+ if (this.overlayStack.length === 0) this.terminal.hideCursor();
346
+ this.requestRender();
347
+ }
348
+ },
349
+ setHidden: (hidden: boolean) => {
350
+ if (entry.hidden === hidden) return;
351
+ entry.hidden = hidden;
352
+ // Update focus when hiding/showing
353
+ if (hidden) {
354
+ // If this overlay had focus, move focus to next visible or preFocus
355
+ if (this.#focusedComponent === component) {
356
+ const topVisible = this.#getTopmostVisibleOverlay();
357
+ this.setFocus(topVisible?.component ?? entry.preFocus);
358
+ }
359
+ } else {
360
+ // Restore focus to this overlay when showing (if it's actually visible)
361
+ if (this.#isOverlayVisible(entry)) {
362
+ this.setFocus(component);
363
+ }
364
+ }
365
+ this.requestRender();
366
+ },
367
+ isHidden: () => entry.hidden,
368
+ };
369
+ }
370
+
371
+ /** Hide the topmost overlay and restore previous focus. */
372
+ hideOverlay(): void {
373
+ const overlay = this.overlayStack.pop();
374
+ if (!overlay) return;
375
+ // Find topmost visible overlay, or fall back to preFocus
376
+ const topVisible = this.#getTopmostVisibleOverlay();
377
+ this.setFocus(topVisible?.component ?? overlay.preFocus);
378
+ if (this.overlayStack.length === 0) this.terminal.hideCursor();
379
+ this.requestRender();
380
+ }
381
+
382
+ /** Check if there are any visible overlays */
383
+ hasOverlay(): boolean {
384
+ return this.overlayStack.some(o => this.#isOverlayVisible(o));
385
+ }
386
+
387
+ /** Check if an overlay entry is currently visible */
388
+ #isOverlayVisible(entry: (typeof this.overlayStack)[number]): boolean {
389
+ if (entry.hidden) return false;
390
+ if (entry.options?.visible) {
391
+ return entry.options.visible(this.terminal.columns, this.terminal.rows);
392
+ }
393
+ return true;
394
+ }
395
+
396
+ /** Find the topmost visible overlay, if any */
397
+ #getTopmostVisibleOverlay(): (typeof this.overlayStack)[number] | undefined {
398
+ for (let i = this.overlayStack.length - 1; i >= 0; i--) {
399
+ if (this.#isOverlayVisible(this.overlayStack[i])) {
400
+ return this.overlayStack[i];
401
+ }
402
+ }
403
+ return undefined;
404
+ }
405
+
406
+ override invalidate(): void {
407
+ super.invalidate();
408
+ for (const overlay of this.overlayStack) overlay.component.invalidate?.();
409
+ }
410
+
411
+ start(): void {
412
+ this.#stopped = false;
413
+ this.terminal.start(
414
+ data => this.#handleInput(data),
415
+ () => this.requestRender(),
416
+ );
417
+ this.terminal.hideCursor();
418
+ this.#querySixelSupport();
419
+ this.#queryCellSize();
420
+ this.requestRender(true);
421
+ }
422
+
423
+ addInputListener(listener: InputListener): () => void {
424
+ this.#inputListeners.add(listener);
425
+ return () => {
426
+ this.#inputListeners.delete(listener);
427
+ };
428
+ }
429
+
430
+ removeInputListener(listener: InputListener): void {
431
+ this.#inputListeners.delete(listener);
432
+ }
433
+
434
+ #querySixelSupport(): void {
435
+ if (TERMINAL.imageProtocol) return;
436
+ if (process.platform !== "win32") return;
437
+ if (!Bun.env.WT_SESSION) return;
438
+ if (!process.stdin.isTTY || !process.stdout.isTTY) return;
439
+
440
+ this.#clearSixelProbeState();
441
+ this.#sixelProbePendingDa = true;
442
+ this.#sixelProbePendingGraphics = true;
443
+ this.#sixelProbeUnsubscribe = this.addInputListener(data => this.#handleSixelProbeInput(data));
444
+ this.terminal.write("\x1b[c");
445
+ this.terminal.write("\x1b[?2;1;0S");
446
+ this.#sixelProbeTimeout = setTimeout(() => {
447
+ this.#finishSixelProbe(false);
448
+ }, 250);
449
+ }
450
+
451
+ #handleSixelProbeInput(data: string): InputListenerResult {
452
+ if (!this.#sixelProbePendingDa && !this.#sixelProbePendingGraphics) {
453
+ return undefined;
454
+ }
455
+
456
+ this.#sixelProbeBuffer += data;
457
+ let passthrough = "";
458
+ let probeOutcome: boolean | null = null;
459
+
460
+ while (this.#sixelProbeBuffer.length > 0) {
461
+ const daMatch = this.#sixelProbeBuffer.match(/\x1b\[\?([0-9;]+)c/u);
462
+ const graphicsMatch = this.#sixelProbeBuffer.match(/\x1b\[\?2;(\d+);([0-9;]+)S/u);
463
+
464
+ if (!daMatch && !graphicsMatch) break;
465
+
466
+ const daIndex = daMatch?.index ?? Number.POSITIVE_INFINITY;
467
+ const graphicsIndex = graphicsMatch?.index ?? Number.POSITIVE_INFINITY;
468
+ const useDa = daIndex <= graphicsIndex;
469
+ const match = useDa ? daMatch : graphicsMatch;
470
+ if (!match || match.index === undefined) break;
471
+
472
+ passthrough += this.#sixelProbeBuffer.slice(0, match.index);
473
+ this.#sixelProbeBuffer = this.#sixelProbeBuffer.slice(match.index + match[0].length);
474
+
475
+ if (useDa && this.#sixelProbePendingDa) {
476
+ this.#sixelProbePendingDa = false;
477
+ const attributes = (match[1] ?? "")
478
+ .split(";")
479
+ .map(value => Number.parseInt(value, 10))
480
+ .filter(value => Number.isFinite(value));
481
+ const hasSixelAttribute = attributes.includes(4);
482
+ if (hasSixelAttribute) {
483
+ this.#sixelProbePendingGraphics = false;
484
+ probeOutcome = true;
485
+ } else if (!this.#sixelProbePendingGraphics) {
486
+ probeOutcome = false;
487
+ }
488
+ } else if (!useDa && this.#sixelProbePendingGraphics) {
489
+ this.#sixelProbePendingGraphics = false;
490
+ const status = Number.parseInt(match[1] ?? "", 10);
491
+ const supportsSixel = !Number.isNaN(status) && status !== 0;
492
+ if (supportsSixel) {
493
+ this.#sixelProbePendingDa = false;
494
+ probeOutcome = true;
495
+ } else if (!this.#sixelProbePendingDa) {
496
+ probeOutcome = false;
497
+ }
498
+ }
499
+ }
500
+
501
+ if (this.#sixelProbePendingDa || this.#sixelProbePendingGraphics) {
502
+ const partialStart = this.#getSixelProbePartialStart(this.#sixelProbeBuffer);
503
+ if (partialStart >= 0) {
504
+ passthrough += this.#sixelProbeBuffer.slice(0, partialStart);
505
+ this.#sixelProbeBuffer = this.#sixelProbeBuffer.slice(partialStart);
506
+ } else {
507
+ passthrough += this.#sixelProbeBuffer;
508
+ this.#sixelProbeBuffer = "";
509
+ }
510
+ } else {
511
+ passthrough += this.#sixelProbeBuffer;
512
+ this.#sixelProbeBuffer = "";
513
+ }
514
+
515
+ if (probeOutcome !== null) {
516
+ this.#finishSixelProbe(probeOutcome);
517
+ }
518
+
519
+ if (passthrough.length === 0) {
520
+ return { consume: true };
521
+ }
522
+
523
+ return { data: passthrough };
524
+ }
525
+
526
+ #getSixelProbePartialStart(buffer: string): number {
527
+ const lastEsc = buffer.lastIndexOf("\x1b");
528
+ if (lastEsc < 0) return -1;
529
+ const tail = buffer.slice(lastEsc);
530
+ if (/^\x1b\[\?[0-9;]*$/u.test(tail)) {
531
+ return lastEsc;
532
+ }
533
+ return -1;
534
+ }
535
+
536
+ #clearSixelProbeState(): void {
537
+ if (this.#sixelProbeTimeout) {
538
+ clearTimeout(this.#sixelProbeTimeout);
539
+ this.#sixelProbeTimeout = undefined;
540
+ }
541
+ if (this.#sixelProbeUnsubscribe) {
542
+ this.#sixelProbeUnsubscribe();
543
+ this.#sixelProbeUnsubscribe = undefined;
544
+ }
545
+ this.#sixelProbePendingDa = false;
546
+ this.#sixelProbePendingGraphics = false;
547
+ this.#sixelProbeBuffer = "";
548
+ }
549
+
550
+ #finishSixelProbe(supported: boolean): void {
551
+ this.#clearSixelProbeState();
552
+ if (!supported || TERMINAL.imageProtocol) return;
553
+
554
+ setTerminalImageProtocol(ImageProtocol.Sixel);
555
+ this.#queryCellSize();
556
+ this.invalidate();
557
+ this.requestRender(true);
558
+ }
559
+ #queryCellSize(): void {
560
+ // Only query if terminal supports images (cell size is only used for image rendering)
561
+ if (!TERMINAL.imageProtocol) {
562
+ return;
563
+ }
564
+ // Query terminal for cell size in pixels: CSI 16 t
565
+ // Response format: CSI 6 ; height ; width t
566
+ this.terminal.write("\x1b[16t");
567
+ }
568
+
569
+ stop(): void {
570
+ this.#clearSixelProbeState();
571
+ this.#stopped = true;
572
+ if (this.#renderTimer) {
573
+ clearTimeout(this.#renderTimer);
574
+ this.#renderTimer = undefined;
575
+ }
576
+ // Move cursor to the end of the content to prevent overwriting/artifacts on exit
577
+ if (this.#previousLines.length > 0) {
578
+ const targetRow = this.#previousLines.length; // Line after the last content
579
+ const lineDiff = targetRow - this.#hardwareCursorRow;
580
+ if (lineDiff > 0) {
581
+ this.terminal.write(`\x1b[${lineDiff}B`);
582
+ } else if (lineDiff < 0) {
583
+ this.terminal.write(`\x1b[${-lineDiff}A`);
584
+ }
585
+ this.terminal.write("\r\n");
586
+ }
587
+
588
+ this.terminal.showCursor();
589
+ this.terminal.stop();
590
+ }
591
+
592
+ requestRender(force = false): void {
593
+ if (force) {
594
+ this.#previousLines = [];
595
+ this.#previousWidth = -1; // -1 triggers widthChanged, forcing a full clear
596
+ this.#previousHeight = -1; // -1 triggers heightChanged, forcing a full clear
597
+ this.#cursorRow = 0;
598
+ this.#hardwareCursorRow = 0;
599
+ this.#viewportTopRow = 0;
600
+ this.#maxLinesRendered = 0;
601
+ if (this.#renderTimer) {
602
+ clearTimeout(this.#renderTimer);
603
+ this.#renderTimer = undefined;
604
+ }
605
+ this.#renderRequested = true;
606
+ process.nextTick(() => {
607
+ if (this.#stopped || !this.#renderRequested) {
608
+ return;
609
+ }
610
+ this.#renderRequested = false;
611
+ this.#lastRenderAt = performance.now();
612
+ this.#doRender();
613
+ });
614
+ return;
615
+ }
616
+ if (this.#renderRequested) return;
617
+ this.#renderRequested = true;
618
+ process.nextTick(() => this.#scheduleRender());
619
+ }
620
+
621
+ #scheduleRender(): void {
622
+ if (this.#stopped || this.#renderTimer || !this.#renderRequested) {
623
+ return;
624
+ }
625
+ const elapsed = performance.now() - this.#lastRenderAt;
626
+ const delay = Math.max(0, TUI.#MIN_RENDER_INTERVAL_MS - elapsed);
627
+ this.#renderTimer = setTimeout(() => {
628
+ this.#renderTimer = undefined;
629
+ if (this.#stopped || !this.#renderRequested) {
630
+ return;
631
+ }
632
+ this.#renderRequested = false;
633
+ this.#lastRenderAt = performance.now();
634
+ this.#doRender();
635
+ if (this.#renderRequested) {
636
+ this.#scheduleRender();
637
+ }
638
+ }, delay);
639
+ }
640
+
641
+ #handleInput(data: string): void {
642
+ if (this.#inputListeners.size > 0) {
643
+ let current = data;
644
+ for (const listener of this.#inputListeners) {
645
+ const result = listener(current);
646
+ if (result?.consume) {
647
+ return;
648
+ }
649
+ if (result?.data !== undefined) {
650
+ current = result.data;
651
+ }
652
+ }
653
+ if (current.length === 0) {
654
+ return;
655
+ }
656
+ data = current;
657
+ }
658
+
659
+ // Consume terminal cell size responses without blocking unrelated input.
660
+ if (this.#consumeCellSizeResponse(data)) {
661
+ return;
662
+ }
663
+
664
+ // Global debug key handler (Shift+Ctrl+D)
665
+ if (matchesKey(data, "shift+ctrl+d") && this.onDebug) {
666
+ this.onDebug();
667
+ return;
668
+ }
669
+
670
+ // If focused component is an overlay, verify it's still visible
671
+ // (visibility can change due to terminal resize or visible() callback)
672
+ const focusedOverlay = this.overlayStack.find(o => o.component === this.#focusedComponent);
673
+ if (focusedOverlay && !this.#isOverlayVisible(focusedOverlay)) {
674
+ // Focused overlay is no longer visible, redirect to topmost visible overlay
675
+ const topVisible = this.#getTopmostVisibleOverlay();
676
+ if (topVisible) {
677
+ this.setFocus(topVisible.component);
678
+ } else {
679
+ // No visible overlays, restore to preFocus
680
+ this.setFocus(focusedOverlay.preFocus);
681
+ }
682
+ }
683
+
684
+ // Pass input to focused component (including Ctrl+C)
685
+ // The focused component can decide how to handle Ctrl+C
686
+ if (this.#focusedComponent?.handleInput) {
687
+ // Filter out key release events unless component opts in
688
+ if (isKeyRelease(data) && !this.#focusedComponent.wantsKeyRelease) {
689
+ return;
690
+ }
691
+ this.#focusedComponent.handleInput(data);
692
+ this.requestRender();
693
+ }
694
+ }
695
+
696
+ #consumeCellSizeResponse(data: string): boolean {
697
+ // Response format: ESC [ 6 ; height ; width t
698
+ const match = data.match(/^\x1b\[6;(\d+);(\d+)t$/);
699
+ if (!match) {
700
+ return false;
701
+ }
702
+
703
+ const heightPx = parseInt(match[1], 10);
704
+ const widthPx = parseInt(match[2], 10);
705
+ if (heightPx <= 0 || widthPx <= 0) {
706
+ return true;
707
+ }
708
+
709
+ setCellDimensions({ widthPx, heightPx });
710
+ // Invalidate all components so images re-render with correct dimensions.
711
+ this.invalidate();
712
+ this.requestRender();
713
+ return true;
714
+ }
715
+
716
+ /**
717
+ * Resolve overlay layout from options.
718
+ * Returns { width, row, col, maxHeight } for rendering.
719
+ */
720
+ #resolveOverlayLayout(
721
+ options: OverlayOptions | undefined,
722
+ overlayHeight: number,
723
+ termWidth: number,
724
+ termHeight: number,
725
+ ): { width: number; row: number; col: number; maxHeight: number | undefined } {
726
+ const opt = options ?? {};
727
+
728
+ // Parse margin (clamp to non-negative)
729
+ const margin =
730
+ typeof opt.margin === "number"
731
+ ? { top: opt.margin, right: opt.margin, bottom: opt.margin, left: opt.margin }
732
+ : (opt.margin ?? {});
733
+ const marginTop = Math.max(0, margin.top ?? 0);
734
+ const marginRight = Math.max(0, margin.right ?? 0);
735
+ const marginBottom = Math.max(0, margin.bottom ?? 0);
736
+ const marginLeft = Math.max(0, margin.left ?? 0);
737
+
738
+ // Available space after margins
739
+ const availWidth = Math.max(1, termWidth - marginLeft - marginRight);
740
+ const availHeight = Math.max(1, termHeight - marginTop - marginBottom);
741
+
742
+ // === Resolve width ===
743
+ let width = parseSizeValue(opt.width, termWidth) ?? Math.min(80, availWidth);
744
+ // Apply minWidth
745
+ if (opt.minWidth !== undefined) {
746
+ width = Math.max(width, opt.minWidth);
747
+ }
748
+ // Clamp to available space
749
+ width = Math.max(1, Math.min(width, availWidth));
750
+
751
+ // === Resolve maxHeight ===
752
+ let maxHeight = parseSizeValue(opt.maxHeight, termHeight);
753
+ // Clamp to available space
754
+ if (maxHeight !== undefined) {
755
+ maxHeight = Math.max(1, Math.min(maxHeight, availHeight));
756
+ }
757
+
758
+ // Effective overlay height (may be clamped by maxHeight)
759
+ const effectiveHeight = maxHeight !== undefined ? Math.min(overlayHeight, maxHeight) : overlayHeight;
760
+
761
+ // === Resolve position ===
762
+ let row: number;
763
+ let col: number;
764
+
765
+ if (opt.row !== undefined) {
766
+ if (typeof opt.row === "string") {
767
+ // Percentage: 0% = top, 100% = bottom (overlay stays within bounds)
768
+ const match = opt.row.match(/^(\d+(?:\.\d+)?)%$/);
769
+ if (match) {
770
+ const maxRow = Math.max(0, availHeight - effectiveHeight);
771
+ const percent = parseFloat(match[1]) / 100;
772
+ row = marginTop + Math.floor(maxRow * percent);
773
+ } else {
774
+ // Invalid format, fall back to center
775
+ row = this.#resolveAnchorRow("center", effectiveHeight, availHeight, marginTop);
776
+ }
777
+ } else {
778
+ // Absolute row position
779
+ row = opt.row;
780
+ }
781
+ } else {
782
+ // Anchor-based (default: center)
783
+ const anchor = opt.anchor ?? "center";
784
+ row = this.#resolveAnchorRow(anchor, effectiveHeight, availHeight, marginTop);
785
+ }
786
+
787
+ if (opt.col !== undefined) {
788
+ if (typeof opt.col === "string") {
789
+ // Percentage: 0% = left, 100% = right (overlay stays within bounds)
790
+ const match = opt.col.match(/^(\d+(?:\.\d+)?)%$/);
791
+ if (match) {
792
+ const maxCol = Math.max(0, availWidth - width);
793
+ const percent = parseFloat(match[1]) / 100;
794
+ col = marginLeft + Math.floor(maxCol * percent);
795
+ } else {
796
+ // Invalid format, fall back to center
797
+ col = this.#resolveAnchorCol("center", width, availWidth, marginLeft);
798
+ }
799
+ } else {
800
+ // Absolute column position
801
+ col = opt.col;
802
+ }
803
+ } else {
804
+ // Anchor-based (default: center)
805
+ const anchor = opt.anchor ?? "center";
806
+ col = this.#resolveAnchorCol(anchor, width, availWidth, marginLeft);
807
+ }
808
+
809
+ // Apply offsets
810
+ if (opt.offsetY !== undefined) row += opt.offsetY;
811
+ if (opt.offsetX !== undefined) col += opt.offsetX;
812
+
813
+ // Clamp to terminal bounds (respecting margins)
814
+ row = Math.max(marginTop, Math.min(row, termHeight - marginBottom - effectiveHeight));
815
+ col = Math.max(marginLeft, Math.min(col, termWidth - marginRight - width));
816
+
817
+ return { width, row, col, maxHeight };
818
+ }
819
+
820
+ #resolveAnchorRow(anchor: OverlayAnchor, height: number, availHeight: number, marginTop: number): number {
821
+ switch (anchor) {
822
+ case "top-left":
823
+ case "top-center":
824
+ case "top-right":
825
+ return marginTop;
826
+ case "bottom-left":
827
+ case "bottom-center":
828
+ case "bottom-right":
829
+ return marginTop + availHeight - height;
830
+ case "left-center":
831
+ case "center":
832
+ case "right-center":
833
+ return marginTop + Math.floor((availHeight - height) / 2);
834
+ }
835
+ }
836
+
837
+ #resolveAnchorCol(anchor: OverlayAnchor, width: number, availWidth: number, marginLeft: number): number {
838
+ switch (anchor) {
839
+ case "top-left":
840
+ case "left-center":
841
+ case "bottom-left":
842
+ return marginLeft;
843
+ case "top-right":
844
+ case "right-center":
845
+ case "bottom-right":
846
+ return marginLeft + availWidth - width;
847
+ case "top-center":
848
+ case "center":
849
+ case "bottom-center":
850
+ return marginLeft + Math.floor((availWidth - width) / 2);
851
+ }
852
+ }
853
+
854
+ /** Composite all overlays into content lines (in stack order, later = on top). */
855
+ #compositeOverlays(lines: string[], termWidth: number, termHeight: number): string[] {
856
+ if (this.overlayStack.length === 0) return lines;
857
+ const result = [...lines];
858
+
859
+ // Pre-render all visible overlays and calculate positions
860
+ const rendered: { overlayLines: string[]; row: number; col: number; w: number }[] = [];
861
+ let minLinesNeeded = result.length;
862
+
863
+ for (const entry of this.overlayStack) {
864
+ // Skip invisible overlays (hidden or visible() returns false)
865
+ if (!this.#isOverlayVisible(entry)) continue;
866
+
867
+ const { component, options } = entry;
868
+
869
+ // Get layout with height=0 first to determine width and maxHeight
870
+ // (width and maxHeight don't depend on overlay height)
871
+ const { width, maxHeight } = this.#resolveOverlayLayout(options, 0, termWidth, termHeight);
872
+
873
+ // Render component at calculated width
874
+ let overlayLines = component.render(width);
875
+
876
+ // Apply maxHeight if specified
877
+ if (maxHeight !== undefined && overlayLines.length > maxHeight) {
878
+ overlayLines = overlayLines.slice(0, maxHeight);
879
+ }
880
+
881
+ // Get final row/col with actual overlay height
882
+ const { row, col } = this.#resolveOverlayLayout(options, overlayLines.length, termWidth, termHeight);
883
+
884
+ rendered.push({ overlayLines, row, col, w: width });
885
+ minLinesNeeded = Math.max(minLinesNeeded, row + overlayLines.length);
886
+ }
887
+
888
+ // Ensure result is tall enough for overlay placement.
889
+ // NOTE: Do not pad to maxLinesRendered.
890
+ // maxLinesRendered tracks the terminal "working area" (max lines ever rendered) and can be much larger
891
+ // than the current content. Padding to it can cause the renderer to output hundreds/thousands of blank
892
+ // lines, effectively scrolling the terminal when an overlay is shown.
893
+ const workingHeight = Math.max(result.length, minLinesNeeded);
894
+
895
+ // Extend result with empty lines if content is too short for overlay placement
896
+ while (result.length < workingHeight) {
897
+ result.push("");
898
+ }
899
+
900
+ const viewportStart = Math.max(0, workingHeight - termHeight);
901
+
902
+ // Track which lines were modified for final verification
903
+ const modifiedLines = new Set<number>();
904
+
905
+ // Composite each overlay
906
+ for (const { overlayLines, row, col, w } of rendered) {
907
+ for (let i = 0; i < overlayLines.length; i++) {
908
+ const idx = viewportStart + row + i;
909
+ if (idx >= 0 && idx < result.length) {
910
+ // Defensive: truncate overlay line to declared width before compositing
911
+ // (components should already respect width, but this ensures it)
912
+ const truncatedOverlayLine =
913
+ visibleWidth(overlayLines[i]) > w ? sliceByColumn(overlayLines[i], 0, w, true) : overlayLines[i];
914
+ result[idx] = this.#compositeLineAt(result[idx], truncatedOverlayLine, col, w, termWidth);
915
+ modifiedLines.add(idx);
916
+ }
917
+ }
918
+ }
919
+
920
+ // Final verification: ensure no composited line exceeds terminal width
921
+ // This is a belt-and-suspenders safeguard - compositeLineAt should already
922
+ // guarantee this, but we verify here to prevent crashes from any edge cases
923
+ // Only check lines that were actually modified (optimization)
924
+ for (const idx of modifiedLines) {
925
+ const lineWidth = visibleWidth(result[idx]);
926
+ if (lineWidth > termWidth) {
927
+ result[idx] = sliceByColumn(result[idx], 0, termWidth, true);
928
+ }
929
+ }
930
+
931
+ return result;
932
+ }
933
+
934
+ /** Splice overlay content into a base line at a specific column. Single-pass optimized. */
935
+ #compositeLineAt(
936
+ baseLine: string,
937
+ overlayLine: string,
938
+ startCol: number,
939
+ overlayWidth: number,
940
+ totalWidth: number,
941
+ ): string {
942
+ if (TERMINAL.isImageLine(baseLine)) return baseLine;
943
+
944
+ // Single pass through baseLine extracts both before and after segments
945
+ const afterStart = startCol + overlayWidth;
946
+ const base = extractSegments(baseLine, startCol, afterStart, totalWidth - afterStart, true);
947
+
948
+ // Extract overlay with width tracking (strict=true to exclude wide chars at boundary)
949
+ const overlay = sliceWithWidth(overlayLine, 0, overlayWidth, true);
950
+
951
+ // Pad segments to target widths
952
+ const beforePad = Math.max(0, startCol - base.beforeWidth);
953
+ const overlayPad = Math.max(0, overlayWidth - overlay.width);
954
+ const actualBeforeWidth = Math.max(startCol, base.beforeWidth);
955
+ const actualOverlayWidth = Math.max(overlayWidth, overlay.width);
956
+ const afterTarget = Math.max(0, totalWidth - actualBeforeWidth - actualOverlayWidth);
957
+ const afterPad = Math.max(0, afterTarget - base.afterWidth);
958
+
959
+ // Compose result
960
+ const r = SEGMENT_RESET;
961
+ const result =
962
+ base.before +
963
+ " ".repeat(beforePad) +
964
+ r +
965
+ overlay.text +
966
+ " ".repeat(overlayPad) +
967
+ r +
968
+ base.after +
969
+ " ".repeat(afterPad);
970
+
971
+ // CRITICAL: Always verify and truncate to terminal width.
972
+ // This is the final safeguard against width overflow which would crash the TUI.
973
+ // Width tracking can drift from actual visible width due to:
974
+ // - Complex ANSI/OSC sequences (hyperlinks, colors)
975
+ // - Wide characters at segment boundaries
976
+ // - Edge cases in segment extraction
977
+ const resultWidth = visibleWidth(result);
978
+ if (resultWidth <= totalWidth) {
979
+ return result;
980
+ }
981
+ // Truncate with strict=true to ensure we don't exceed totalWidth
982
+ return sliceByColumn(result, 0, totalWidth, true);
983
+ }
984
+
985
+ /**
986
+ * Find and extract cursor position from rendered lines.
987
+ * Searches for CURSOR_MARKER, calculates its position, and strips it from the output.
988
+ * Only scans the bottom terminal height lines (visible viewport).
989
+ * @param lines - Rendered lines to search
990
+ * @param height - Terminal height (visible viewport size)
991
+ * @returns Cursor position { row, col } or null if no marker found
992
+ */
993
+ #extractCursorPosition(lines: string[], height: number): { row: number; col: number } | null {
994
+ // Only scan the bottom `height` lines (visible viewport)
995
+ const viewportTop = Math.max(0, lines.length - height);
996
+ for (let row = lines.length - 1; row >= viewportTop; row--) {
997
+ const line = lines[row];
998
+ const markerIndex = line.indexOf(CURSOR_MARKER);
999
+ if (markerIndex !== -1) {
1000
+ // Calculate visual column (width of text before marker)
1001
+ const beforeMarker = line.slice(0, markerIndex);
1002
+ const col = visibleWidth(beforeMarker);
1003
+
1004
+ // Strip marker from the line
1005
+ lines[row] = line.slice(0, markerIndex) + line.slice(markerIndex + CURSOR_MARKER.length);
1006
+
1007
+ return { row, col };
1008
+ }
1009
+ }
1010
+ return null;
1011
+ }
1012
+
1013
+ /**
1014
+ * Append the per-line terminator ({@link LINE_TERMINATOR}) to every
1015
+ * non-image line and normalize for terminal rendering. Mutates the input
1016
+ * array in place so downstream diffing/storage sees exactly the bytes
1017
+ * written to the terminal — without this, the diff cache disagrees with
1018
+ * emitted output and OSC 8 hyperlink state can leak across lines.
1019
+ */
1020
+ #applyLineResets(lines: string[]): string[] {
1021
+ for (let i = 0; i < lines.length; i++) {
1022
+ const line = lines[i];
1023
+ if (TERMINAL.isImageLine(line)) continue;
1024
+ const normalized = normalizeTerminalOutput(line);
1025
+ // Only close OSC 8 hyperlinks when the line actually opened one;
1026
+ // emitting `\x1b]8;;\x07` on every line just feeds the terminal's OSC
1027
+ // parser for no reason (measurable cost in xterm.js parse loop).
1028
+ lines[i] = normalized + (normalized.includes("\x1b]8;") ? LINE_TERMINATOR : SEGMENT_RESET);
1029
+ }
1030
+ return lines;
1031
+ }
1032
+ #truncateLinesToWidth(lines: string[], width: number): string[] {
1033
+ for (let i = 0; i < lines.length; i++) {
1034
+ const line = lines[i];
1035
+ if (TERMINAL.isImageLine(line) || visibleWidth(line) <= width) continue;
1036
+ const truncated = truncateToWidth(line, width, Ellipsis.Omit);
1037
+ lines[i] = truncated + (truncated.includes("\x1b]8;") ? LINE_TERMINATOR : SEGMENT_RESET);
1038
+ }
1039
+ return lines;
1040
+ }
1041
+
1042
+ #doRender(): void {
1043
+ if (this.#stopped) return;
1044
+ const width = this.terminal.columns;
1045
+ const height = this.terminal.rows;
1046
+ let viewportTop = Math.max(0, this.#maxLinesRendered - height);
1047
+ let prevViewportTop = this.#viewportTopRow;
1048
+ let hardwareCursorRow = this.#hardwareCursorRow;
1049
+ const computeLineDiff = (targetRow: number): number => {
1050
+ const currentScreenRow = hardwareCursorRow - prevViewportTop;
1051
+ const targetScreenRow = targetRow - viewportTop;
1052
+ return targetScreenRow - currentScreenRow;
1053
+ };
1054
+
1055
+ // Render all components to get new lines
1056
+ let newLines = this.render(width);
1057
+
1058
+ // Composite overlays into the rendered lines (before differential compare)
1059
+ if (this.overlayStack.length > 0) {
1060
+ newLines = this.#compositeOverlays(newLines, width, height);
1061
+ }
1062
+
1063
+ // Extract cursor position (marker must be found before diff comparison)
1064
+ const cursorPos = this.#extractCursorPosition(newLines, height);
1065
+
1066
+ // Terminate every non-image line so #previousLines mirrors emitted bytes
1067
+ // (closes SGR + OSC 8 hyperlink state). Must run after cursor extraction
1068
+ // because the marker is embedded mid-line, and before any diff/full render
1069
+ // path so cache comparisons stay byte-accurate.
1070
+ newLines = this.#applyLineResets(newLines);
1071
+ newLines = this.#truncateLinesToWidth(newLines, width);
1072
+
1073
+ // Width changed - need full re-render (line wrapping changes)
1074
+ const widthChanged = this.#previousWidth !== 0 && this.#previousWidth !== width;
1075
+ const heightChanged = this.#previousHeight !== 0 && this.#previousHeight !== height;
1076
+
1077
+ // Helper to clear scrollback and viewport and render all new lines
1078
+ const fullRender = (clear: boolean): void => {
1079
+ this.#fullRedrawCount += 1;
1080
+ let buffer = "\x1b[?2026h"; // Begin synchronized output
1081
+ // Skip clearing scrollback (3J) in multiplexers — users actively navigate scrollback history
1082
+ if (clear) buffer += isMultiplexerSession() ? "\x1b[2J\x1b[H" : "\x1b[2J\x1b[H\x1b[3J";
1083
+ for (let i = 0; i < newLines.length; i++) {
1084
+ if (i > 0) buffer += "\r\n";
1085
+ // Lines were pre-terminated/normalized by #applyLineResets; image
1086
+ // lines were left untouched there.
1087
+ buffer += newLines[i];
1088
+ }
1089
+ this.#cursorRow = Math.max(0, newLines.length - 1);
1090
+ const { seq, toRow } = this.#cursorControlSequence(cursorPos, newLines.length, this.#cursorRow);
1091
+ this.#hardwareCursorRow = toRow;
1092
+ buffer += seq;
1093
+ buffer += "\x1b[?2026l"; // End synchronized output
1094
+ this.terminal.write(buffer);
1095
+ // Reset max lines when clearing, otherwise track growth
1096
+ if (clear) {
1097
+ this.#maxLinesRendered = newLines.length;
1098
+ } else {
1099
+ this.#maxLinesRendered = Math.max(this.#maxLinesRendered, newLines.length);
1100
+ }
1101
+ this.#viewportTopRow = Math.max(0, this.#maxLinesRendered - height);
1102
+ this.#previousLines = newLines;
1103
+ this.#previousWidth = width;
1104
+ this.#previousHeight = height;
1105
+ };
1106
+
1107
+ const multiplexerViewportRepaint = (reason: string): void => {
1108
+ this.#fullRedrawCount += 1;
1109
+ const nextViewportTop = Math.max(0, newLines.length - height);
1110
+ const currentScreenRow = Math.max(0, Math.min(height - 1, hardwareCursorRow - prevViewportTop));
1111
+ let buffer = "\x1b[?2026h";
1112
+ if (currentScreenRow > 0) {
1113
+ buffer += `\x1b[${currentScreenRow}A`;
1114
+ }
1115
+ buffer += "\r";
1116
+ for (let screenRow = 0; screenRow < height; screenRow++) {
1117
+ if (screenRow > 0) buffer += "\r\n";
1118
+ buffer += "\x1b[2K";
1119
+ const lineIndex = nextViewportTop + screenRow;
1120
+ if (lineIndex >= newLines.length) continue;
1121
+ const line = newLines[lineIndex];
1122
+ const isImage = TERMINAL.isImageLine(line);
1123
+ if (!isImage && visibleWidth(line) > width) {
1124
+ let truncatedLine = truncateToWidth(line, width, Ellipsis.Omit);
1125
+ truncatedLine += truncatedLine.includes("\x1b]8;") ? LINE_TERMINATOR : SEGMENT_RESET;
1126
+ buffer += truncatedLine;
1127
+ } else {
1128
+ buffer += line;
1129
+ }
1130
+ }
1131
+
1132
+ const finalPhysicalRow = nextViewportTop + Math.max(0, height - 1);
1133
+ let cursorSeq = "\x1b[?25l";
1134
+ let cursorToRow = finalPhysicalRow;
1135
+ if (cursorPos && cursorPos.row >= nextViewportTop && cursorPos.row < nextViewportTop + height) {
1136
+ const cursor = this.#cursorControlSequence(cursorPos, newLines.length, finalPhysicalRow);
1137
+ cursorSeq = cursor.seq;
1138
+ cursorToRow = cursor.toRow;
1139
+ }
1140
+ this.#hardwareCursorRow = cursorToRow;
1141
+ buffer += cursorSeq;
1142
+ buffer += "\x1b[?2026l";
1143
+ this.terminal.write(buffer);
1144
+
1145
+ if ($flag("PI_DEBUG_REDRAW")) {
1146
+ const logPath = getDebugLogPath();
1147
+ const msg = `[${new Date().toISOString()}] multiplexerViewportRepaint: ${reason} (prev=${this.#previousLines.length}, new=${newLines.length}, height=${height}, viewportTop=${nextViewportTop})\n`;
1148
+ fs.appendFileSync(logPath, msg);
1149
+ }
1150
+ // In multiplexers this deliberately prioritizes the live viewport over
1151
+ // historical scrollback repair. After offscreen changes, #previousLines
1152
+ // tracks the desired logical transcript, not every byte emitted into the
1153
+ // multiplexer scrollback.
1154
+ this.#cursorRow = Math.max(0, newLines.length - 1);
1155
+ this.#maxLinesRendered = newLines.length;
1156
+ this.#viewportTopRow = nextViewportTop;
1157
+ this.#previousLines = newLines;
1158
+ this.#previousWidth = width;
1159
+ this.#previousHeight = height;
1160
+ };
1161
+
1162
+ const debugRedraw = $flag("PI_DEBUG_REDRAW");
1163
+ const logRedraw = (reason: string): void => {
1164
+ if (!debugRedraw) return;
1165
+ const logPath = getDebugLogPath();
1166
+ const msg = `[${new Date().toISOString()}] fullRender: ${reason} (prev=${this.#previousLines.length}, new=${newLines.length}, height=${height})\n`;
1167
+ fs.appendFileSync(logPath, msg);
1168
+ };
1169
+
1170
+ // First render - just output everything without clearing (assumes clean screen)
1171
+ if (this.#previousLines.length === 0 && !widthChanged && !heightChanged) {
1172
+ logRedraw("first render");
1173
+ fullRender(false);
1174
+ return;
1175
+ }
1176
+
1177
+ // Width changes always need a full re-render because wrapping changes.
1178
+ if (widthChanged) {
1179
+ logRedraw(`terminal width changed (${this.#previousWidth} -> ${width})`);
1180
+ fullRender(true);
1181
+ return;
1182
+ }
1183
+
1184
+ // Height changes normally need a full re-render to keep the visible viewport aligned,
1185
+ // but Termux changes height when the software keyboard shows or hides.
1186
+ // In that environment, a full redraw causes the entire history to replay on every toggle.
1187
+ if (heightChanged) {
1188
+ if (isMultiplexerSession() && !useLegacyMultiplexerFullRender()) {
1189
+ multiplexerViewportRepaint(`terminal height changed (${this.#previousHeight} -> ${height})`);
1190
+ return;
1191
+ }
1192
+ if (!isTermuxSession() && !isMultiplexerSession()) {
1193
+ logRedraw(`terminal height changed (${this.#previousHeight} -> ${height})`);
1194
+ fullRender(true);
1195
+ return;
1196
+ }
1197
+ }
1198
+
1199
+ // Content shrunk below the previous render and no overlays - re-render to clear empty rows
1200
+ // (overlays need the padding, so only do this when no overlays are active)
1201
+ // Configurable via setClearOnShrink() or PI_CLEAR_ON_SHRINK=0 env var
1202
+ if (this.#clearOnShrink && newLines.length < this.#previousLines.length && this.overlayStack.length === 0) {
1203
+ logRedraw(`clearOnShrink (prev=${this.#previousLines.length}, new=${newLines.length})`);
1204
+ fullRender(true);
1205
+ return;
1206
+ }
1207
+
1208
+ // Find first and last changed lines
1209
+ let firstChanged = -1;
1210
+ let lastChanged = -1;
1211
+ const maxLines = Math.max(newLines.length, this.#previousLines.length);
1212
+ for (let i = 0; i < maxLines; i++) {
1213
+ const oldLine = i < this.#previousLines.length ? this.#previousLines[i] : "";
1214
+ const newLine = i < newLines.length ? newLines[i] : "";
1215
+
1216
+ if (oldLine !== newLine) {
1217
+ if (firstChanged === -1) {
1218
+ firstChanged = i;
1219
+ }
1220
+ lastChanged = i;
1221
+ }
1222
+ }
1223
+ const appendedLines = newLines.length > this.#previousLines.length;
1224
+ if (appendedLines) {
1225
+ if (firstChanged === -1) {
1226
+ firstChanged = this.#previousLines.length;
1227
+ }
1228
+ lastChanged = newLines.length - 1;
1229
+ }
1230
+ const appendStart = appendedLines && firstChanged === this.#previousLines.length && firstChanged > 0;
1231
+
1232
+ // No changes - but still need to update hardware cursor position if it moved
1233
+ if (firstChanged === -1) {
1234
+ this.#writeCursorPosition(cursorPos, newLines.length);
1235
+ this.#viewportTopRow = Math.max(0, this.#maxLinesRendered - height);
1236
+ return;
1237
+ }
1238
+
1239
+ // All changes are in deleted lines (nothing to render, just clear)
1240
+ if (firstChanged >= newLines.length) {
1241
+ if (this.#previousLines.length > newLines.length) {
1242
+ let buffer = "\x1b[?2026h";
1243
+ // Move to end of new content (clamp to 0 for empty content)
1244
+ const targetRow = Math.max(0, newLines.length - 1);
1245
+ const lineDiff = computeLineDiff(targetRow);
1246
+ if (lineDiff > 0) buffer += `\x1b[${lineDiff}B`;
1247
+ else if (lineDiff < 0) buffer += `\x1b[${-lineDiff}A`;
1248
+ buffer += "\r";
1249
+ // Clear extra lines without scrolling
1250
+ const extraLines = this.#previousLines.length - newLines.length;
1251
+ if (extraLines > height) {
1252
+ logRedraw(`extraLines > height (${extraLines} > ${height})`);
1253
+ if (isMultiplexerSession() && !useLegacyMultiplexerFullRender()) {
1254
+ multiplexerViewportRepaint(`extraLines > height (${extraLines} > ${height})`);
1255
+ } else {
1256
+ fullRender(true);
1257
+ }
1258
+ return;
1259
+ }
1260
+ const clearStartOffset = newLines.length > 0 && extraLines > 0 ? 1 : 0;
1261
+ if (clearStartOffset > 0) {
1262
+ buffer += `\x1b[${clearStartOffset}B`;
1263
+ }
1264
+ for (let i = 0; i < extraLines; i++) {
1265
+ buffer += "\r\x1b[2K";
1266
+ if (i < extraLines - 1) buffer += "\x1b[1B";
1267
+ }
1268
+ const moveUp = extraLines - 1 + clearStartOffset;
1269
+ if (moveUp > 0) {
1270
+ buffer += `\x1b[${moveUp}A`;
1271
+ }
1272
+ this.#cursorRow = targetRow;
1273
+ const { seq, toRow } = this.#cursorControlSequence(cursorPos, newLines.length, targetRow);
1274
+ this.#hardwareCursorRow = toRow;
1275
+ buffer += seq;
1276
+ buffer += "\x1b[?2026l";
1277
+ this.terminal.write(buffer);
1278
+ }
1279
+ this.#previousLines = newLines;
1280
+ this.#previousWidth = width;
1281
+ this.#previousHeight = height;
1282
+ this.#maxLinesRendered = newLines.length;
1283
+ this.#viewportTopRow = Math.max(0, newLines.length - height);
1284
+ return;
1285
+ }
1286
+
1287
+ // Differential rendering can only touch what was actually visible.
1288
+ // Any change above the previous viewport requires a full redraw so terminal
1289
+ // scrollback ends up consistent with the new transcript state.
1290
+ if (firstChanged < prevViewportTop) {
1291
+ logRedraw(`firstChanged < viewportTop (${firstChanged} < ${prevViewportTop})`);
1292
+ if (isMultiplexerSession() && !useLegacyMultiplexerFullRender()) {
1293
+ multiplexerViewportRepaint(`firstChanged < viewportTop (${firstChanged} < ${prevViewportTop})`);
1294
+ } else {
1295
+ fullRender(true);
1296
+ }
1297
+ return;
1298
+ }
1299
+
1300
+ // Render from first changed line to end
1301
+ // Build buffer with all updates wrapped in synchronized output
1302
+ let buffer = "\x1b[?2026h"; // Begin synchronized output
1303
+ const prevViewportBottom = prevViewportTop + height - 1;
1304
+ const moveTargetRow = appendStart ? firstChanged - 1 : firstChanged;
1305
+ if (moveTargetRow > prevViewportBottom) {
1306
+ const currentScreenRow = Math.max(0, Math.min(height - 1, hardwareCursorRow - prevViewportTop));
1307
+ const moveToBottom = height - 1 - currentScreenRow;
1308
+ if (moveToBottom > 0) {
1309
+ buffer += `\x1b[${moveToBottom}B`;
1310
+ }
1311
+ const scroll = moveTargetRow - prevViewportBottom;
1312
+ buffer += "\r\n".repeat(scroll);
1313
+ prevViewportTop += scroll;
1314
+ viewportTop += scroll;
1315
+ hardwareCursorRow = moveTargetRow;
1316
+ }
1317
+
1318
+ // Move cursor to first changed line (use hardwareCursorRow for actual position)
1319
+ const lineDiff = computeLineDiff(moveTargetRow);
1320
+ if (lineDiff > 0) {
1321
+ buffer += `\x1b[${lineDiff}B`; // Move down
1322
+ } else if (lineDiff < 0) {
1323
+ buffer += `\x1b[${-lineDiff}A`; // Move up
1324
+ }
1325
+
1326
+ buffer += appendStart ? "\r\n" : "\r"; // Move to column 0
1327
+
1328
+ // Only render changed lines (firstChanged to lastChanged), not all lines to end
1329
+ // This reduces flicker when only a single line changes (e.g., spinner animation)
1330
+ const renderEnd = Math.min(lastChanged, newLines.length - 1);
1331
+ for (let i = firstChanged; i <= renderEnd; i++) {
1332
+ if (i > firstChanged) buffer += "\r\n";
1333
+ buffer += "\x1b[2K"; // Clear current line
1334
+ const line = newLines[i];
1335
+ let truncatedLine = line;
1336
+ const isImage = TERMINAL.isImageLine(line);
1337
+ if (!isImage && visibleWidth(line) > width) {
1338
+ if (debugRedraw) {
1339
+ const debugData = [
1340
+ `[TUI Truncate] ${new Date().toISOString()}`,
1341
+ `Line ${i} truncated: ${visibleWidth(line)} > ${width}`,
1342
+ `Content preview: ${line.slice(0, 100)}...`,
1343
+ "",
1344
+ ].join("\n");
1345
+ try {
1346
+ fs.appendFileSync(getDebugLogPath(), debugData);
1347
+ } catch {
1348
+ // Ignore write errors - truncation should still work
1349
+ }
1350
+ }
1351
+ truncatedLine = truncateToWidth(line, width, Ellipsis.Omit);
1352
+ // Re-append the terminator: truncateToWidth removes trailing
1353
+ // content past the visible-width budget, which may also drop the
1354
+ // terminator appended by #applyLineResets. Match the conditional
1355
+ // OSC 8 close strategy used there.
1356
+ truncatedLine += truncatedLine.includes("\x1b]8;") ? LINE_TERMINATOR : SEGMENT_RESET;
1357
+ }
1358
+ // Non-image lines are pre-terminated/normalized by #applyLineResets;
1359
+ // truncated lines re-append LINE_TERMINATOR above.
1360
+ buffer += truncatedLine;
1361
+ }
1362
+
1363
+ // Track where cursor ended up after rendering
1364
+ let finalCursorRow = renderEnd;
1365
+
1366
+ // If we had more lines before, clear them and move cursor back
1367
+ if (this.#previousLines.length > newLines.length) {
1368
+ // Move to end of new content first if we stopped before it
1369
+ if (renderEnd < newLines.length - 1) {
1370
+ const moveDown = newLines.length - 1 - renderEnd;
1371
+ buffer += `\x1b[${moveDown}B`;
1372
+ finalCursorRow = newLines.length - 1;
1373
+ }
1374
+ const extraLines = this.#previousLines.length - newLines.length;
1375
+ for (let i = newLines.length; i < this.#previousLines.length; i++) {
1376
+ buffer += "\r\n\x1b[2K";
1377
+ }
1378
+ // Move cursor back to end of new content
1379
+ buffer += `\x1b[${extraLines}A`;
1380
+ }
1381
+
1382
+ const { seq, toRow } = this.#cursorControlSequence(cursorPos, newLines.length, finalCursorRow);
1383
+ this.#hardwareCursorRow = toRow;
1384
+ buffer += seq;
1385
+ buffer += "\x1b[?2026l"; // End synchronized output
1386
+
1387
+ if ($flag("PI_TUI_DEBUG")) {
1388
+ const debugDir = "/tmp/tui";
1389
+ fs.mkdirSync(debugDir, { recursive: true });
1390
+ const debugPath = path.join(debugDir, `render-${Date.now()}-${Math.random().toString(36).slice(2)}.log`);
1391
+ const debugData = [
1392
+ `firstChanged: ${firstChanged}`,
1393
+ `viewportTop: ${viewportTop}`,
1394
+ `cursorRow: ${this.#cursorRow}`,
1395
+ `height: ${height}`,
1396
+ `lineDiff: ${lineDiff}`,
1397
+ `hardwareCursorRow: ${hardwareCursorRow}`,
1398
+ `hardwareCursorRow (post): ${this.#hardwareCursorRow}`,
1399
+ `renderEnd: ${renderEnd}`,
1400
+ `finalCursorRow: ${finalCursorRow}`,
1401
+ `cursorPos: ${JSON.stringify(cursorPos)}`,
1402
+ `newLines.length: ${newLines.length}`,
1403
+ `previousLines.length: ${this.#previousLines.length}`,
1404
+ "",
1405
+ "=== newLines ===",
1406
+ JSON.stringify(newLines, null, 2),
1407
+ "",
1408
+ "=== previousLines ===",
1409
+ JSON.stringify(this.#previousLines, null, 2),
1410
+ "",
1411
+ "=== buffer ===",
1412
+ JSON.stringify(buffer),
1413
+ ].join("\n");
1414
+ fs.writeFileSync(debugPath, debugData);
1415
+ }
1416
+
1417
+ // Write entire buffer at once
1418
+ this.terminal.write(buffer);
1419
+
1420
+ // Track cursor position for next render.
1421
+ // cursorRow tracks end of content (for viewport calculation).
1422
+ // #hardwareCursorRow was already updated by #cursorControlSequence above.
1423
+ this.#cursorRow = Math.max(0, newLines.length - 1);
1424
+ // Track content height for viewport calculation
1425
+ this.#maxLinesRendered = newLines.length;
1426
+ this.#viewportTopRow = Math.max(0, newLines.length - height);
1427
+
1428
+ this.#previousLines = newLines;
1429
+ this.#previousWidth = width;
1430
+ this.#previousHeight = height;
1431
+ }
1432
+
1433
+ /**
1434
+ * Build cursor control sequences to position the hardware cursor for the IME
1435
+ * candidate window. Returns escape sequences and the resulting cursor row for
1436
+ * the caller to update `#hardwareCursorRow`. The sequences should be appended
1437
+ * into the caller's own synchronized output block to avoid a flicker between
1438
+ * content and cursor frames.
1439
+ */
1440
+ #cursorControlSequence(
1441
+ cursorPos: { row: number; col: number } | null,
1442
+ totalLines: number,
1443
+ fromRow: number,
1444
+ ): { seq: string; toRow: number } {
1445
+ // No IME target or no content — hide cursor regardless of preference
1446
+ if (!cursorPos || totalLines <= 0) return { seq: "\x1b[?25l", toRow: fromRow };
1447
+
1448
+ // Clamp cursor position to valid range
1449
+ const targetRow = Math.max(0, Math.min(cursorPos.row, totalLines - 1));
1450
+ const targetCol = Math.max(0, cursorPos.col);
1451
+
1452
+ // Move cursor from current position to target
1453
+ const rowDelta = targetRow - fromRow;
1454
+ let seq = "";
1455
+ if (rowDelta > 0) {
1456
+ seq += `\x1b[${rowDelta}B`; // Move down
1457
+ } else if (rowDelta < 0) {
1458
+ seq += `\x1b[${-rowDelta}A`; // Move up
1459
+ }
1460
+ // Move to absolute column (1-indexed)
1461
+ seq += `\x1b[${targetCol + 1}G`;
1462
+ seq += this.#showHardwareCursor ? "\x1b[?25h" : "\x1b[?25l";
1463
+
1464
+ return { seq, toRow: targetRow };
1465
+ }
1466
+
1467
+ /**
1468
+ * Write the hardware cursor position to the terminal as a standalone
1469
+ * synchronized output block. Use when there is no surrounding render buffer
1470
+ * to embed the sequences into.
1471
+ */
1472
+ #writeCursorPosition(cursorPos: { row: number; col: number } | null, totalLines: number): void {
1473
+ if (!cursorPos || totalLines <= 0) {
1474
+ this.terminal.hideCursor();
1475
+ return;
1476
+ }
1477
+ const { seq, toRow } = this.#cursorControlSequence(cursorPos, totalLines, this.#hardwareCursorRow);
1478
+ this.#hardwareCursorRow = toRow;
1479
+ this.terminal.write(`\x1b[?2026h${seq}\x1b[?2026l`);
1480
+ }
1481
+ }