@greppleai/grip-tui 0.0.0-stage → 0.0.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.
- package/README.md +818 -2
- package/dist/autocomplete.d.ts +48 -0
- package/dist/autocomplete.d.ts.map +1 -0
- package/dist/autocomplete.js +566 -0
- package/dist/autocomplete.js.map +1 -0
- package/dist/components/alt-screen-flash.d.ts +13 -0
- package/dist/components/alt-screen-flash.d.ts.map +1 -0
- package/dist/components/alt-screen-flash.js +37 -0
- package/dist/components/alt-screen-flash.js.map +1 -0
- package/dist/components/box.d.ts +22 -0
- package/dist/components/box.d.ts.map +1 -0
- package/dist/components/box.js +104 -0
- package/dist/components/box.js.map +1 -0
- package/dist/components/cancellable-loader.d.ts +22 -0
- package/dist/components/cancellable-loader.d.ts.map +1 -0
- package/dist/components/cancellable-loader.js +35 -0
- package/dist/components/cancellable-loader.js.map +1 -0
- package/dist/components/editor.d.ts +292 -0
- package/dist/components/editor.d.ts.map +1 -0
- package/dist/components/editor.js +1887 -0
- package/dist/components/editor.js.map +1 -0
- package/dist/components/h-stack.d.ts +7 -0
- package/dist/components/h-stack.d.ts.map +1 -0
- package/dist/components/h-stack.js +43 -0
- package/dist/components/h-stack.js.map +1 -0
- package/dist/components/image.d.ts +32 -0
- package/dist/components/image.d.ts.map +1 -0
- package/dist/components/image.js +76 -0
- package/dist/components/image.js.map +1 -0
- package/dist/components/input.d.ts +46 -0
- package/dist/components/input.d.ts.map +1 -0
- package/dist/components/input.js +352 -0
- package/dist/components/input.js.map +1 -0
- package/dist/components/loader.d.ts +32 -0
- package/dist/components/loader.d.ts.map +1 -0
- package/dist/components/loader.js +80 -0
- package/dist/components/loader.js.map +1 -0
- package/dist/components/markdown.d.ts +132 -0
- package/dist/components/markdown.d.ts.map +1 -0
- package/dist/components/markdown.js +717 -0
- package/dist/components/markdown.js.map +1 -0
- package/dist/components/scroll-view.d.ts +53 -0
- package/dist/components/scroll-view.d.ts.map +1 -0
- package/dist/components/scroll-view.js +173 -0
- package/dist/components/scroll-view.js.map +1 -0
- package/dist/components/select-list.d.ts +50 -0
- package/dist/components/select-list.d.ts.map +1 -0
- package/dist/components/select-list.js +159 -0
- package/dist/components/select-list.js.map +1 -0
- package/dist/components/settings-list.d.ts +52 -0
- package/dist/components/settings-list.d.ts.map +1 -0
- package/dist/components/settings-list.js +187 -0
- package/dist/components/settings-list.js.map +1 -0
- package/dist/components/spacer.d.ts +12 -0
- package/dist/components/spacer.d.ts.map +1 -0
- package/dist/components/spacer.js +23 -0
- package/dist/components/spacer.js.map +1 -0
- package/dist/components/stack.d.ts +32 -0
- package/dist/components/stack.d.ts.map +1 -0
- package/dist/components/stack.js +122 -0
- package/dist/components/stack.js.map +1 -0
- package/dist/components/text.d.ts +19 -0
- package/dist/components/text.d.ts.map +1 -0
- package/dist/components/text.js +89 -0
- package/dist/components/text.js.map +1 -0
- package/dist/components/truncated-text.d.ts +13 -0
- package/dist/components/truncated-text.d.ts.map +1 -0
- package/dist/components/truncated-text.js +51 -0
- package/dist/components/truncated-text.js.map +1 -0
- package/dist/components/v-stack.d.ts +8 -0
- package/dist/components/v-stack.d.ts.map +1 -0
- package/dist/components/v-stack.js +27 -0
- package/dist/components/v-stack.js.map +1 -0
- package/dist/editor-component.d.ts +39 -0
- package/dist/editor-component.d.ts.map +1 -0
- package/dist/editor-component.js +2 -0
- package/dist/editor-component.js.map +1 -0
- package/dist/fuzzy.d.ts +16 -0
- package/dist/fuzzy.d.ts.map +1 -0
- package/dist/fuzzy.js +101 -0
- package/dist/fuzzy.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/keybindings.d.ts +243 -0
- package/dist/keybindings.d.ts.map +1 -0
- package/dist/keybindings.js +209 -0
- package/dist/keybindings.js.map +1 -0
- package/dist/keys.d.ts +184 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/keys.js +1053 -0
- package/dist/keys.js.map +1 -0
- package/dist/kill-ring.d.ts +30 -0
- package/dist/kill-ring.d.ts.map +1 -0
- package/dist/kill-ring.js +37 -0
- package/dist/kill-ring.js.map +1 -0
- package/dist/latex.d.ts +10 -0
- package/dist/latex.d.ts.map +1 -0
- package/dist/latex.js +1406 -0
- package/dist/latex.js.map +1 -0
- package/dist/layout-node.d.ts +43 -0
- package/dist/layout-node.d.ts.map +1 -0
- package/dist/layout-node.js +6 -0
- package/dist/layout-node.js.map +1 -0
- package/dist/layout.d.ts +40 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +386 -0
- package/dist/layout.js.map +1 -0
- package/dist/native-modifiers.d.ts +3 -0
- package/dist/native-modifiers.d.ts.map +1 -0
- package/dist/native-modifiers.js +53 -0
- package/dist/native-modifiers.js.map +1 -0
- package/dist/stdin-buffer.d.ts +58 -0
- package/dist/stdin-buffer.d.ts.map +1 -0
- package/dist/stdin-buffer.js +318 -0
- package/dist/stdin-buffer.js.map +1 -0
- package/dist/terminal-control.d.ts +5 -0
- package/dist/terminal-control.d.ts.map +1 -0
- package/dist/terminal-control.js +26 -0
- package/dist/terminal-control.js.map +1 -0
- package/dist/terminal-image.d.ts +112 -0
- package/dist/terminal-image.d.ts.map +1 -0
- package/dist/terminal-image.js +508 -0
- package/dist/terminal-image.js.map +1 -0
- package/dist/terminal.d.ts +117 -0
- package/dist/terminal.d.ts.map +1 -0
- package/dist/terminal.js +490 -0
- package/dist/terminal.js.map +1 -0
- package/dist/tui-alt-screen.d.ts +114 -0
- package/dist/tui-alt-screen.d.ts.map +1 -0
- package/dist/tui-alt-screen.js +950 -0
- package/dist/tui-alt-screen.js.map +1 -0
- package/dist/tui-main-screen.d.ts +52 -0
- package/dist/tui-main-screen.d.ts.map +1 -0
- package/dist/tui-main-screen.js +511 -0
- package/dist/tui-main-screen.js.map +1 -0
- package/dist/tui.d.ts +295 -0
- package/dist/tui.d.ts.map +1 -0
- package/dist/tui.js +868 -0
- package/dist/tui.js.map +1 -0
- package/dist/undo-stack.d.ts +17 -0
- package/dist/undo-stack.d.ts.map +1 -0
- package/dist/undo-stack.js +25 -0
- package/dist/undo-stack.js.map +1 -0
- package/dist/utils.d.ts +96 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +1106 -0
- package/dist/utils.js.map +1 -0
- package/dist/word-navigation.d.ts +25 -0
- package/dist/word-navigation.d.ts.map +1 -0
- package/dist/word-navigation.js +94 -0
- package/dist/word-navigation.js.map +1 -0
- package/native/darwin/prebuilds/darwin-arm64/darwin-modifiers.node +0 -0
- package/native/darwin/prebuilds/darwin-x64/darwin-modifiers.node +0 -0
- package/native/win32/prebuilds/win32-arm64/win32-console-mode.node +0 -0
- package/native/win32/prebuilds/win32-x64/win32-console-mode.node +0 -0
- package/package.json +47 -5
package/dist/tui.d.ts
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal TUI implementation with differential rendering
|
|
3
|
+
*/
|
|
4
|
+
import type { Terminal } from "./terminal.ts";
|
|
5
|
+
import { visibleWidth } from "./utils.ts";
|
|
6
|
+
/**
|
|
7
|
+
* Component interface - all components must implement this
|
|
8
|
+
*/
|
|
9
|
+
export interface Component {
|
|
10
|
+
/**
|
|
11
|
+
* Render the component to lines for the given viewport width
|
|
12
|
+
* @param width - Current viewport width
|
|
13
|
+
* @returns Array of strings, each representing a line
|
|
14
|
+
*/
|
|
15
|
+
render(width: number): string[];
|
|
16
|
+
/**
|
|
17
|
+
* Optional handler for keyboard input when component has focus
|
|
18
|
+
*/
|
|
19
|
+
handleInput?(data: string): void;
|
|
20
|
+
/**
|
|
21
|
+
* If true, component receives key release events (Kitty protocol).
|
|
22
|
+
* Default is false - release events are filtered out.
|
|
23
|
+
*/
|
|
24
|
+
wantsKeyRelease?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Invalidate any cached rendering state.
|
|
27
|
+
* Called when theme changes or when component needs to re-render from scratch.
|
|
28
|
+
*/
|
|
29
|
+
invalidate(): void;
|
|
30
|
+
}
|
|
31
|
+
export type TuiInputListenerResult = {
|
|
32
|
+
consume?: boolean;
|
|
33
|
+
data?: string;
|
|
34
|
+
} | undefined;
|
|
35
|
+
export type TuiInputListener = (data: string) => TuiInputListenerResult;
|
|
36
|
+
/**
|
|
37
|
+
* Interface for components that can receive focus and display a hardware cursor.
|
|
38
|
+
* When focused, the component should emit CURSOR_MARKER at the cursor position
|
|
39
|
+
* in its render output. TUI will find this marker and position the hardware
|
|
40
|
+
* cursor there for proper IME candidate window positioning.
|
|
41
|
+
*/
|
|
42
|
+
export interface Focusable {
|
|
43
|
+
/** Set by TUI when focus changes. Component should emit CURSOR_MARKER when true. */
|
|
44
|
+
focused: boolean;
|
|
45
|
+
}
|
|
46
|
+
/** Type guard to check if a component implements Focusable */
|
|
47
|
+
export declare function isFocusable(component: Component | null): component is Component & Focusable;
|
|
48
|
+
/**
|
|
49
|
+
* Cursor position marker - APC (Application Program Command) sequence.
|
|
50
|
+
* This is a zero-width escape sequence that terminals ignore.
|
|
51
|
+
* Components emit this at the cursor position when focused.
|
|
52
|
+
* TUI finds and strips this marker, then positions the hardware cursor there.
|
|
53
|
+
*/
|
|
54
|
+
export declare const CURSOR_MARKER = "\u001B_pi:c\u0007";
|
|
55
|
+
export { visibleWidth };
|
|
56
|
+
/**
|
|
57
|
+
* Anchor position for overlays
|
|
58
|
+
*/
|
|
59
|
+
export type OverlayAnchor = "center" | "top-left" | "top-right" | "bottom-left" | "bottom-right" | "top-center" | "bottom-center" | "left-center" | "right-center";
|
|
60
|
+
/**
|
|
61
|
+
* Margin configuration for overlays
|
|
62
|
+
*/
|
|
63
|
+
export interface OverlayMargin {
|
|
64
|
+
top?: number;
|
|
65
|
+
right?: number;
|
|
66
|
+
bottom?: number;
|
|
67
|
+
left?: number;
|
|
68
|
+
}
|
|
69
|
+
/** Value that can be absolute (number) or percentage (string like "50%") */
|
|
70
|
+
export type SizeValue = number | `${number}%`;
|
|
71
|
+
/**
|
|
72
|
+
* Options for overlay positioning and sizing.
|
|
73
|
+
* Values can be absolute numbers or percentage strings (e.g., "50%").
|
|
74
|
+
*/
|
|
75
|
+
export interface OverlayOptions {
|
|
76
|
+
/** Width in columns, or percentage of terminal width (e.g., "50%") */
|
|
77
|
+
width?: SizeValue;
|
|
78
|
+
/** Minimum width in columns */
|
|
79
|
+
minWidth?: number;
|
|
80
|
+
/** Maximum height in rows, or percentage of terminal height (e.g., "50%") */
|
|
81
|
+
maxHeight?: SizeValue;
|
|
82
|
+
/** Anchor point for positioning (default: 'center') */
|
|
83
|
+
anchor?: OverlayAnchor;
|
|
84
|
+
/** Horizontal offset from anchor position (positive = right) */
|
|
85
|
+
offsetX?: number;
|
|
86
|
+
/** Vertical offset from anchor position (positive = down) */
|
|
87
|
+
offsetY?: number;
|
|
88
|
+
/** Row position: absolute number, or percentage (e.g., "25%" = 25% from top) */
|
|
89
|
+
row?: SizeValue;
|
|
90
|
+
/** Column position: absolute number, or percentage (e.g., "50%" = centered horizontally) */
|
|
91
|
+
col?: SizeValue;
|
|
92
|
+
/** Margin from terminal edges. Number applies to all sides. */
|
|
93
|
+
margin?: OverlayMargin | number;
|
|
94
|
+
/**
|
|
95
|
+
* Control overlay visibility based on terminal dimensions.
|
|
96
|
+
* If provided, overlay is only rendered when this returns true.
|
|
97
|
+
* Called each render cycle with current terminal dimensions.
|
|
98
|
+
*/
|
|
99
|
+
visible?: (termWidth: number, termHeight: number) => boolean;
|
|
100
|
+
/** If true, don't capture keyboard focus when shown */
|
|
101
|
+
nonCapturing?: boolean;
|
|
102
|
+
}
|
|
103
|
+
/** Options for {@link OverlayHandle.unfocus}. */
|
|
104
|
+
export interface OverlayUnfocusOptions {
|
|
105
|
+
/** Explicit target to focus after releasing this overlay. */
|
|
106
|
+
target: Component | null;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Handle returned by showOverlay for controlling the overlay
|
|
110
|
+
*/
|
|
111
|
+
export interface OverlayHandle {
|
|
112
|
+
/** Permanently remove the overlay (cannot be shown again) */
|
|
113
|
+
hide(): void;
|
|
114
|
+
/** Temporarily hide or show the overlay */
|
|
115
|
+
setHidden(hidden: boolean): void;
|
|
116
|
+
/** Check if overlay is temporarily hidden */
|
|
117
|
+
isHidden(): boolean;
|
|
118
|
+
/** Focus this overlay and bring it to the visual front */
|
|
119
|
+
focus(): void;
|
|
120
|
+
/** Release focus to the next visible capturing overlay or previous target, or to an explicit target when provided */
|
|
121
|
+
unfocus(options?: OverlayUnfocusOptions): void;
|
|
122
|
+
/** Check if this overlay currently has focus */
|
|
123
|
+
isFocused(): boolean;
|
|
124
|
+
}
|
|
125
|
+
export interface RenderedCursorPosition {
|
|
126
|
+
row: number;
|
|
127
|
+
col: number;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Container - a component that contains other components
|
|
131
|
+
*/
|
|
132
|
+
export declare class Container implements Component {
|
|
133
|
+
children: Component[];
|
|
134
|
+
addChild(component: Component): void;
|
|
135
|
+
removeChild(component: Component): void;
|
|
136
|
+
clear(): void;
|
|
137
|
+
invalidate(): void;
|
|
138
|
+
render(width: number): string[];
|
|
139
|
+
}
|
|
140
|
+
/** Composite overlay content into a terminal line at a fixed column. */
|
|
141
|
+
export declare function compositeTuiLine(baseLine: string, overlayLine: string, startCol: number, overlayWidth: number, totalWidth: number): string;
|
|
142
|
+
export type TuiMode = "regular" | "fullscreen";
|
|
143
|
+
export interface TuiStopOptions {
|
|
144
|
+
/** Restore the previous main buffer without replaying renderer content. */
|
|
145
|
+
preserveScreen?: boolean;
|
|
146
|
+
}
|
|
147
|
+
export interface TUI extends Component {
|
|
148
|
+
readonly mode: TuiMode;
|
|
149
|
+
children: Component[];
|
|
150
|
+
terminal: Terminal;
|
|
151
|
+
onDebug?: () => void;
|
|
152
|
+
readonly fullRedraws: number;
|
|
153
|
+
readonly hasOverlayEntries: boolean;
|
|
154
|
+
addChild(component: Component): void;
|
|
155
|
+
removeChild(component: Component): void;
|
|
156
|
+
clear(): void;
|
|
157
|
+
getShowHardwareCursor(): boolean;
|
|
158
|
+
setShowHardwareCursor(enabled: boolean): void;
|
|
159
|
+
getClearOnShrink(): boolean;
|
|
160
|
+
setClearOnShrink(enabled: boolean): void;
|
|
161
|
+
getFocusedComponent(): Component | null;
|
|
162
|
+
setFocus(component: Component | null): void;
|
|
163
|
+
showOverlay(component: Component, options?: OverlayOptions): OverlayHandle;
|
|
164
|
+
hideOverlay(): void;
|
|
165
|
+
hasOverlay(): boolean;
|
|
166
|
+
start(): void;
|
|
167
|
+
stop(options?: TuiStopOptions): void;
|
|
168
|
+
renderNow(force?: boolean): void;
|
|
169
|
+
requestRender(force?: boolean): void;
|
|
170
|
+
addInputListener(listener: TuiInputListener): () => void;
|
|
171
|
+
removeInputListener(listener: TuiInputListener): void;
|
|
172
|
+
}
|
|
173
|
+
export declare const VIEWPORT_TUI: unique symbol;
|
|
174
|
+
export interface ViewportTUI extends TUI {
|
|
175
|
+
readonly [VIEWPORT_TUI]: true;
|
|
176
|
+
setLayoutRoot(component: Component | undefined): void;
|
|
177
|
+
}
|
|
178
|
+
export declare function isViewportTUI(tui: TUI): tui is ViewportTUI;
|
|
179
|
+
export declare abstract class TuiBase extends Container implements TUI {
|
|
180
|
+
abstract readonly mode: TuiMode;
|
|
181
|
+
terminal: Terminal;
|
|
182
|
+
private focusedComponent;
|
|
183
|
+
private inputListeners;
|
|
184
|
+
/** Global callback for debug key (Shift+Ctrl+D). Called before input is forwarded to focused component. */
|
|
185
|
+
onDebug?: () => void;
|
|
186
|
+
private renderRequested;
|
|
187
|
+
private immediateRenderScheduled;
|
|
188
|
+
private renderTimer;
|
|
189
|
+
private lastRenderAt;
|
|
190
|
+
private static readonly MIN_RENDER_INTERVAL_MS;
|
|
191
|
+
private showHardwareCursor;
|
|
192
|
+
private clearOnShrink;
|
|
193
|
+
protected fullRedrawCount: number;
|
|
194
|
+
protected stopped: boolean;
|
|
195
|
+
protected readonly logDirectory: string;
|
|
196
|
+
private focusOrderCounter;
|
|
197
|
+
private overlayStack;
|
|
198
|
+
get hasOverlayEntries(): boolean;
|
|
199
|
+
private overlayFocusRestore;
|
|
200
|
+
constructor(terminal: Terminal, showHardwareCursor?: boolean, logDirectory?: string);
|
|
201
|
+
protected abstract doRender(): void;
|
|
202
|
+
protected resetRenderState(): void;
|
|
203
|
+
protected beforeTerminalStart(): void;
|
|
204
|
+
protected afterTerminalStart(): void;
|
|
205
|
+
protected onTerminalResize(): void;
|
|
206
|
+
protected onOverlayStackChanged(): void;
|
|
207
|
+
protected beforeTerminalStop(_options?: TuiStopOptions): void;
|
|
208
|
+
protected afterTerminalStop(_options?: TuiStopOptions): void;
|
|
209
|
+
get fullRedraws(): number;
|
|
210
|
+
getShowHardwareCursor(): boolean;
|
|
211
|
+
setShowHardwareCursor(enabled: boolean): void;
|
|
212
|
+
getClearOnShrink(): boolean;
|
|
213
|
+
/**
|
|
214
|
+
* Set whether to trigger full re-render when content shrinks.
|
|
215
|
+
* When true (default), empty rows are cleared when content shrinks.
|
|
216
|
+
* When false, empty rows remain (reduces redraws on slower terminals).
|
|
217
|
+
*/
|
|
218
|
+
setClearOnShrink(enabled: boolean): void;
|
|
219
|
+
getFocusedComponent(): Component | null;
|
|
220
|
+
setFocus(component: Component | null): void;
|
|
221
|
+
private resolveNonOverlayFocus;
|
|
222
|
+
private resolveNullFocus;
|
|
223
|
+
private resolveFocusTarget;
|
|
224
|
+
private applyFocus;
|
|
225
|
+
private setFocusInternal;
|
|
226
|
+
private clearOverlayFocusRestore;
|
|
227
|
+
private clearOverlayFocusRestoreFor;
|
|
228
|
+
private resolveBlockedOverlayFocusResume;
|
|
229
|
+
private getVisibleOverlayFocusRestore;
|
|
230
|
+
private isOverlayFocusAncestor;
|
|
231
|
+
private retargetOverlayPreFocus;
|
|
232
|
+
protected getMountedRoots(): readonly Component[];
|
|
233
|
+
private isComponentMounted;
|
|
234
|
+
private containsComponent;
|
|
235
|
+
private applyBlockedOverlayUnfocus;
|
|
236
|
+
private unfocusOverlay;
|
|
237
|
+
/**
|
|
238
|
+
* Show an overlay component with configurable positioning and sizing.
|
|
239
|
+
* Returns a handle to control the overlay's visibility.
|
|
240
|
+
*/
|
|
241
|
+
showOverlay(component: Component, options?: OverlayOptions): OverlayHandle;
|
|
242
|
+
/** Hide the topmost overlay and restore previous focus. */
|
|
243
|
+
hideOverlay(): void;
|
|
244
|
+
/** Check if there are any visible overlays */
|
|
245
|
+
hasOverlay(): boolean;
|
|
246
|
+
protected hasCapturingOverlay(): boolean;
|
|
247
|
+
/** Check if an overlay entry is currently visible */
|
|
248
|
+
private isOverlayVisible;
|
|
249
|
+
/** Find the visual-frontmost visible capturing overlay, if any */
|
|
250
|
+
private getTopmostVisibleOverlay;
|
|
251
|
+
private refreshOverlayVisibility;
|
|
252
|
+
private retargetFocusFromHiddenOverlay;
|
|
253
|
+
private focusNewlyVisibleOverlay;
|
|
254
|
+
private restoreVisibleOverlayFocus;
|
|
255
|
+
private reconcileOverlayFocus;
|
|
256
|
+
invalidate(): void;
|
|
257
|
+
start(): void;
|
|
258
|
+
addInputListener(listener: TuiInputListener): () => void;
|
|
259
|
+
removeInputListener(listener: TuiInputListener): void;
|
|
260
|
+
private queryCellSize;
|
|
261
|
+
stop(options?: TuiStopOptions): void;
|
|
262
|
+
renderNow(force?: boolean): void;
|
|
263
|
+
requestRender(force?: boolean): void;
|
|
264
|
+
private requestImmediateRender;
|
|
265
|
+
private cancelRenderTimer;
|
|
266
|
+
private scheduleRender;
|
|
267
|
+
private applyInputListeners;
|
|
268
|
+
private handleTerminalInput;
|
|
269
|
+
private consumeCellSizeResponse;
|
|
270
|
+
private resolveOverlayBounds;
|
|
271
|
+
private resolveOverlayWidth;
|
|
272
|
+
private resolveOverlayMaxHeight;
|
|
273
|
+
private resolveOverlayRow;
|
|
274
|
+
private resolveOverlayColumn;
|
|
275
|
+
/** Resolve overlay layout from options. */
|
|
276
|
+
private resolveOverlayLayout;
|
|
277
|
+
private resolveAnchorRow;
|
|
278
|
+
private resolveAnchorCol;
|
|
279
|
+
private prepareOverlayRenderPlan;
|
|
280
|
+
private compositeRenderedOverlay;
|
|
281
|
+
/** Composite all overlays into content lines (sorted by focusOrder, higher = on top). */
|
|
282
|
+
protected compositeOverlays(lines: string[], termWidth: number, termHeight: number): string[];
|
|
283
|
+
protected applyLineResets(lines: string[]): string[];
|
|
284
|
+
private compositeLineAt;
|
|
285
|
+
/**
|
|
286
|
+
* Find and extract cursor position from rendered lines.
|
|
287
|
+
* Searches for CURSOR_MARKER, calculates its position, and strips it from the output.
|
|
288
|
+
* Only scans the bottom terminal height lines (visible viewport).
|
|
289
|
+
* @param lines - Rendered lines to search
|
|
290
|
+
* @param height - Terminal height (visible viewport size)
|
|
291
|
+
* @returns Cursor position { row, col } or null if no marker found
|
|
292
|
+
*/
|
|
293
|
+
protected extractCursorPosition(lines: string[], height: number): RenderedCursorPosition | null;
|
|
294
|
+
}
|
|
295
|
+
//# sourceMappingURL=tui.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tui.d.ts","sourceRoot":"","sources":["../src/tui.ts"],"names":[],"mappings":"AAAA;;GAEG;AAMH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C,OAAO,EAA2E,YAAY,EAAE,MAAM,YAAY,CAAC;AAEnH;;GAEG;AACH,MAAM,WAAW,SAAS;IACzB;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAEhC;;OAEG;IACH,WAAW,CAAC,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAEjC;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAE1B;;;OAGG;IACH,UAAU,IAAI,IAAI,CAAC;CACnB;AAED,MAAM,MAAM,sBAAsB,GAAG;IAAE,OAAO,CAAC,EAAE,OAAO,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA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AAO,CAAC,oBAAoB;IAW5B,2CAA2C;IAC3C,OAAO,CAAC,oBAAoB;IAkB5B,OAAO,CAAC,gBAAgB;IAiBxB,OAAO,CAAC,gBAAgB;IAiBxB,OAAO,CAAC,wBAAwB;IAkBhC,OAAO,CAAC,wBAAwB;IAwBhC,yFAAyF;IACzF,SAAS,CAAC,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CAW5F;IAED,SAAS,CAAC,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CASnD;IAED,OAAO,CAAC,eAAe;IAUvB;;;;;;;OAOG;IACH,SAAS,CAAC,qBAAqB,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,sBAAsB,GAAG,IAAI,CAkB9F;CACD","sourcesContent":["/**\n * Minimal TUI implementation with differential rendering\n */\n\nimport * as os from \"node:os\";\nimport * as path from \"node:path\";\nimport { performance } from \"node:perf_hooks\";\nimport { isKeyRelease, matchesKey } from \"./keys.ts\";\nimport type { Terminal } from \"./terminal.ts\";\nimport { getCapabilities, isImageLine, setCellDimensions } from \"./terminal-image.ts\";\nimport { extractSegments, normalizeTerminalOutput, sliceByColumn, sliceWithWidth, visibleWidth } from \"./utils.ts\";\n\n/**\n * Component interface - all components must implement this\n */\nexport interface Component {\n\t/**\n\t * Render the component to lines for the given viewport width\n\t * @param width - Current viewport width\n\t * @returns Array of strings, each representing a line\n\t */\n\trender(width: number): string[];\n\n\t/**\n\t * Optional handler for keyboard input when component has focus\n\t */\n\thandleInput?(data: string): void;\n\n\t/**\n\t * If true, component receives key release events (Kitty protocol).\n\t * Default is false - release events are filtered out.\n\t */\n\twantsKeyRelease?: boolean;\n\n\t/**\n\t * Invalidate any cached rendering state.\n\t * Called when theme changes or when component needs to re-render from scratch.\n\t */\n\tinvalidate(): void;\n}\n\nexport type TuiInputListenerResult = { consume?: boolean; data?: string } | undefined;\nexport type TuiInputListener = (data: string) => TuiInputListenerResult;\n\n/**\n * Interface for components that can receive focus and display a hardware cursor.\n * When focused, the component should emit CURSOR_MARKER at the cursor position\n * in its render output. TUI will find this marker and position the hardware\n * cursor there for proper IME candidate window positioning.\n */\nexport interface Focusable {\n\t/** Set by TUI when focus changes. Component should emit CURSOR_MARKER when true. */\n\tfocused: boolean;\n}\n\n/** Type guard to check if a component implements Focusable */\nexport function isFocusable(component: Component | null): component is Component & Focusable {\n\treturn component !== null && \"focused\" in component;\n}\n\n/**\n * Cursor position marker - APC (Application Program Command) sequence.\n * This is a zero-width escape sequence that terminals ignore.\n * Components emit this at the cursor position when focused.\n * TUI finds and strips this marker, then positions the hardware cursor there.\n */\nexport const CURSOR_MARKER = \"\\x1b_pi:c\\x07\";\n\nexport { visibleWidth };\n\n/**\n * Anchor position for overlays\n */\nexport type OverlayAnchor =\n\t| \"center\"\n\t| \"top-left\"\n\t| \"top-right\"\n\t| \"bottom-left\"\n\t| \"bottom-right\"\n\t| \"top-center\"\n\t| \"bottom-center\"\n\t| \"left-center\"\n\t| \"right-center\";\n\n/**\n * Margin configuration for overlays\n */\nexport interface OverlayMargin {\n\ttop?: number;\n\tright?: number;\n\tbottom?: number;\n\tleft?: number;\n}\n\n/** Value that can be absolute (number) or percentage (string like \"50%\") */\nexport type SizeValue = number | `${number}%`;\n\n/** Parse a SizeValue into absolute value given a reference size */\nfunction parseSizeValue(value: SizeValue | undefined, referenceSize: number): number | undefined {\n\tif (value === undefined) return undefined;\n\tif (typeof value === \"number\") return value;\n\t// Parse percentage string like \"50%\"\n\tconst match = value.match(/^(\\d+(?:\\.\\d+)?)%$/);\n\tif (match) {\n\t\treturn Math.floor((referenceSize * parseFloat(match[1])) / 100);\n\t}\n\treturn undefined;\n}\n\n/**\n * Options for overlay positioning and sizing.\n * Values can be absolute numbers or percentage strings (e.g., \"50%\").\n */\nexport interface OverlayOptions {\n\t// === Sizing ===\n\t/** Width in columns, or percentage of terminal width (e.g., \"50%\") */\n\twidth?: SizeValue;\n\t/** Minimum width in columns */\n\tminWidth?: number;\n\t/** Maximum height in rows, or percentage of terminal height (e.g., \"50%\") */\n\tmaxHeight?: SizeValue;\n\n\t// === Positioning - anchor-based ===\n\t/** Anchor point for positioning (default: 'center') */\n\tanchor?: OverlayAnchor;\n\t/** Horizontal offset from anchor position (positive = right) */\n\toffsetX?: number;\n\t/** Vertical offset from anchor position (positive = down) */\n\toffsetY?: number;\n\n\t// === Positioning - percentage or absolute ===\n\t/** Row position: absolute number, or percentage (e.g., \"25%\" = 25% from top) */\n\trow?: SizeValue;\n\t/** Column position: absolute number, or percentage (e.g., \"50%\" = centered horizontally) */\n\tcol?: SizeValue;\n\n\t// === Margin from terminal edges ===\n\t/** Margin from terminal edges. Number applies to all sides. */\n\tmargin?: OverlayMargin | number;\n\n\t// === Visibility ===\n\t/**\n\t * Control overlay visibility based on terminal dimensions.\n\t * If provided, overlay is only rendered when this returns true.\n\t * Called each render cycle with current terminal dimensions.\n\t */\n\tvisible?: (termWidth: number, termHeight: number) => boolean;\n\t/** If true, don't capture keyboard focus when shown */\n\tnonCapturing?: boolean;\n}\n\n/** Options for {@link OverlayHandle.unfocus}. */\nexport interface OverlayUnfocusOptions {\n\t/** Explicit target to focus after releasing this overlay. */\n\ttarget: Component | null;\n}\n\n/**\n * Handle returned by showOverlay for controlling the overlay\n */\nexport interface OverlayHandle {\n\t/** Permanently remove the overlay (cannot be shown again) */\n\thide(): void;\n\t/** Temporarily hide or show the overlay */\n\tsetHidden(hidden: boolean): void;\n\t/** Check if overlay is temporarily hidden */\n\tisHidden(): boolean;\n\t/** Focus this overlay and bring it to the visual front */\n\tfocus(): void;\n\t/** Release focus to the next visible capturing overlay or previous target, or to an explicit target when provided */\n\tunfocus(options?: OverlayUnfocusOptions): void;\n\t/** Check if this overlay currently has focus */\n\tisFocused(): boolean;\n}\n\ntype OverlayStackEntry = {\n\tcomponent: Component;\n\toptions?: OverlayOptions;\n\tpreFocus: Component | null;\n\thidden: boolean;\n\tfocusOrder: number;\n\tvisible: boolean;\n};\n\ntype OverlayBlockedFocusResume = { status: \"restore-overlay\" } | { status: \"focus-target\"; target: Component | null };\ntype EligibleOverlayFocusRestoreState = { status: \"eligible\"; overlay: OverlayStackEntry };\ntype BlockedOverlayFocusRestoreState = {\n\tstatus: \"blocked\";\n\toverlay: OverlayStackEntry;\n\tblockedBy: Component;\n\tresume: OverlayBlockedFocusResume;\n};\ntype ActiveOverlayFocusRestoreState = EligibleOverlayFocusRestoreState | BlockedOverlayFocusRestoreState;\ntype OverlayFocusRestoreState = { status: \"inactive\" } | ActiveOverlayFocusRestoreState;\ntype OverlayFocusRestorePolicy = \"clear\" | \"preserve\";\n\ninterface FocusTransitionRequest {\n\tcomponent: Component | null;\n\toverlayFocusRestore: OverlayFocusRestorePolicy;\n}\n\ninterface ResolvedOverlayLayout {\n\twidth: number;\n\trow: number;\n\tcol: number;\n\tmaxHeight: number | undefined;\n}\n\ninterface RenderedOverlay {\n\tlines: string[];\n\trow: number;\n\tcol: number;\n\twidth: number;\n}\n\ninterface OverlayRenderPlan {\n\toverlays: RenderedOverlay[];\n\tminimumLineCount: number;\n}\n\ninterface OverlayLayoutBounds {\n\tmarginTop: number;\n\tmarginRight: number;\n\tmarginBottom: number;\n\tmarginLeft: number;\n\tavailableWidth: number;\n\tavailableHeight: number;\n}\n\nexport interface RenderedCursorPosition {\n\trow: number;\n\tcol: number;\n}\n/**\n * Container - a component that contains other components\n */\nexport class Container implements Component {\n\tchildren: Component[] = [];\n\n\taddChild(component: Component): void {\n\t\tthis.children.push(component);\n\t}\n\n\tremoveChild(component: Component): void {\n\t\tconst index = this.children.indexOf(component);\n\t\tif (index !== -1) {\n\t\t\tthis.children.splice(index, 1);\n\t\t}\n\t}\n\n\tclear(): void {\n\t\tthis.children = [];\n\t}\n\n\tinvalidate(): void {\n\t\tfor (const child of this.children) {\n\t\t\tchild.invalidate?.();\n\t\t}\n\t}\n\n\trender(width: number): string[] {\n\t\tconst lines: string[] = [];\n\t\tfor (const child of this.children) {\n\t\t\tconst childLines = child.render(width);\n\t\t\tfor (const line of childLines) {\n\t\t\t\tlines.push(line);\n\t\t\t}\n\t\t}\n\t\treturn lines;\n\t}\n}\n\n/**\n * TUI - Main class for managing terminal UI with differential rendering\n */\nconst SEGMENT_RESET = \"\\x1b[0m\\x1b]8;;\\x07\";\n\n/** Composite overlay content into a terminal line at a fixed column. */\nexport function compositeTuiLine(\n\tbaseLine: string,\n\toverlayLine: string,\n\tstartCol: number,\n\toverlayWidth: number,\n\ttotalWidth: number,\n): string {\n\tif (isImageLine(baseLine)) return baseLine;\n\n\tconst afterStart = startCol + overlayWidth;\n\tconst base = extractSegments(baseLine, startCol, afterStart, totalWidth - afterStart, true);\n\tconst overlay = sliceWithWidth(overlayLine, 0, overlayWidth, true);\n\tconst beforePad = Math.max(0, startCol - base.beforeWidth);\n\tconst overlayPad = Math.max(0, overlayWidth - overlay.width);\n\tconst actualBeforeWidth = Math.max(startCol, base.beforeWidth);\n\tconst actualOverlayWidth = Math.max(overlayWidth, overlay.width);\n\tconst afterTarget = Math.max(0, totalWidth - actualBeforeWidth - actualOverlayWidth);\n\tconst afterPad = Math.max(0, afterTarget - base.afterWidth);\n\tconst result =\n\t\tbase.before +\n\t\t\" \".repeat(beforePad) +\n\t\tSEGMENT_RESET +\n\t\toverlay.text +\n\t\t\" \".repeat(overlayPad) +\n\t\tSEGMENT_RESET +\n\t\tbase.after +\n\t\t\" \".repeat(afterPad);\n\n\treturn visibleWidth(result) <= totalWidth ? result : sliceByColumn(result, 0, totalWidth, true);\n}\n\nexport type TuiMode = \"regular\" | \"fullscreen\";\n\nexport interface TuiStopOptions {\n\t/** Restore the previous main buffer without replaying renderer content. */\n\tpreserveScreen?: boolean;\n}\n\nexport interface TUI extends Component {\n\treadonly mode: TuiMode;\n\tchildren: Component[];\n\tterminal: Terminal;\n\tonDebug?: () => void;\n\treadonly fullRedraws: number;\n\treadonly hasOverlayEntries: boolean;\n\taddChild(component: Component): void;\n\tremoveChild(component: Component): void;\n\tclear(): void;\n\tgetShowHardwareCursor(): boolean;\n\tsetShowHardwareCursor(enabled: boolean): void;\n\tgetClearOnShrink(): boolean;\n\tsetClearOnShrink(enabled: boolean): void;\n\tgetFocusedComponent(): Component | null;\n\tsetFocus(component: Component | null): void;\n\tshowOverlay(component: Component, options?: OverlayOptions): OverlayHandle;\n\thideOverlay(): void;\n\thasOverlay(): boolean;\n\tstart(): void;\n\tstop(options?: TuiStopOptions): void;\n\trenderNow(force?: boolean): void;\n\trequestRender(force?: boolean): void;\n\taddInputListener(listener: TuiInputListener): () => void;\n\tremoveInputListener(listener: TuiInputListener): void;\n}\n\nexport const VIEWPORT_TUI = Symbol.for(\"@greppleai/grip-tui/viewport\");\n\nexport interface ViewportTUI extends TUI {\n\treadonly [VIEWPORT_TUI]: true;\n\tsetLayoutRoot(component: Component | undefined): void;\n}\n\nexport function isViewportTUI(tui: TUI): tui is ViewportTUI {\n\treturn (tui as Partial<ViewportTUI>)[VIEWPORT_TUI] === true;\n}\n\nexport abstract class TuiBase extends Container implements TUI {\n\tabstract readonly mode: TuiMode;\n\tpublic terminal: Terminal;\n\tprivate focusedComponent: Component | null = null;\n\tprivate inputListeners = new Set<TuiInputListener>();\n\n\t/** Global callback for debug key (Shift+Ctrl+D). Called before input is forwarded to focused component. */\n\tpublic onDebug?: () => void;\n\tprivate renderRequested = false;\n\tprivate immediateRenderScheduled = false;\n\tprivate renderTimer: NodeJS.Timeout | undefined;\n\tprivate lastRenderAt = 0;\n\tprivate static readonly MIN_RENDER_INTERVAL_MS = 16;\n\tprivate showHardwareCursor = process.env.GRIP_HARDWARE_CURSOR === \"1\";\n\tprivate clearOnShrink = process.env.GRIP_CLEAR_ON_SHRINK === \"1\";\n\tprotected fullRedrawCount = 0;\n\tprotected stopped = false;\n\tprotected readonly logDirectory: string;\n\n\t// Overlay stack for modal components rendered on top of base content\n\tprivate focusOrderCounter = 0;\n\tprivate overlayStack: OverlayStackEntry[] = [];\n\n\tget hasOverlayEntries(): boolean {\n\t\treturn this.overlayStack.length > 0;\n\t}\n\tprivate overlayFocusRestore: OverlayFocusRestoreState = { status: \"inactive\" };\n\n\tconstructor(terminal: Terminal, showHardwareCursor?: boolean, logDirectory?: string) {\n\t\tsuper();\n\t\tthis.terminal = terminal;\n\t\tthis.logDirectory =\n\t\t\tlogDirectory ?? process.env.GRIP_CODING_AGENT_DIR ?? path.join(os.homedir(), \".grip\", \"agent\");\n\t\tif (showHardwareCursor !== undefined) {\n\t\t\tthis.showHardwareCursor = showHardwareCursor;\n\t\t}\n\t}\n\n\tprotected abstract doRender(): void;\n\n\tprotected resetRenderState(): void {}\n\n\tprotected beforeTerminalStart(): void {}\n\n\tprotected afterTerminalStart(): void {}\n\n\tprotected onTerminalResize(): void {}\n\n\tprotected onOverlayStackChanged(): void {}\n\n\tprotected beforeTerminalStop(_options?: TuiStopOptions): void {}\n\n\tprotected afterTerminalStop(_options?: TuiStopOptions): void {}\n\n\tget fullRedraws(): number {\n\t\treturn this.fullRedrawCount;\n\t}\n\n\tgetShowHardwareCursor(): boolean {\n\t\treturn this.showHardwareCursor;\n\t}\n\n\tsetShowHardwareCursor(enabled: boolean): void {\n\t\tif (this.showHardwareCursor === enabled) return;\n\t\tthis.showHardwareCursor = enabled;\n\t\tif (!enabled) {\n\t\t\tthis.terminal.hideCursor();\n\t\t}\n\t\tthis.requestRender();\n\t}\n\n\tgetClearOnShrink(): boolean {\n\t\treturn this.clearOnShrink;\n\t}\n\n\t/**\n\t * Set whether to trigger full re-render when content shrinks.\n\t * When true (default), empty rows are cleared when content shrinks.\n\t * When false, empty rows remain (reduces redraws on slower terminals).\n\t */\n\tsetClearOnShrink(enabled: boolean): void {\n\t\tthis.clearOnShrink = enabled;\n\t}\n\n\tgetFocusedComponent(): Component | null {\n\t\treturn this.focusedComponent;\n\t}\n\n\tsetFocus(component: Component | null): void {\n\t\tthis.setFocusInternal({ component, overlayFocusRestore: \"clear\" });\n\t}\n\n\tprivate resolveNonOverlayFocus(\n\t\tnextFocus: Component,\n\t\tpreviousFocus: Component | null,\n\t\trestoreState: OverlayFocusRestoreState,\n\t): Component | null {\n\t\tif (restoreState.status === \"blocked\" && restoreState.blockedBy === previousFocus) {\n\t\t\tif (restoreState.resume.status === \"focus-target\" || !this.isComponentMounted(restoreState.blockedBy)) {\n\t\t\t\treturn this.resolveBlockedOverlayFocusResume(restoreState);\n\t\t\t}\n\t\t\tthis.overlayFocusRestore = {\n\t\t\t\tstatus: \"blocked\",\n\t\t\t\toverlay: restoreState.overlay,\n\t\t\t\tblockedBy: nextFocus,\n\t\t\t\tresume: restoreState.resume,\n\t\t\t};\n\t\t\treturn nextFocus;\n\t\t}\n\n\t\tconst previousFocusedOverlay = previousFocus\n\t\t\t? this.overlayStack.find((entry) => entry.component === previousFocus && this.isOverlayVisible(entry))\n\t\t\t: undefined;\n\t\tif (\n\t\t\tpreviousFocusedOverlay &&\n\t\t\trestoreState.status !== \"inactive\" &&\n\t\t\trestoreState.overlay === previousFocusedOverlay &&\n\t\t\t!this.isOverlayFocusAncestor(previousFocusedOverlay, nextFocus)\n\t\t) {\n\t\t\tthis.overlayFocusRestore = {\n\t\t\t\tstatus: \"blocked\",\n\t\t\t\toverlay: previousFocusedOverlay,\n\t\t\t\tblockedBy: nextFocus,\n\t\t\t\tresume: { status: \"restore-overlay\" },\n\t\t\t};\n\t\t}\n\t\treturn nextFocus;\n\t}\n\n\tprivate resolveNullFocus(\n\t\tpreviousFocus: Component | null,\n\t\trestoreState: OverlayFocusRestoreState,\n\t\toverlayFocusRestore: OverlayFocusRestorePolicy,\n\t): Component | null {\n\t\tif (restoreState.status === \"blocked\" && restoreState.blockedBy === previousFocus) {\n\t\t\treturn this.resolveBlockedOverlayFocusResume(restoreState);\n\t\t}\n\t\tif (overlayFocusRestore === \"clear\") this.clearOverlayFocusRestore();\n\t\treturn null;\n\t}\n\n\tprivate resolveFocusTarget(\n\t\tcomponent: Component | null,\n\t\toverlayFocusRestore: OverlayFocusRestorePolicy,\n\t): Component | null {\n\t\tconst previousFocus = this.focusedComponent;\n\t\tconst restoreState = this.getVisibleOverlayFocusRestore();\n\t\tif (component === null) return this.resolveNullFocus(previousFocus, restoreState, overlayFocusRestore);\n\t\tconst isOverlay = this.overlayStack.some((entry) => entry.component === component);\n\t\tif (isOverlay) return component;\n\t\treturn this.resolveNonOverlayFocus(component, previousFocus, restoreState);\n\t}\n\n\tprivate applyFocus(nextFocus: Component | null): void {\n\t\tif (isFocusable(this.focusedComponent)) this.focusedComponent.focused = false;\n\t\tthis.focusedComponent = nextFocus;\n\t\tif (isFocusable(nextFocus)) nextFocus.focused = true;\n\n\t\tconst focusedOverlay = nextFocus\n\t\t\t? this.overlayStack.find((entry) => entry.component === nextFocus && this.isOverlayVisible(entry))\n\t\t\t: undefined;\n\t\tif (focusedOverlay) this.overlayFocusRestore = { status: \"eligible\", overlay: focusedOverlay };\n\t}\n\n\tprivate setFocusInternal({ component, overlayFocusRestore }: FocusTransitionRequest): void {\n\t\tconst nextFocus = this.resolveFocusTarget(component, overlayFocusRestore);\n\t\tthis.applyFocus(nextFocus);\n\t}\n\n\tprivate clearOverlayFocusRestore(): void {\n\t\tthis.overlayFocusRestore = { status: \"inactive\" };\n\t}\n\n\tprivate clearOverlayFocusRestoreFor(overlay: OverlayStackEntry): void {\n\t\tif (this.overlayFocusRestore.status !== \"inactive\" && this.overlayFocusRestore.overlay === overlay) {\n\t\t\tthis.clearOverlayFocusRestore();\n\t\t}\n\t}\n\n\tprivate resolveBlockedOverlayFocusResume(restoreState: BlockedOverlayFocusRestoreState): Component | null {\n\t\tif (restoreState.resume.status === \"restore-overlay\") return restoreState.overlay.component;\n\t\tthis.clearOverlayFocusRestore();\n\t\treturn restoreState.resume.target;\n\t}\n\n\tprivate getVisibleOverlayFocusRestore(): OverlayFocusRestoreState {\n\t\tconst restoreState = this.overlayFocusRestore;\n\t\tif (restoreState.status === \"inactive\") return restoreState;\n\t\tif (!this.overlayStack.includes(restoreState.overlay) || !this.isOverlayVisible(restoreState.overlay)) {\n\t\t\treturn { status: \"inactive\" };\n\t\t}\n\t\treturn restoreState;\n\t}\n\n\tprivate isOverlayFocusAncestor(entry: OverlayStackEntry, component: Component): boolean {\n\t\tconst preFocusByComponent = new Map<Component, Component | null>();\n\t\tfor (const overlay of this.overlayStack) {\n\t\t\tif (!preFocusByComponent.has(overlay.component)) {\n\t\t\t\tpreFocusByComponent.set(overlay.component, overlay.preFocus);\n\t\t\t}\n\t\t}\n\n\t\tconst visited = new Set<Component>();\n\t\tlet current = entry.preFocus;\n\t\twhile (current && !visited.has(current)) {\n\t\t\tvisited.add(current);\n\t\t\tif (current === component) return true;\n\t\t\tcurrent = preFocusByComponent.get(current) ?? null;\n\t\t}\n\t\treturn false;\n\t}\n\n\tprivate retargetOverlayPreFocus(removed: OverlayStackEntry): void {\n\t\tfor (const overlay of this.overlayStack) {\n\t\t\tif (overlay !== removed && overlay.preFocus === removed.component) {\n\t\t\t\toverlay.preFocus = removed.preFocus;\n\t\t\t}\n\t\t}\n\t}\n\n\tprotected getMountedRoots(): readonly Component[] {\n\t\treturn this.children;\n\t}\n\n\tprivate isComponentMounted(component: Component): boolean {\n\t\treturn this.getMountedRoots().some((child) => this.containsComponent(child, component));\n\t}\n\n\tprivate containsComponent(root: Component, target: Component): boolean {\n\t\tif (root === target) return true;\n\t\tif (!(root instanceof Container)) return false;\n\t\treturn root.children.some((child) => this.containsComponent(child, target));\n\t}\n\n\tprivate applyBlockedOverlayUnfocus(\n\t\tentry: OverlayStackEntry,\n\t\trestoreState: OverlayFocusRestoreState,\n\t\toptions: OverlayUnfocusOptions | undefined,\n\t): boolean {\n\t\tif (\n\t\t\trestoreState.status !== \"blocked\" ||\n\t\t\trestoreState.overlay !== entry ||\n\t\t\tthis.focusedComponent !== restoreState.blockedBy\n\t\t) {\n\t\t\treturn false;\n\t\t}\n\t\tif (options) {\n\t\t\tthis.overlayFocusRestore = {\n\t\t\t\tstatus: \"blocked\",\n\t\t\t\toverlay: entry,\n\t\t\t\tblockedBy: restoreState.blockedBy,\n\t\t\t\tresume: { status: \"focus-target\", target: options.target },\n\t\t\t};\n\t\t} else {\n\t\t\tthis.clearOverlayFocusRestore();\n\t\t}\n\t\tthis.requestRender();\n\t\treturn true;\n\t}\n\n\tprivate unfocusOverlay(entry: OverlayStackEntry, options: OverlayUnfocusOptions | undefined): void {\n\t\tconst isFocused = this.focusedComponent === entry.component;\n\t\tconst restoreState = this.overlayFocusRestore;\n\t\tconst hasPendingRestore = restoreState.status !== \"inactive\" && restoreState.overlay === entry;\n\t\tif (!isFocused && !hasPendingRestore) return;\n\t\tif (this.applyBlockedOverlayUnfocus(entry, restoreState, options)) return;\n\n\t\tthis.clearOverlayFocusRestoreFor(entry);\n\t\tif (isFocused || options) {\n\t\t\tconst topVisible = this.getTopmostVisibleOverlay();\n\t\t\tconst fallbackTarget = topVisible && topVisible !== entry ? topVisible.component : entry.preFocus;\n\t\t\tthis.setFocus(options ? options.target : fallbackTarget);\n\t\t}\n\t\tthis.requestRender();\n\t}\n\n\t/**\n\t * Show an overlay component with configurable positioning and sizing.\n\t * Returns a handle to control the overlay's visibility.\n\t */\n\tshowOverlay(component: Component, options?: OverlayOptions): OverlayHandle {\n\t\tconst entry: OverlayStackEntry = {\n\t\t\tcomponent,\n\t\t\t...(options === undefined ? {} : { options }),\n\t\t\tpreFocus: this.focusedComponent,\n\t\t\thidden: false,\n\t\t\tfocusOrder: ++this.focusOrderCounter,\n\t\t\tvisible: false,\n\t\t};\n\t\tthis.overlayStack.push(entry);\n\t\tentry.visible = this.isOverlayVisible(entry);\n\t\tthis.onOverlayStackChanged();\n\t\t// Only focus if overlay is actually visible\n\t\tif (!options?.nonCapturing && this.isOverlayVisible(entry)) {\n\t\t\tthis.setFocus(component);\n\t\t}\n\t\tthis.terminal.hideCursor();\n\t\tthis.requestRender();\n\n\t\t// Return handle for controlling this overlay\n\t\treturn {\n\t\t\thide: () => {\n\t\t\t\tconst index = this.overlayStack.indexOf(entry);\n\t\t\t\tif (index !== -1) {\n\t\t\t\t\tthis.clearOverlayFocusRestoreFor(entry);\n\t\t\t\t\tthis.retargetOverlayPreFocus(entry);\n\t\t\t\t\tthis.overlayStack.splice(index, 1);\n\t\t\t\t\tthis.onOverlayStackChanged();\n\t\t\t\t\t// Restore focus if this overlay had focus\n\t\t\t\t\tif (this.focusedComponent === component) {\n\t\t\t\t\t\tconst topVisible = this.getTopmostVisibleOverlay();\n\t\t\t\t\t\tthis.setFocus(topVisible?.component ?? entry.preFocus);\n\t\t\t\t\t}\n\t\t\t\t\tif (this.overlayStack.length === 0) this.terminal.hideCursor();\n\t\t\t\t\tthis.requestRender();\n\t\t\t\t}\n\t\t\t},\n\t\t\tsetHidden: (hidden: boolean) => {\n\t\t\t\tif (entry.hidden === hidden) return;\n\t\t\t\tentry.hidden = hidden;\n\t\t\t\tentry.visible = this.isOverlayVisible(entry);\n\t\t\t\t// Update focus when hiding/showing\n\t\t\t\tif (hidden) {\n\t\t\t\t\tthis.clearOverlayFocusRestoreFor(entry);\n\t\t\t\t\t// If this overlay had focus, move focus to next visible or preFocus\n\t\t\t\t\tif (this.focusedComponent === component) {\n\t\t\t\t\t\tconst topVisible = this.getTopmostVisibleOverlay();\n\t\t\t\t\t\tthis.setFocus(topVisible?.component ?? entry.preFocus);\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\t// Restore focus to this overlay when showing (if it's actually visible)\n\t\t\t\t\tif (!options?.nonCapturing && this.isOverlayVisible(entry)) {\n\t\t\t\t\t\tentry.focusOrder = ++this.focusOrderCounter;\n\t\t\t\t\t\tthis.setFocus(component);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tthis.onOverlayStackChanged();\n\t\t\t\tthis.requestRender();\n\t\t\t},\n\t\t\tisHidden: () => entry.hidden,\n\t\t\tfocus: () => {\n\t\t\t\tif (!this.overlayStack.includes(entry) || !this.isOverlayVisible(entry)) return;\n\t\t\t\tentry.focusOrder = ++this.focusOrderCounter;\n\t\t\t\tthis.setFocus(component);\n\t\t\t\tthis.requestRender();\n\t\t\t},\n\t\t\tunfocus: (unfocusOptions) => this.unfocusOverlay(entry, unfocusOptions),\n\t\t\tisFocused: () => this.focusedComponent === component,\n\t\t};\n\t}\n\n\t/** Hide the topmost overlay and restore previous focus. */\n\thideOverlay(): void {\n\t\tconst overlay = this.overlayStack[this.overlayStack.length - 1];\n\t\tif (!overlay) return;\n\t\tthis.clearOverlayFocusRestoreFor(overlay);\n\t\tthis.retargetOverlayPreFocus(overlay);\n\t\tthis.overlayStack.pop();\n\t\tthis.onOverlayStackChanged();\n\t\tif (this.focusedComponent === overlay.component) {\n\t\t\t// Find topmost visible overlay, or fall back to preFocus\n\t\t\tconst topVisible = this.getTopmostVisibleOverlay();\n\t\t\tthis.setFocus(topVisible?.component ?? overlay.preFocus);\n\t\t}\n\t\tif (this.overlayStack.length === 0) this.terminal.hideCursor();\n\t\tthis.requestRender();\n\t}\n\n\t/** Check if there are any visible overlays */\n\thasOverlay(): boolean {\n\t\treturn this.overlayStack.some((o) => this.isOverlayVisible(o));\n\t}\n\n\tprotected hasCapturingOverlay(): boolean {\n\t\treturn this.overlayStack.some((o) => !o.options?.nonCapturing && this.isOverlayVisible(o));\n\t}\n\n\t/** Check if an overlay entry is currently visible */\n\tprivate isOverlayVisible(entry: OverlayStackEntry): boolean {\n\t\tif (entry.hidden) return false;\n\t\tif (entry.options?.visible) {\n\t\t\treturn entry.options.visible(this.terminal.columns, this.terminal.rows);\n\t\t}\n\t\treturn true;\n\t}\n\n\t/** Find the visual-frontmost visible capturing overlay, if any */\n\tprivate getTopmostVisibleOverlay(): OverlayStackEntry | undefined {\n\t\tlet topmost: OverlayStackEntry | undefined;\n\t\tfor (const overlay of this.overlayStack) {\n\t\t\tif (overlay.options?.nonCapturing || !this.isOverlayVisible(overlay)) continue;\n\t\t\tif (!topmost || overlay.focusOrder > topmost.focusOrder) {\n\t\t\t\ttopmost = overlay;\n\t\t\t}\n\t\t}\n\t\treturn topmost;\n\t}\n\n\tprivate refreshOverlayVisibility(): OverlayStackEntry | undefined {\n\t\tlet newlyVisible: OverlayStackEntry | undefined;\n\t\tfor (const overlay of this.overlayStack) {\n\t\t\tconst visible = this.isOverlayVisible(overlay);\n\t\t\tif (visible && !overlay.visible && !overlay.options?.nonCapturing) {\n\t\t\t\tif (!newlyVisible || overlay.focusOrder > newlyVisible.focusOrder) newlyVisible = overlay;\n\t\t\t}\n\t\t\toverlay.visible = visible;\n\t\t}\n\t\treturn newlyVisible;\n\t}\n\n\tprivate retargetFocusFromHiddenOverlay(): void {\n\t\tconst focusedOverlay = this.overlayStack.find((overlay) => overlay.component === this.focusedComponent);\n\t\tif (!focusedOverlay || this.isOverlayVisible(focusedOverlay)) return;\n\t\tconst topVisible = this.getTopmostVisibleOverlay();\n\t\tif (topVisible) {\n\t\t\tthis.setFocus(topVisible.component);\n\t\t\treturn;\n\t\t}\n\t\tthis.setFocusInternal({ component: focusedOverlay.preFocus, overlayFocusRestore: \"preserve\" });\n\t}\n\n\tprivate focusNewlyVisibleOverlay(newlyVisible: OverlayStackEntry | undefined): void {\n\t\tif (!newlyVisible) return;\n\t\tconst currentFocusedOverlay = this.overlayStack.find(\n\t\t\t(overlay) => overlay.component === this.focusedComponent && this.isOverlayVisible(overlay),\n\t\t);\n\t\tif (!currentFocusedOverlay || newlyVisible.focusOrder > currentFocusedOverlay.focusOrder) {\n\t\t\tthis.setFocus(newlyVisible.component);\n\t\t}\n\t}\n\n\tprivate restoreVisibleOverlayFocus(): void {\n\t\tif (this.overlayStack.some((overlay) => overlay.component === this.focusedComponent)) return;\n\t\tconst restoreState = this.getVisibleOverlayFocusRestore();\n\t\tif (restoreState.status === \"eligible\") {\n\t\t\tthis.setFocus(restoreState.overlay.component);\n\t\t\treturn;\n\t\t}\n\t\tif (restoreState.status !== \"blocked\" || restoreState.blockedBy === this.focusedComponent) return;\n\t\tif (restoreState.resume.status === \"restore-overlay\") {\n\t\t\tthis.setFocus(restoreState.overlay.component);\n\t\t\treturn;\n\t\t}\n\t\tthis.clearOverlayFocusRestore();\n\t\tthis.setFocus(restoreState.resume.target);\n\t}\n\n\tprivate reconcileOverlayFocus(): void {\n\t\tconst newlyVisible = this.refreshOverlayVisibility();\n\t\tthis.retargetFocusFromHiddenOverlay();\n\t\tthis.focusNewlyVisibleOverlay(newlyVisible);\n\t\tthis.restoreVisibleOverlayFocus();\n\t}\n\n\toverride invalidate(): void {\n\t\tfor (const root of this.getMountedRoots()) root.invalidate();\n\t\tfor (const overlay of this.overlayStack) overlay.component.invalidate?.();\n\t}\n\n\tstart(): void {\n\t\tthis.stopped = false;\n\t\ttry {\n\t\t\tthis.beforeTerminalStart();\n\t\t\tthis.terminal.start(\n\t\t\t\t(data) => this.handleTerminalInput(data),\n\t\t\t\t() => {\n\t\t\t\t\tthis.reconcileOverlayFocus();\n\t\t\t\t\tthis.onTerminalResize();\n\t\t\t\t\tthis.requestRender();\n\t\t\t\t},\n\t\t\t);\n\t\t\tthis.afterTerminalStart();\n\t\t\tthis.terminal.hideCursor();\n\t\t\tthis.queryCellSize();\n\t\t\tthis.requestRender();\n\t\t} catch (error) {\n\t\t\t// A terminal may throw after partially entering raw or alternate-screen mode.\n\t\t\t// Route startup failures through the same complete cleanup path as signals.\n\t\t\ttry {\n\t\t\t\tthis.stop();\n\t\t\t} catch {\n\t\t\t\t// Preserve the startup failure; cleanup is best-effort but exhaustive.\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\taddInputListener(listener: TuiInputListener): () => void {\n\t\tthis.inputListeners.add(listener);\n\t\treturn () => {\n\t\t\tthis.inputListeners.delete(listener);\n\t\t};\n\t}\n\n\tremoveInputListener(listener: TuiInputListener): void {\n\t\tthis.inputListeners.delete(listener);\n\t}\n\n\tprivate queryCellSize(): void {\n\t\t// Only query if terminal supports images (cell size is only used for image rendering)\n\t\tif (!getCapabilities().images) {\n\t\t\treturn;\n\t\t}\n\t\t// Query terminal for cell size in pixels: CSI 16 t\n\t\t// Response format: CSI 6 ; height ; width t\n\t\tthis.terminal.write(\"\\x1b[16t\");\n\t}\n\n\tstop(options?: TuiStopOptions): void {\n\t\tthis.stopped = true;\n\t\tthis.renderRequested = false;\n\t\tthis.cancelRenderTimer();\n\t\tlet failure: unknown;\n\t\tconst cleanup = (action: () => void): void => {\n\t\t\ttry {\n\t\t\t\taction();\n\t\t\t} catch (error) {\n\t\t\t\tfailure ??= error;\n\t\t\t}\n\t\t};\n\t\tcleanup(() => this.beforeTerminalStop(options));\n\t\tcleanup(() => this.terminal.showCursor());\n\t\tcleanup(() => this.terminal.stop());\n\t\t// Alternate-screen exit and capability restoration must run even when\n\t\t// keyboard/raw-mode shutdown reports an error.\n\t\tcleanup(() => this.afterTerminalStop(options));\n\t\tif (failure !== undefined) throw failure;\n\t}\n\n\trenderNow(force = false): void {\n\t\tif (force) this.resetRenderState();\n\t\tthis.renderRequested = false;\n\t\tthis.cancelRenderTimer();\n\t\tthis.lastRenderAt = performance.now();\n\t\tthis.doRender();\n\t}\n\n\trequestRender(force = false): void {\n\t\tif (force) {\n\t\t\tthis.resetRenderState();\n\t\t\tthis.requestImmediateRender();\n\t\t\treturn;\n\t\t}\n\t\tif (this.renderRequested) return;\n\t\tthis.renderRequested = true;\n\t\tprocess.nextTick(() => this.scheduleRender());\n\t}\n\n\tprivate requestImmediateRender(): void {\n\t\tthis.cancelRenderTimer();\n\t\tthis.renderRequested = true;\n\t\tif (this.immediateRenderScheduled) return;\n\t\tthis.immediateRenderScheduled = true;\n\t\tprocess.nextTick(() => {\n\t\t\tthis.immediateRenderScheduled = false;\n\t\t\tif (this.stopped || !this.renderRequested) return;\n\t\t\tthis.cancelRenderTimer();\n\t\t\tthis.renderRequested = false;\n\t\t\tthis.lastRenderAt = performance.now();\n\t\t\tthis.doRender();\n\t\t});\n\t}\n\n\tprivate cancelRenderTimer(): void {\n\t\tif (!this.renderTimer) return;\n\t\tclearTimeout(this.renderTimer);\n\t\tthis.renderTimer = undefined;\n\t}\n\n\tprivate scheduleRender(): void {\n\t\tif (this.stopped || this.renderTimer || !this.renderRequested) return;\n\t\tconst elapsed = performance.now() - this.lastRenderAt;\n\t\tconst delay = Math.max(0, TuiBase.MIN_RENDER_INTERVAL_MS - elapsed);\n\t\tthis.renderTimer = setTimeout(() => {\n\t\t\tthis.renderTimer = undefined;\n\t\t\tif (this.stopped || !this.renderRequested) return;\n\t\t\tthis.renderRequested = false;\n\t\t\tthis.lastRenderAt = performance.now();\n\t\t\tthis.doRender();\n\t\t\tif (this.renderRequested) this.scheduleRender();\n\t\t}, delay);\n\t}\n\n\tprivate applyInputListeners(data: string): string | undefined {\n\t\tif (this.inputListeners.size === 0) return data;\n\t\tlet current = data;\n\t\tfor (const listener of this.inputListeners) {\n\t\t\tconst result = listener(current);\n\t\t\tif (result?.consume) return undefined;\n\t\t\tif (result?.data !== undefined) current = result.data;\n\t\t}\n\t\treturn current.length === 0 ? undefined : current;\n\t}\n\n\tprivate handleTerminalInput(data: string): void {\n\t\tthis.reconcileOverlayFocus();\n\t\tconst filteredData = this.applyInputListeners(data);\n\t\tif (filteredData === undefined) return;\n\t\tdata = filteredData;\n\n\t\t// Consume terminal cell size responses without blocking unrelated input.\n\t\tif (this.consumeCellSizeResponse(data)) return;\n\n\t\t// Global debug key handler (Shift+Ctrl+D)\n\t\tif (matchesKey(data, \"shift+ctrl+d\") && this.onDebug) {\n\t\t\tthis.onDebug();\n\t\t\treturn;\n\t\t}\n\n\t\t// Pass input to focused component (including Ctrl+C).\n\t\t// The focused component can decide how to handle Ctrl+C.\n\t\tif (!this.focusedComponent?.handleInput) return;\n\t\t// Filter out key release events unless component opts in.\n\t\tif (isKeyRelease(data) && !this.focusedComponent.wantsKeyRelease) return;\n\t\tthis.focusedComponent.handleInput(data);\n\t\tthis.requestImmediateRender();\n\t}\n\n\tprivate consumeCellSizeResponse(data: string): boolean {\n\t\t// Response format: ESC [ 6 ; height ; width t\n\t\tconst match = data.match(/^\\x1b\\[6;(\\d+);(\\d+)t$/);\n\t\tif (!match) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst heightPx = parseInt(match[1], 10);\n\t\tconst widthPx = parseInt(match[2], 10);\n\t\tif (heightPx <= 0 || widthPx <= 0) {\n\t\t\treturn true;\n\t\t}\n\n\t\tsetCellDimensions({ widthPx, heightPx });\n\t\t// Invalidate all components so images re-render with correct dimensions.\n\t\tthis.invalidate();\n\t\tthis.requestRender();\n\t\treturn true;\n\t}\n\n\tprivate resolveOverlayBounds(options: OverlayOptions, termWidth: number, termHeight: number): OverlayLayoutBounds {\n\t\tconst margin =\n\t\t\ttypeof options.margin === \"number\"\n\t\t\t\t? { top: options.margin, right: options.margin, bottom: options.margin, left: options.margin }\n\t\t\t\t: (options.margin ?? {});\n\t\tconst marginTop = Math.max(0, margin.top ?? 0);\n\t\tconst marginRight = Math.max(0, margin.right ?? 0);\n\t\tconst marginBottom = Math.max(0, margin.bottom ?? 0);\n\t\tconst marginLeft = Math.max(0, margin.left ?? 0);\n\t\treturn {\n\t\t\tmarginTop,\n\t\t\tmarginRight,\n\t\t\tmarginBottom,\n\t\t\tmarginLeft,\n\t\t\tavailableWidth: Math.max(1, termWidth - marginLeft - marginRight),\n\t\t\tavailableHeight: Math.max(1, termHeight - marginTop - marginBottom),\n\t\t};\n\t}\n\n\tprivate resolveOverlayWidth(options: OverlayOptions, termWidth: number, bounds: OverlayLayoutBounds): number {\n\t\tlet width = parseSizeValue(options.width, termWidth) ?? Math.min(80, bounds.availableWidth);\n\t\tif (options.minWidth !== undefined) width = Math.max(width, options.minWidth);\n\t\treturn Math.max(1, Math.min(width, bounds.availableWidth));\n\t}\n\n\tprivate resolveOverlayMaxHeight(\n\t\toptions: OverlayOptions,\n\t\ttermHeight: number,\n\t\tbounds: OverlayLayoutBounds,\n\t): number | undefined {\n\t\tconst maxHeight = parseSizeValue(options.maxHeight, termHeight);\n\t\treturn maxHeight === undefined ? undefined : Math.max(1, Math.min(maxHeight, bounds.availableHeight));\n\t}\n\n\tprivate resolveOverlayRow(options: OverlayOptions, overlayHeight: number, bounds: OverlayLayoutBounds): number {\n\t\tif (options.row === undefined) {\n\t\t\treturn this.resolveAnchorRow(\n\t\t\t\toptions.anchor ?? \"center\",\n\t\t\t\toverlayHeight,\n\t\t\t\tbounds.availableHeight,\n\t\t\t\tbounds.marginTop,\n\t\t\t);\n\t\t}\n\t\tif (typeof options.row === \"number\") return options.row;\n\t\tconst match = options.row.match(/^(\\d+(?:\\.\\d+)?)%$/);\n\t\tif (!match) return this.resolveAnchorRow(\"center\", overlayHeight, bounds.availableHeight, bounds.marginTop);\n\t\tconst maxRow = Math.max(0, bounds.availableHeight - overlayHeight);\n\t\treturn bounds.marginTop + Math.floor(maxRow * (parseFloat(match[1]) / 100));\n\t}\n\n\tprivate resolveOverlayColumn(options: OverlayOptions, width: number, bounds: OverlayLayoutBounds): number {\n\t\tif (options.col === undefined) {\n\t\t\treturn this.resolveAnchorCol(options.anchor ?? \"center\", width, bounds.availableWidth, bounds.marginLeft);\n\t\t}\n\t\tif (typeof options.col === \"number\") return options.col;\n\t\tconst match = options.col.match(/^(\\d+(?:\\.\\d+)?)%$/);\n\t\tif (!match) return this.resolveAnchorCol(\"center\", width, bounds.availableWidth, bounds.marginLeft);\n\t\tconst maxCol = Math.max(0, bounds.availableWidth - width);\n\t\treturn bounds.marginLeft + Math.floor(maxCol * (parseFloat(match[1]) / 100));\n\t}\n\n\t/** Resolve overlay layout from options. */\n\tprivate resolveOverlayLayout(\n\t\toptions: OverlayOptions | undefined,\n\t\toverlayHeight: number,\n\t\ttermWidth: number,\n\t\ttermHeight: number,\n\t): ResolvedOverlayLayout {\n\t\tconst resolvedOptions = options ?? {};\n\t\tconst bounds = this.resolveOverlayBounds(resolvedOptions, termWidth, termHeight);\n\t\tconst width = this.resolveOverlayWidth(resolvedOptions, termWidth, bounds);\n\t\tconst maxHeight = this.resolveOverlayMaxHeight(resolvedOptions, termHeight, bounds);\n\t\tconst effectiveHeight = maxHeight === undefined ? overlayHeight : Math.min(overlayHeight, maxHeight);\n\t\tlet row = this.resolveOverlayRow(resolvedOptions, effectiveHeight, bounds) + (resolvedOptions.offsetY ?? 0);\n\t\tlet col = this.resolveOverlayColumn(resolvedOptions, width, bounds) + (resolvedOptions.offsetX ?? 0);\n\t\trow = Math.max(bounds.marginTop, Math.min(row, termHeight - bounds.marginBottom - effectiveHeight));\n\t\tcol = Math.max(bounds.marginLeft, Math.min(col, termWidth - bounds.marginRight - width));\n\t\treturn { width, row, col, maxHeight };\n\t}\n\n\tprivate resolveAnchorRow(anchor: OverlayAnchor, height: number, availHeight: number, marginTop: number): number {\n\t\tswitch (anchor) {\n\t\t\tcase \"top-left\":\n\t\t\tcase \"top-center\":\n\t\t\tcase \"top-right\":\n\t\t\t\treturn marginTop;\n\t\t\tcase \"bottom-left\":\n\t\t\tcase \"bottom-center\":\n\t\t\tcase \"bottom-right\":\n\t\t\t\treturn marginTop + availHeight - height;\n\t\t\tcase \"left-center\":\n\t\t\tcase \"center\":\n\t\t\tcase \"right-center\":\n\t\t\t\treturn marginTop + Math.floor((availHeight - height) / 2);\n\t\t}\n\t}\n\n\tprivate resolveAnchorCol(anchor: OverlayAnchor, width: number, availWidth: number, marginLeft: number): number {\n\t\tswitch (anchor) {\n\t\t\tcase \"top-left\":\n\t\t\tcase \"left-center\":\n\t\t\tcase \"bottom-left\":\n\t\t\t\treturn marginLeft;\n\t\t\tcase \"top-right\":\n\t\t\tcase \"right-center\":\n\t\t\tcase \"bottom-right\":\n\t\t\t\treturn marginLeft + availWidth - width;\n\t\t\tcase \"top-center\":\n\t\t\tcase \"center\":\n\t\t\tcase \"bottom-center\":\n\t\t\t\treturn marginLeft + Math.floor((availWidth - width) / 2);\n\t\t}\n\t}\n\n\tprivate prepareOverlayRenderPlan(baseLineCount: number, termWidth: number, termHeight: number): OverlayRenderPlan {\n\t\tconst visibleEntries = this.overlayStack.filter((entry) => this.isOverlayVisible(entry));\n\t\tvisibleEntries.sort((left, right) => left.focusOrder - right.focusOrder);\n\t\tconst overlays: RenderedOverlay[] = [];\n\t\tlet minimumLineCount = baseLineCount;\n\t\tfor (const entry of visibleEntries) {\n\t\t\tconst { width, maxHeight } = this.resolveOverlayLayout(entry.options, 0, termWidth, termHeight);\n\t\t\tlet overlayLines = entry.component.render(width);\n\t\t\tif (maxHeight !== undefined && overlayLines.length > maxHeight) {\n\t\t\t\toverlayLines = overlayLines.slice(0, maxHeight);\n\t\t\t}\n\t\t\tconst { row, col } = this.resolveOverlayLayout(entry.options, overlayLines.length, termWidth, termHeight);\n\t\t\toverlays.push({ lines: overlayLines, row, col, width });\n\t\t\tminimumLineCount = Math.max(minimumLineCount, row + overlayLines.length);\n\t\t}\n\t\treturn { overlays, minimumLineCount };\n\t}\n\n\tprivate compositeRenderedOverlay(\n\t\tresult: string[],\n\t\toverlay: RenderedOverlay,\n\t\tviewportStart: number,\n\t\ttermWidth: number,\n\t): void {\n\t\tfor (let lineIndex = 0; lineIndex < overlay.lines.length; lineIndex++) {\n\t\t\tconst targetIndex = viewportStart + overlay.row + lineIndex;\n\t\t\tif (targetIndex < 0 || targetIndex >= result.length) continue;\n\t\t\tconst overlayLine = overlay.lines[lineIndex]!;\n\t\t\tconst truncatedLine =\n\t\t\t\tvisibleWidth(overlayLine) > overlay.width\n\t\t\t\t\t? sliceByColumn(overlayLine, 0, overlay.width, true)\n\t\t\t\t\t: overlayLine;\n\t\t\tresult[targetIndex] = this.compositeLineAt(\n\t\t\t\tresult[targetIndex]!,\n\t\t\t\ttruncatedLine,\n\t\t\t\toverlay.col,\n\t\t\t\toverlay.width,\n\t\t\t\ttermWidth,\n\t\t\t);\n\t\t}\n\t}\n\n\t/** Composite all overlays into content lines (sorted by focusOrder, higher = on top). */\n\tprotected compositeOverlays(lines: string[], termWidth: number, termHeight: number): string[] {\n\t\tif (this.overlayStack.length === 0) return lines;\n\t\tconst result = [...lines];\n\t\tconst plan = this.prepareOverlayRenderPlan(result.length, termWidth, termHeight);\n\t\tconst workingHeight = Math.max(result.length, termHeight, plan.minimumLineCount);\n\t\twhile (result.length < workingHeight) result.push(\"\");\n\t\tconst viewportStart = Math.max(0, workingHeight - termHeight);\n\t\tfor (const overlay of plan.overlays) {\n\t\t\tthis.compositeRenderedOverlay(result, overlay, viewportStart, termWidth);\n\t\t}\n\t\treturn result;\n\t}\n\n\tprotected applyLineResets(lines: string[]): string[] {\n\t\tconst reset = SEGMENT_RESET;\n\t\tfor (let i = 0; i < lines.length; i++) {\n\t\t\tconst line = lines[i];\n\t\t\tif (!isImageLine(line)) {\n\t\t\t\tlines[i] = normalizeTerminalOutput(line) + reset;\n\t\t\t}\n\t\t}\n\t\treturn lines;\n\t}\n\n\tprivate compositeLineAt(\n\t\tbaseLine: string,\n\t\toverlayLine: string,\n\t\tstartCol: number,\n\t\toverlayWidth: number,\n\t\ttotalWidth: number,\n\t): string {\n\t\treturn compositeTuiLine(baseLine, overlayLine, startCol, overlayWidth, totalWidth);\n\t}\n\n\t/**\n\t * Find and extract cursor position from rendered lines.\n\t * Searches for CURSOR_MARKER, calculates its position, and strips it from the output.\n\t * Only scans the bottom terminal height lines (visible viewport).\n\t * @param lines - Rendered lines to search\n\t * @param height - Terminal height (visible viewport size)\n\t * @returns Cursor position { row, col } or null if no marker found\n\t */\n\tprotected extractCursorPosition(lines: string[], height: number): RenderedCursorPosition | null {\n\t\t// Only scan the bottom `height` lines (visible viewport)\n\t\tconst viewportTop = Math.max(0, lines.length - height);\n\t\tfor (let row = lines.length - 1; row >= viewportTop; row--) {\n\t\t\tconst line = lines[row];\n\t\t\tconst markerIndex = line.indexOf(CURSOR_MARKER);\n\t\t\tif (markerIndex !== -1) {\n\t\t\t\t// Calculate visual column (width of text before marker)\n\t\t\t\tconst beforeMarker = line.slice(0, markerIndex);\n\t\t\t\tconst col = visibleWidth(beforeMarker);\n\n\t\t\t\t// Strip marker from the line\n\t\t\t\tlines[row] = line.slice(0, markerIndex) + line.slice(markerIndex + CURSOR_MARKER.length);\n\n\t\t\t\treturn { row, col };\n\t\t\t}\n\t\t}\n\t\treturn null;\n\t}\n}\n"]}
|