@dsh-cc/pi-tui 0.5.0
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/LICENSE +21 -0
- package/PORTING.md +18 -0
- package/README.md +7 -0
- package/lib/alt-screen-search.d.ts +26 -0
- package/lib/alt-screen-search.d.ts.map +1 -0
- package/lib/alt-screen-search.js +122 -0
- package/lib/alt-screen-search.js.map +1 -0
- package/lib/autocomplete.d.ts +56 -0
- package/lib/autocomplete.d.ts.map +1 -0
- package/lib/autocomplete.js +632 -0
- package/lib/autocomplete.js.map +1 -0
- package/lib/components/alt-screen-flash.d.ts +13 -0
- package/lib/components/alt-screen-flash.d.ts.map +1 -0
- package/lib/components/alt-screen-flash.js +37 -0
- package/lib/components/alt-screen-flash.js.map +1 -0
- package/lib/components/box.d.ts +22 -0
- package/lib/components/box.d.ts.map +1 -0
- package/lib/components/box.js +104 -0
- package/lib/components/box.js.map +1 -0
- package/lib/components/cancellable-loader.d.ts +22 -0
- package/lib/components/cancellable-loader.d.ts.map +1 -0
- package/lib/components/cancellable-loader.js +35 -0
- package/lib/components/cancellable-loader.js.map +1 -0
- package/lib/components/editor.d.ts +255 -0
- package/lib/components/editor.d.ts.map +1 -0
- package/lib/components/editor.js +1961 -0
- package/lib/components/editor.js.map +1 -0
- package/lib/components/h-stack.d.ts +7 -0
- package/lib/components/h-stack.d.ts.map +1 -0
- package/lib/components/h-stack.js +43 -0
- package/lib/components/h-stack.js.map +1 -0
- package/lib/components/image.d.ts +28 -0
- package/lib/components/image.d.ts.map +1 -0
- package/lib/components/image.js +90 -0
- package/lib/components/image.js.map +1 -0
- package/lib/components/input.d.ts +37 -0
- package/lib/components/input.d.ts.map +1 -0
- package/lib/components/input.js +378 -0
- package/lib/components/input.js.map +1 -0
- package/lib/components/loader.d.ts +31 -0
- package/lib/components/loader.d.ts.map +1 -0
- package/lib/components/loader.js +69 -0
- package/lib/components/loader.js.map +1 -0
- package/lib/components/markdown.d.ts +103 -0
- package/lib/components/markdown.d.ts.map +1 -0
- package/lib/components/markdown.js +811 -0
- package/lib/components/markdown.js.map +1 -0
- package/lib/components/scroll-view.d.ts +56 -0
- package/lib/components/scroll-view.d.ts.map +1 -0
- package/lib/components/scroll-view.js +189 -0
- package/lib/components/scroll-view.js.map +1 -0
- package/lib/components/select-list.d.ts +50 -0
- package/lib/components/select-list.d.ts.map +1 -0
- package/lib/components/select-list.js +159 -0
- package/lib/components/select-list.js.map +1 -0
- package/lib/components/settings-list.d.ts +56 -0
- package/lib/components/settings-list.d.ts.map +1 -0
- package/lib/components/settings-list.js +202 -0
- package/lib/components/settings-list.js.map +1 -0
- package/lib/components/spacer.d.ts +12 -0
- package/lib/components/spacer.d.ts.map +1 -0
- package/lib/components/spacer.js +23 -0
- package/lib/components/spacer.js.map +1 -0
- package/lib/components/stack.d.ts +32 -0
- package/lib/components/stack.d.ts.map +1 -0
- package/lib/components/stack.js +111 -0
- package/lib/components/stack.js.map +1 -0
- package/lib/components/text.d.ts +19 -0
- package/lib/components/text.d.ts.map +1 -0
- package/lib/components/text.js +90 -0
- package/lib/components/text.js.map +1 -0
- package/lib/components/truncated-text.d.ts +13 -0
- package/lib/components/truncated-text.d.ts.map +1 -0
- package/lib/components/truncated-text.js +51 -0
- package/lib/components/truncated-text.js.map +1 -0
- package/lib/components/v-stack.d.ts +8 -0
- package/lib/components/v-stack.d.ts.map +1 -0
- package/lib/components/v-stack.js +26 -0
- package/lib/components/v-stack.js.map +1 -0
- package/lib/editor-component.d.ts +39 -0
- package/lib/editor-component.d.ts.map +1 -0
- package/lib/editor-component.js +2 -0
- package/lib/editor-component.js.map +1 -0
- package/lib/fuzzy.d.ts +16 -0
- package/lib/fuzzy.d.ts.map +1 -0
- package/lib/fuzzy.js +110 -0
- package/lib/fuzzy.js.map +1 -0
- package/lib/index.d.ts +31 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +42 -0
- package/lib/index.js.map +1 -0
- package/lib/keybindings.d.ts +273 -0
- package/lib/keybindings.d.ts.map +1 -0
- package/lib/keybindings.js +233 -0
- package/lib/keybindings.js.map +1 -0
- package/lib/keys.d.ts +184 -0
- package/lib/keys.d.ts.map +1 -0
- package/lib/keys.js +1174 -0
- package/lib/keys.js.map +1 -0
- package/lib/kill-ring.d.ts +28 -0
- package/lib/kill-ring.d.ts.map +1 -0
- package/lib/kill-ring.js +44 -0
- package/lib/kill-ring.js.map +1 -0
- package/lib/latex.d.ts +10 -0
- package/lib/latex.d.ts.map +1 -0
- package/lib/latex.js +1271 -0
- package/lib/latex.js.map +1 -0
- package/lib/layout-node.d.ts +40 -0
- package/lib/layout-node.d.ts.map +1 -0
- package/lib/layout-node.js +6 -0
- package/lib/layout-node.js.map +1 -0
- package/lib/layout.d.ts +40 -0
- package/lib/layout.d.ts.map +1 -0
- package/lib/layout.js +321 -0
- package/lib/layout.js.map +1 -0
- package/lib/native-modifiers.d.ts +3 -0
- package/lib/native-modifiers.d.ts.map +1 -0
- package/lib/native-modifiers.js +54 -0
- package/lib/native-modifiers.js.map +1 -0
- package/lib/native-module-path.d.ts +7 -0
- package/lib/native-module-path.d.ts.map +1 -0
- package/lib/native-module-path.js +19 -0
- package/lib/native-module-path.js.map +1 -0
- package/lib/stdin-buffer.d.ts +56 -0
- package/lib/stdin-buffer.d.ts.map +1 -0
- package/lib/stdin-buffer.js +366 -0
- package/lib/stdin-buffer.js.map +1 -0
- package/lib/terminal-colors.d.ts +10 -0
- package/lib/terminal-colors.d.ts.map +1 -0
- package/lib/terminal-colors.js +59 -0
- package/lib/terminal-colors.js.map +1 -0
- package/lib/terminal-image.d.ts +116 -0
- package/lib/terminal-image.d.ts.map +1 -0
- package/lib/terminal-image.js +518 -0
- package/lib/terminal-image.js.map +1 -0
- package/lib/terminal.d.ts +117 -0
- package/lib/terminal.d.ts.map +1 -0
- package/lib/terminal.js +447 -0
- package/lib/terminal.js.map +1 -0
- package/lib/tui-alt-screen.d.ts +118 -0
- package/lib/tui-alt-screen.d.ts.map +1 -0
- package/lib/tui-alt-screen.js +1148 -0
- package/lib/tui-alt-screen.js.map +1 -0
- package/lib/tui-main-screen.d.ts +39 -0
- package/lib/tui-main-screen.d.ts.map +1 -0
- package/lib/tui-main-screen.js +533 -0
- package/lib/tui-main-screen.js.map +1 -0
- package/lib/tui.d.ts +308 -0
- package/lib/tui.d.ts.map +1 -0
- package/lib/tui.js +941 -0
- package/lib/tui.js.map +1 -0
- package/lib/undo-stack.d.ts +17 -0
- package/lib/undo-stack.d.ts.map +1 -0
- package/lib/undo-stack.js +25 -0
- package/lib/undo-stack.js.map +1 -0
- package/lib/utils.d.ts +96 -0
- package/lib/utils.d.ts.map +1 -0
- package/lib/utils.js +1191 -0
- package/lib/utils.js.map +1 -0
- package/lib/word-navigation.d.ts +25 -0
- package/lib/word-navigation.d.ts.map +1 -0
- package/lib/word-navigation.js +96 -0
- package/lib/word-navigation.js.map +1 -0
- package/package.json +42 -0
package/lib/tui.js
ADDED
|
@@ -0,0 +1,941 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal TUI implementation with differential rendering
|
|
3
|
+
*/
|
|
4
|
+
import * as os from "node:os";
|
|
5
|
+
import * as path from "node:path";
|
|
6
|
+
import { performance } from "node:perf_hooks";
|
|
7
|
+
import { isKeyRelease, matchesKey } from "./keys.js";
|
|
8
|
+
import { isOsc11BackgroundColorResponse, parseOsc11BackgroundColor, parseTerminalColorSchemeReport, } from "./terminal-colors.js";
|
|
9
|
+
import { getCapabilities, isImageLine, setCellDimensions } from "./terminal-image.js";
|
|
10
|
+
import { extractSegments, normalizeTerminalOutput, sliceByColumn, sliceWithWidth, visibleWidth } from "./utils.js";
|
|
11
|
+
/** Type guard to check if a component implements Focusable */
|
|
12
|
+
export function isFocusable(component) {
|
|
13
|
+
return component !== null && "focused" in component;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Cursor position marker - APC (Application Program Command) sequence.
|
|
17
|
+
* This is a zero-width escape sequence that terminals ignore.
|
|
18
|
+
* Components emit this at the cursor position when focused.
|
|
19
|
+
* TUI finds and strips this marker, then positions the hardware cursor there.
|
|
20
|
+
*/
|
|
21
|
+
export const CURSOR_MARKER = "\x1b_pi:c\x07";
|
|
22
|
+
export { visibleWidth };
|
|
23
|
+
/** Parse a SizeValue into absolute value given a reference size */
|
|
24
|
+
function parseSizeValue(value, referenceSize) {
|
|
25
|
+
if (value === undefined)
|
|
26
|
+
return undefined;
|
|
27
|
+
if (typeof value === "number")
|
|
28
|
+
return value;
|
|
29
|
+
// Parse percentage string like "50%"
|
|
30
|
+
const match = value.match(/^(\d+(?:\.\d+)?)%$/);
|
|
31
|
+
if (match) {
|
|
32
|
+
return Math.floor((referenceSize * parseFloat(match[1])) / 100);
|
|
33
|
+
}
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Container - a component that contains other components
|
|
38
|
+
*/
|
|
39
|
+
export class Container {
|
|
40
|
+
children = [];
|
|
41
|
+
addChild(component) {
|
|
42
|
+
this.children.push(component);
|
|
43
|
+
}
|
|
44
|
+
removeChild(component) {
|
|
45
|
+
const index = this.children.indexOf(component);
|
|
46
|
+
if (index !== -1) {
|
|
47
|
+
this.children.splice(index, 1);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
clear() {
|
|
51
|
+
this.children = [];
|
|
52
|
+
}
|
|
53
|
+
invalidate() {
|
|
54
|
+
for (const child of this.children) {
|
|
55
|
+
child.invalidate?.();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
render(width) {
|
|
59
|
+
const lines = [];
|
|
60
|
+
for (const child of this.children) {
|
|
61
|
+
const childLines = child.render(width);
|
|
62
|
+
for (const line of childLines) {
|
|
63
|
+
lines.push(line);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return lines;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* TUI - Main class for managing terminal UI with differential rendering
|
|
71
|
+
*/
|
|
72
|
+
const SEGMENT_RESET = "\x1b[0m\x1b]8;;\x07";
|
|
73
|
+
/** Composite overlay content into a terminal line at a fixed column. */
|
|
74
|
+
export function compositeTuiLine(baseLine, overlayLine, startCol, overlayWidth, totalWidth) {
|
|
75
|
+
if (isImageLine(baseLine))
|
|
76
|
+
return baseLine;
|
|
77
|
+
const afterStart = startCol + overlayWidth;
|
|
78
|
+
const base = extractSegments(baseLine, startCol, afterStart, totalWidth - afterStart, true);
|
|
79
|
+
const overlay = sliceWithWidth(overlayLine, 0, overlayWidth, true);
|
|
80
|
+
const beforePad = Math.max(0, startCol - base.beforeWidth);
|
|
81
|
+
const overlayPad = Math.max(0, overlayWidth - overlay.width);
|
|
82
|
+
const actualBeforeWidth = Math.max(startCol, base.beforeWidth);
|
|
83
|
+
const actualOverlayWidth = Math.max(overlayWidth, overlay.width);
|
|
84
|
+
const afterTarget = Math.max(0, totalWidth - actualBeforeWidth - actualOverlayWidth);
|
|
85
|
+
const afterPad = Math.max(0, afterTarget - base.afterWidth);
|
|
86
|
+
const result = base.before +
|
|
87
|
+
" ".repeat(beforePad) +
|
|
88
|
+
SEGMENT_RESET +
|
|
89
|
+
overlay.text +
|
|
90
|
+
" ".repeat(overlayPad) +
|
|
91
|
+
SEGMENT_RESET +
|
|
92
|
+
base.after +
|
|
93
|
+
" ".repeat(afterPad);
|
|
94
|
+
return visibleWidth(result) <= totalWidth ? result : sliceByColumn(result, 0, totalWidth, true);
|
|
95
|
+
}
|
|
96
|
+
export const VIEWPORT_TUI = Symbol.for("@earendil-works/pi-tui/viewport");
|
|
97
|
+
export function isViewportTUI(tui) {
|
|
98
|
+
return tui[VIEWPORT_TUI] === true;
|
|
99
|
+
}
|
|
100
|
+
export class TuiBase extends Container {
|
|
101
|
+
terminal;
|
|
102
|
+
focusedComponent = null;
|
|
103
|
+
inputListeners = new Set();
|
|
104
|
+
/** Global callback for debug key (Shift+Ctrl+D). Called before input is forwarded to focused component. */
|
|
105
|
+
onDebug;
|
|
106
|
+
renderRequested = false;
|
|
107
|
+
immediateRenderScheduled = false;
|
|
108
|
+
renderTimer;
|
|
109
|
+
lastRenderAt = 0;
|
|
110
|
+
static MIN_RENDER_INTERVAL_MS = 16;
|
|
111
|
+
showHardwareCursor = process.env.PI_HARDWARE_CURSOR === "1";
|
|
112
|
+
clearOnShrink = process.env.PI_CLEAR_ON_SHRINK === "1";
|
|
113
|
+
fullRedrawCount = 0;
|
|
114
|
+
stopped = false;
|
|
115
|
+
pendingOsc11BackgroundReplies = 0;
|
|
116
|
+
pendingOsc11BackgroundQueries = [];
|
|
117
|
+
terminalColorSchemeListeners = new Set();
|
|
118
|
+
terminalColorSchemeNotificationsEnabled = false;
|
|
119
|
+
logDirectory;
|
|
120
|
+
// Overlay stack for modal components rendered on top of base content
|
|
121
|
+
focusOrderCounter = 0;
|
|
122
|
+
overlayStack = [];
|
|
123
|
+
get hasOverlayEntries() {
|
|
124
|
+
return this.overlayStack.length > 0;
|
|
125
|
+
}
|
|
126
|
+
overlayFocusRestore = { status: "inactive" };
|
|
127
|
+
constructor(terminal, showHardwareCursor, logDirectory) {
|
|
128
|
+
super();
|
|
129
|
+
this.terminal = terminal;
|
|
130
|
+
this.logDirectory = logDirectory ?? process.env.PI_CODING_AGENT_DIR ?? path.join(os.homedir(), ".pi", "agent");
|
|
131
|
+
if (showHardwareCursor !== undefined) {
|
|
132
|
+
this.showHardwareCursor = showHardwareCursor;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
resetRenderState() { }
|
|
136
|
+
beforeTerminalStart() { }
|
|
137
|
+
afterTerminalStart() { }
|
|
138
|
+
beforeTerminalStop(_options) { }
|
|
139
|
+
afterTerminalStop(_options) { }
|
|
140
|
+
get fullRedraws() {
|
|
141
|
+
return this.fullRedrawCount;
|
|
142
|
+
}
|
|
143
|
+
getShowHardwareCursor() {
|
|
144
|
+
return this.showHardwareCursor;
|
|
145
|
+
}
|
|
146
|
+
setShowHardwareCursor(enabled) {
|
|
147
|
+
if (this.showHardwareCursor === enabled)
|
|
148
|
+
return;
|
|
149
|
+
this.showHardwareCursor = enabled;
|
|
150
|
+
if (!enabled) {
|
|
151
|
+
this.terminal.hideCursor();
|
|
152
|
+
}
|
|
153
|
+
this.requestRender();
|
|
154
|
+
}
|
|
155
|
+
getClearOnShrink() {
|
|
156
|
+
return this.clearOnShrink;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Set whether to trigger full re-render when content shrinks.
|
|
160
|
+
* When true (default), empty rows are cleared when content shrinks.
|
|
161
|
+
* When false, empty rows remain (reduces redraws on slower terminals).
|
|
162
|
+
*/
|
|
163
|
+
setClearOnShrink(enabled) {
|
|
164
|
+
this.clearOnShrink = enabled;
|
|
165
|
+
}
|
|
166
|
+
getFocusedComponent() {
|
|
167
|
+
return this.focusedComponent;
|
|
168
|
+
}
|
|
169
|
+
setFocus(component) {
|
|
170
|
+
this.setFocusInternal({ component, overlayFocusRestore: "clear" });
|
|
171
|
+
}
|
|
172
|
+
setFocusInternal({ component, overlayFocusRestore, }) {
|
|
173
|
+
const previousFocus = this.focusedComponent;
|
|
174
|
+
let nextFocus = component;
|
|
175
|
+
const previousFocusedOverlay = previousFocus
|
|
176
|
+
? this.overlayStack.find((entry) => entry.component === previousFocus && this.isOverlayVisible(entry))
|
|
177
|
+
: undefined;
|
|
178
|
+
const nextFocusIsOverlay = nextFocus ? this.overlayStack.some((entry) => entry.component === nextFocus) : false;
|
|
179
|
+
const restoreState = this.getVisibleOverlayFocusRestore();
|
|
180
|
+
if (nextFocus && !nextFocusIsOverlay) {
|
|
181
|
+
if (restoreState.status === "blocked" && restoreState.blockedBy === previousFocus) {
|
|
182
|
+
if (restoreState.resume.status === "focus-target" || !this.isComponentMounted(restoreState.blockedBy)) {
|
|
183
|
+
nextFocus = this.resolveBlockedOverlayFocusResume(restoreState);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
this.overlayFocusRestore = {
|
|
187
|
+
status: "blocked",
|
|
188
|
+
overlay: restoreState.overlay,
|
|
189
|
+
blockedBy: nextFocus,
|
|
190
|
+
resume: restoreState.resume,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
else if (previousFocusedOverlay &&
|
|
195
|
+
restoreState.status !== "inactive" &&
|
|
196
|
+
restoreState.overlay === previousFocusedOverlay &&
|
|
197
|
+
!this.isOverlayFocusAncestor(previousFocusedOverlay, nextFocus)) {
|
|
198
|
+
this.overlayFocusRestore = {
|
|
199
|
+
status: "blocked",
|
|
200
|
+
overlay: previousFocusedOverlay,
|
|
201
|
+
blockedBy: nextFocus,
|
|
202
|
+
resume: { status: "restore-overlay" },
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
else if (nextFocus === null) {
|
|
207
|
+
if (restoreState.status === "blocked" && restoreState.blockedBy === previousFocus) {
|
|
208
|
+
nextFocus = this.resolveBlockedOverlayFocusResume(restoreState);
|
|
209
|
+
}
|
|
210
|
+
else if (overlayFocusRestore === "clear") {
|
|
211
|
+
this.clearOverlayFocusRestore();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (isFocusable(this.focusedComponent)) {
|
|
215
|
+
this.focusedComponent.focused = false;
|
|
216
|
+
}
|
|
217
|
+
this.focusedComponent = nextFocus;
|
|
218
|
+
if (isFocusable(nextFocus)) {
|
|
219
|
+
nextFocus.focused = true;
|
|
220
|
+
}
|
|
221
|
+
const focusedOverlay = nextFocus
|
|
222
|
+
? this.overlayStack.find((entry) => entry.component === nextFocus && this.isOverlayVisible(entry))
|
|
223
|
+
: undefined;
|
|
224
|
+
if (focusedOverlay) {
|
|
225
|
+
this.overlayFocusRestore = { status: "eligible", overlay: focusedOverlay };
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
clearOverlayFocusRestore() {
|
|
229
|
+
this.overlayFocusRestore = { status: "inactive" };
|
|
230
|
+
}
|
|
231
|
+
clearOverlayFocusRestoreFor(overlay) {
|
|
232
|
+
if (this.overlayFocusRestore.status !== "inactive" && this.overlayFocusRestore.overlay === overlay) {
|
|
233
|
+
this.clearOverlayFocusRestore();
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
resolveBlockedOverlayFocusResume(restoreState) {
|
|
237
|
+
if (restoreState.resume.status === "restore-overlay")
|
|
238
|
+
return restoreState.overlay.component;
|
|
239
|
+
this.clearOverlayFocusRestore();
|
|
240
|
+
return restoreState.resume.target;
|
|
241
|
+
}
|
|
242
|
+
getVisibleOverlayFocusRestore() {
|
|
243
|
+
const restoreState = this.overlayFocusRestore;
|
|
244
|
+
if (restoreState.status === "inactive")
|
|
245
|
+
return restoreState;
|
|
246
|
+
if (!this.overlayStack.includes(restoreState.overlay) || !this.isOverlayVisible(restoreState.overlay)) {
|
|
247
|
+
return { status: "inactive" };
|
|
248
|
+
}
|
|
249
|
+
return restoreState;
|
|
250
|
+
}
|
|
251
|
+
isOverlayFocusAncestor(entry, component) {
|
|
252
|
+
const visited = new Set();
|
|
253
|
+
let current = entry.preFocus;
|
|
254
|
+
while (current && !visited.has(current)) {
|
|
255
|
+
visited.add(current);
|
|
256
|
+
if (current === component)
|
|
257
|
+
return true;
|
|
258
|
+
current = this.overlayStack.find((overlay) => overlay.component === current)?.preFocus ?? null;
|
|
259
|
+
}
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
retargetOverlayPreFocus(removed) {
|
|
263
|
+
for (const overlay of this.overlayStack) {
|
|
264
|
+
if (overlay !== removed && overlay.preFocus === removed.component) {
|
|
265
|
+
overlay.preFocus = removed.preFocus;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
getMountedRoots() {
|
|
270
|
+
return this.children;
|
|
271
|
+
}
|
|
272
|
+
isComponentMounted(component) {
|
|
273
|
+
return this.getMountedRoots().some((child) => this.containsComponent(child, component));
|
|
274
|
+
}
|
|
275
|
+
containsComponent(root, target) {
|
|
276
|
+
if (root === target)
|
|
277
|
+
return true;
|
|
278
|
+
if (!(root instanceof Container))
|
|
279
|
+
return false;
|
|
280
|
+
return root.children.some((child) => this.containsComponent(child, target));
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Show an overlay component with configurable positioning and sizing.
|
|
284
|
+
* Returns a handle to control the overlay's visibility.
|
|
285
|
+
*/
|
|
286
|
+
showOverlay(component, options) {
|
|
287
|
+
const entry = {
|
|
288
|
+
component,
|
|
289
|
+
...(options === undefined ? {} : { options }),
|
|
290
|
+
preFocus: this.focusedComponent,
|
|
291
|
+
hidden: false,
|
|
292
|
+
focusOrder: ++this.focusOrderCounter,
|
|
293
|
+
};
|
|
294
|
+
this.overlayStack.push(entry);
|
|
295
|
+
// Only focus if overlay is actually visible
|
|
296
|
+
if (!options?.nonCapturing && this.isOverlayVisible(entry)) {
|
|
297
|
+
this.setFocus(component);
|
|
298
|
+
}
|
|
299
|
+
this.terminal.hideCursor();
|
|
300
|
+
this.requestRender();
|
|
301
|
+
// Return handle for controlling this overlay
|
|
302
|
+
return {
|
|
303
|
+
hide: () => {
|
|
304
|
+
const index = this.overlayStack.indexOf(entry);
|
|
305
|
+
if (index !== -1) {
|
|
306
|
+
this.clearOverlayFocusRestoreFor(entry);
|
|
307
|
+
this.retargetOverlayPreFocus(entry);
|
|
308
|
+
this.overlayStack.splice(index, 1);
|
|
309
|
+
// Restore focus if this overlay had focus
|
|
310
|
+
if (this.focusedComponent === component) {
|
|
311
|
+
const topVisible = this.getTopmostVisibleOverlay();
|
|
312
|
+
this.setFocus(topVisible?.component ?? entry.preFocus);
|
|
313
|
+
}
|
|
314
|
+
if (this.overlayStack.length === 0)
|
|
315
|
+
this.terminal.hideCursor();
|
|
316
|
+
this.requestRender();
|
|
317
|
+
}
|
|
318
|
+
},
|
|
319
|
+
setHidden: (hidden) => {
|
|
320
|
+
if (entry.hidden === hidden)
|
|
321
|
+
return;
|
|
322
|
+
entry.hidden = hidden;
|
|
323
|
+
// Update focus when hiding/showing
|
|
324
|
+
if (hidden) {
|
|
325
|
+
this.clearOverlayFocusRestoreFor(entry);
|
|
326
|
+
// If this overlay had focus, move focus to next visible or preFocus
|
|
327
|
+
if (this.focusedComponent === component) {
|
|
328
|
+
const topVisible = this.getTopmostVisibleOverlay();
|
|
329
|
+
this.setFocus(topVisible?.component ?? entry.preFocus);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
// Restore focus to this overlay when showing (if it's actually visible)
|
|
334
|
+
if (!options?.nonCapturing && this.isOverlayVisible(entry)) {
|
|
335
|
+
entry.focusOrder = ++this.focusOrderCounter;
|
|
336
|
+
this.setFocus(component);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
this.requestRender();
|
|
340
|
+
},
|
|
341
|
+
isHidden: () => entry.hidden,
|
|
342
|
+
focus: () => {
|
|
343
|
+
if (!this.overlayStack.includes(entry) || !this.isOverlayVisible(entry))
|
|
344
|
+
return;
|
|
345
|
+
entry.focusOrder = ++this.focusOrderCounter;
|
|
346
|
+
this.setFocus(component);
|
|
347
|
+
this.requestRender();
|
|
348
|
+
},
|
|
349
|
+
unfocus: (unfocusOptions) => {
|
|
350
|
+
const isFocused = this.focusedComponent === component;
|
|
351
|
+
const restoreState = this.overlayFocusRestore;
|
|
352
|
+
const hasPendingRestore = restoreState.status !== "inactive" && restoreState.overlay === entry;
|
|
353
|
+
if (!isFocused && !hasPendingRestore)
|
|
354
|
+
return;
|
|
355
|
+
if (restoreState.status === "blocked" &&
|
|
356
|
+
restoreState.overlay === entry &&
|
|
357
|
+
this.focusedComponent === restoreState.blockedBy) {
|
|
358
|
+
if (unfocusOptions) {
|
|
359
|
+
this.overlayFocusRestore = {
|
|
360
|
+
status: "blocked",
|
|
361
|
+
overlay: entry,
|
|
362
|
+
blockedBy: restoreState.blockedBy,
|
|
363
|
+
resume: { status: "focus-target", target: unfocusOptions.target },
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
else {
|
|
367
|
+
this.clearOverlayFocusRestore();
|
|
368
|
+
}
|
|
369
|
+
this.requestRender();
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
this.clearOverlayFocusRestoreFor(entry);
|
|
373
|
+
if (isFocused || unfocusOptions) {
|
|
374
|
+
const topVisible = this.getTopmostVisibleOverlay();
|
|
375
|
+
const fallbackTarget = topVisible && topVisible !== entry ? topVisible.component : entry.preFocus;
|
|
376
|
+
this.setFocus(unfocusOptions ? unfocusOptions.target : fallbackTarget);
|
|
377
|
+
}
|
|
378
|
+
this.requestRender();
|
|
379
|
+
},
|
|
380
|
+
isFocused: () => this.focusedComponent === component,
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
/** Hide the topmost overlay and restore previous focus. */
|
|
384
|
+
hideOverlay() {
|
|
385
|
+
const overlay = this.overlayStack[this.overlayStack.length - 1];
|
|
386
|
+
if (!overlay)
|
|
387
|
+
return;
|
|
388
|
+
this.clearOverlayFocusRestoreFor(overlay);
|
|
389
|
+
this.retargetOverlayPreFocus(overlay);
|
|
390
|
+
this.overlayStack.pop();
|
|
391
|
+
if (this.focusedComponent === overlay.component) {
|
|
392
|
+
// Find topmost visible overlay, or fall back to preFocus
|
|
393
|
+
const topVisible = this.getTopmostVisibleOverlay();
|
|
394
|
+
this.setFocus(topVisible?.component ?? overlay.preFocus);
|
|
395
|
+
}
|
|
396
|
+
if (this.overlayStack.length === 0)
|
|
397
|
+
this.terminal.hideCursor();
|
|
398
|
+
this.requestRender();
|
|
399
|
+
}
|
|
400
|
+
/** Check if there are any visible overlays */
|
|
401
|
+
hasOverlay() {
|
|
402
|
+
return this.overlayStack.some((o) => this.isOverlayVisible(o));
|
|
403
|
+
}
|
|
404
|
+
/** Check if the focused component is a visible overlay */
|
|
405
|
+
isOverlayFocused() {
|
|
406
|
+
return this.overlayStack.some((entry) => entry.component === this.focusedComponent && this.isOverlayVisible(entry));
|
|
407
|
+
}
|
|
408
|
+
/** Check if an overlay entry is currently visible */
|
|
409
|
+
isOverlayVisible(entry) {
|
|
410
|
+
if (entry.hidden)
|
|
411
|
+
return false;
|
|
412
|
+
if (entry.options?.visible) {
|
|
413
|
+
return entry.options.visible(this.terminal.columns, this.terminal.rows);
|
|
414
|
+
}
|
|
415
|
+
return true;
|
|
416
|
+
}
|
|
417
|
+
/** Find the visual-frontmost visible capturing overlay, if any */
|
|
418
|
+
getTopmostVisibleOverlay() {
|
|
419
|
+
let topmost;
|
|
420
|
+
for (const overlay of this.overlayStack) {
|
|
421
|
+
if (overlay.options?.nonCapturing || !this.isOverlayVisible(overlay))
|
|
422
|
+
continue;
|
|
423
|
+
if (!topmost || overlay.focusOrder > topmost.focusOrder) {
|
|
424
|
+
topmost = overlay;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return topmost;
|
|
428
|
+
}
|
|
429
|
+
invalidate() {
|
|
430
|
+
for (const root of this.getMountedRoots())
|
|
431
|
+
root.invalidate();
|
|
432
|
+
for (const overlay of this.overlayStack)
|
|
433
|
+
overlay.component.invalidate();
|
|
434
|
+
}
|
|
435
|
+
start() {
|
|
436
|
+
this.stopped = false;
|
|
437
|
+
this.beforeTerminalStart();
|
|
438
|
+
this.terminal.start((data) => this.handleTerminalInput(data), () => this.requestRender());
|
|
439
|
+
this.afterTerminalStart();
|
|
440
|
+
this.terminal.hideCursor();
|
|
441
|
+
if (this.terminalColorSchemeNotificationsEnabled) {
|
|
442
|
+
this.terminal.write("\x1b[?2031h");
|
|
443
|
+
}
|
|
444
|
+
this.queryCellSize();
|
|
445
|
+
this.requestRender();
|
|
446
|
+
}
|
|
447
|
+
addInputListener(listener) {
|
|
448
|
+
this.inputListeners.add(listener);
|
|
449
|
+
return () => {
|
|
450
|
+
this.inputListeners.delete(listener);
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
removeInputListener(listener) {
|
|
454
|
+
this.inputListeners.delete(listener);
|
|
455
|
+
}
|
|
456
|
+
onTerminalColorSchemeChange(listener) {
|
|
457
|
+
this.terminalColorSchemeListeners.add(listener);
|
|
458
|
+
return () => {
|
|
459
|
+
this.terminalColorSchemeListeners.delete(listener);
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
setTerminalColorSchemeNotifications(enabled) {
|
|
463
|
+
if (this.terminalColorSchemeNotificationsEnabled === enabled) {
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
this.terminalColorSchemeNotificationsEnabled = enabled;
|
|
467
|
+
if (!this.stopped) {
|
|
468
|
+
this.terminal.write(enabled ? "\x1b[?2031h" : "\x1b[?2031l");
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
queryCellSize() {
|
|
472
|
+
// Only query if terminal supports images (cell size is only used for image rendering)
|
|
473
|
+
if (!getCapabilities().images) {
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
// Query terminal for cell size in pixels: CSI 16 t
|
|
477
|
+
// Response format: CSI 6 ; height ; width t
|
|
478
|
+
this.terminal.write("\x1b[16t");
|
|
479
|
+
}
|
|
480
|
+
stop(options = {}) {
|
|
481
|
+
this.stopped = true;
|
|
482
|
+
this.cancelRenderTimer();
|
|
483
|
+
if (this.terminalColorSchemeNotificationsEnabled) {
|
|
484
|
+
this.terminal.write("\x1b[?2031l");
|
|
485
|
+
}
|
|
486
|
+
this.beforeTerminalStop(options);
|
|
487
|
+
this.terminal.showCursor();
|
|
488
|
+
this.terminal.stop();
|
|
489
|
+
this.afterTerminalStop(options);
|
|
490
|
+
}
|
|
491
|
+
renderNow(force = false) {
|
|
492
|
+
if (force)
|
|
493
|
+
this.resetRenderState();
|
|
494
|
+
this.renderRequested = false;
|
|
495
|
+
this.cancelRenderTimer();
|
|
496
|
+
this.lastRenderAt = performance.now();
|
|
497
|
+
this.doRender();
|
|
498
|
+
}
|
|
499
|
+
requestRender(force = false) {
|
|
500
|
+
if (force) {
|
|
501
|
+
this.resetRenderState();
|
|
502
|
+
this.requestImmediateRender();
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
if (this.renderRequested)
|
|
506
|
+
return;
|
|
507
|
+
this.renderRequested = true;
|
|
508
|
+
process.nextTick(() => this.scheduleRender());
|
|
509
|
+
}
|
|
510
|
+
requestImmediateRender() {
|
|
511
|
+
this.cancelRenderTimer();
|
|
512
|
+
this.renderRequested = true;
|
|
513
|
+
if (this.immediateRenderScheduled)
|
|
514
|
+
return;
|
|
515
|
+
this.immediateRenderScheduled = true;
|
|
516
|
+
process.nextTick(() => {
|
|
517
|
+
this.immediateRenderScheduled = false;
|
|
518
|
+
if (this.stopped || !this.renderRequested)
|
|
519
|
+
return;
|
|
520
|
+
// A previously queued scheduleRender() can create a timer before this
|
|
521
|
+
// callback runs. User input must preempt that throttled frame.
|
|
522
|
+
this.cancelRenderTimer();
|
|
523
|
+
this.renderRequested = false;
|
|
524
|
+
this.lastRenderAt = performance.now();
|
|
525
|
+
this.doRender();
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
cancelRenderTimer() {
|
|
529
|
+
if (!this.renderTimer)
|
|
530
|
+
return;
|
|
531
|
+
clearTimeout(this.renderTimer);
|
|
532
|
+
this.renderTimer = undefined;
|
|
533
|
+
}
|
|
534
|
+
scheduleRender() {
|
|
535
|
+
if (this.stopped || this.renderTimer || !this.renderRequested) {
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
const elapsed = performance.now() - this.lastRenderAt;
|
|
539
|
+
const delay = Math.max(0, TuiBase.MIN_RENDER_INTERVAL_MS - elapsed);
|
|
540
|
+
this.renderTimer = setTimeout(() => {
|
|
541
|
+
this.renderTimer = undefined;
|
|
542
|
+
if (this.stopped || !this.renderRequested) {
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
this.renderRequested = false;
|
|
546
|
+
this.lastRenderAt = performance.now();
|
|
547
|
+
this.doRender();
|
|
548
|
+
if (this.renderRequested) {
|
|
549
|
+
this.scheduleRender();
|
|
550
|
+
}
|
|
551
|
+
}, delay);
|
|
552
|
+
}
|
|
553
|
+
handleTerminalInput(data) {
|
|
554
|
+
if (this.consumeOsc11BackgroundResponse(data)) {
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
if (this.consumeTerminalColorSchemeReport(data)) {
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
if (this.inputListeners.size > 0) {
|
|
561
|
+
let current = data;
|
|
562
|
+
for (const listener of this.inputListeners) {
|
|
563
|
+
const result = listener(current);
|
|
564
|
+
if (result?.consume) {
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
if (result?.data !== undefined) {
|
|
568
|
+
current = result.data;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
if (current.length === 0) {
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
data = current;
|
|
575
|
+
}
|
|
576
|
+
// Consume terminal cell size responses without blocking unrelated input.
|
|
577
|
+
if (this.consumeCellSizeResponse(data)) {
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
// Global debug key handler (Shift+Ctrl+D)
|
|
581
|
+
if (matchesKey(data, "shift+ctrl+d") && this.onDebug) {
|
|
582
|
+
this.onDebug();
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
// If focused component is an overlay, verify it's still visible
|
|
586
|
+
// (visibility can change due to terminal resize or visible() callback)
|
|
587
|
+
const focusedOverlay = this.overlayStack.find((o) => o.component === this.focusedComponent);
|
|
588
|
+
if (focusedOverlay && !this.isOverlayVisible(focusedOverlay)) {
|
|
589
|
+
// Focused overlay is no longer visible, redirect to topmost visible overlay
|
|
590
|
+
const topVisible = this.getTopmostVisibleOverlay();
|
|
591
|
+
if (topVisible) {
|
|
592
|
+
this.setFocus(topVisible.component);
|
|
593
|
+
}
|
|
594
|
+
else {
|
|
595
|
+
this.setFocusInternal({ component: focusedOverlay.preFocus, overlayFocusRestore: "preserve" });
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
const focusIsOverlay = this.overlayStack.some((o) => o.component === this.focusedComponent);
|
|
599
|
+
if (!focusIsOverlay) {
|
|
600
|
+
const restoreState = this.getVisibleOverlayFocusRestore();
|
|
601
|
+
if (restoreState.status === "eligible") {
|
|
602
|
+
this.setFocus(restoreState.overlay.component);
|
|
603
|
+
}
|
|
604
|
+
else if (restoreState.status === "blocked" && restoreState.blockedBy !== this.focusedComponent) {
|
|
605
|
+
if (restoreState.resume.status === "restore-overlay") {
|
|
606
|
+
this.setFocus(restoreState.overlay.component);
|
|
607
|
+
}
|
|
608
|
+
else {
|
|
609
|
+
this.clearOverlayFocusRestore();
|
|
610
|
+
this.setFocus(restoreState.resume.target);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
// Pass input to focused component (including Ctrl+C)
|
|
615
|
+
// The focused component can decide how to handle Ctrl+C
|
|
616
|
+
if (this.focusedComponent?.handleInput) {
|
|
617
|
+
// Filter out key release events unless component opts in
|
|
618
|
+
if (isKeyRelease(data) && !this.focusedComponent.wantsKeyRelease) {
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
this.focusedComponent.handleInput(data);
|
|
622
|
+
// Keyboard input is latency-sensitive. Avoid the throttled timer path,
|
|
623
|
+
// where even setTimeout(0) can take a full 16 ms tick on Windows.
|
|
624
|
+
this.requestImmediateRender();
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
consumeOsc11BackgroundResponse(data) {
|
|
628
|
+
if (this.pendingOsc11BackgroundReplies <= 0) {
|
|
629
|
+
return false;
|
|
630
|
+
}
|
|
631
|
+
if (!isOsc11BackgroundColorResponse(data)) {
|
|
632
|
+
return false;
|
|
633
|
+
}
|
|
634
|
+
const rgb = parseOsc11BackgroundColor(data);
|
|
635
|
+
this.pendingOsc11BackgroundReplies -= 1;
|
|
636
|
+
const query = this.pendingOsc11BackgroundQueries.shift();
|
|
637
|
+
if (query && !query.settled) {
|
|
638
|
+
query.settled = true;
|
|
639
|
+
if (query.timer) {
|
|
640
|
+
clearTimeout(query.timer);
|
|
641
|
+
query.timer = undefined;
|
|
642
|
+
}
|
|
643
|
+
query.resolve?.(rgb);
|
|
644
|
+
query.resolve = undefined;
|
|
645
|
+
}
|
|
646
|
+
return true;
|
|
647
|
+
}
|
|
648
|
+
consumeTerminalColorSchemeReport(data) {
|
|
649
|
+
const scheme = parseTerminalColorSchemeReport(data);
|
|
650
|
+
if (!scheme) {
|
|
651
|
+
return false;
|
|
652
|
+
}
|
|
653
|
+
for (const listener of this.terminalColorSchemeListeners) {
|
|
654
|
+
listener(scheme);
|
|
655
|
+
}
|
|
656
|
+
return true;
|
|
657
|
+
}
|
|
658
|
+
consumeCellSizeResponse(data) {
|
|
659
|
+
// Response format: ESC [ 6 ; height ; width t
|
|
660
|
+
const match = data.match(/^\x1b\[6;(\d+);(\d+)t$/);
|
|
661
|
+
if (!match) {
|
|
662
|
+
return false;
|
|
663
|
+
}
|
|
664
|
+
const heightPx = parseInt(match[1], 10);
|
|
665
|
+
const widthPx = parseInt(match[2], 10);
|
|
666
|
+
if (heightPx <= 0 || widthPx <= 0) {
|
|
667
|
+
return true;
|
|
668
|
+
}
|
|
669
|
+
setCellDimensions({ widthPx, heightPx });
|
|
670
|
+
// Invalidate all components so images re-render with correct dimensions.
|
|
671
|
+
this.invalidate();
|
|
672
|
+
this.requestRender();
|
|
673
|
+
return true;
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
* Resolve overlay layout from options.
|
|
677
|
+
* Returns { width, row, col, maxHeight } for rendering.
|
|
678
|
+
*/
|
|
679
|
+
resolveOverlayLayout(options, overlayHeight, termWidth, termHeight) {
|
|
680
|
+
const opt = options ?? {};
|
|
681
|
+
// Parse margin (clamp to non-negative)
|
|
682
|
+
const margin = typeof opt.margin === "number"
|
|
683
|
+
? { top: opt.margin, right: opt.margin, bottom: opt.margin, left: opt.margin }
|
|
684
|
+
: (opt.margin ?? {});
|
|
685
|
+
const marginTop = Math.max(0, margin.top ?? 0);
|
|
686
|
+
const marginRight = Math.max(0, margin.right ?? 0);
|
|
687
|
+
const marginBottom = Math.max(0, margin.bottom ?? 0);
|
|
688
|
+
const marginLeft = Math.max(0, margin.left ?? 0);
|
|
689
|
+
// Available space after margins
|
|
690
|
+
const availWidth = Math.max(1, termWidth - marginLeft - marginRight);
|
|
691
|
+
const availHeight = Math.max(1, termHeight - marginTop - marginBottom);
|
|
692
|
+
// === Resolve width ===
|
|
693
|
+
let width = parseSizeValue(opt.width, termWidth) ?? Math.min(80, availWidth);
|
|
694
|
+
// Apply minWidth
|
|
695
|
+
if (opt.minWidth !== undefined) {
|
|
696
|
+
width = Math.max(width, opt.minWidth);
|
|
697
|
+
}
|
|
698
|
+
// Clamp to available space
|
|
699
|
+
width = Math.max(1, Math.min(width, availWidth));
|
|
700
|
+
// === Resolve maxHeight ===
|
|
701
|
+
let maxHeight = parseSizeValue(opt.maxHeight, termHeight);
|
|
702
|
+
// Clamp to available space
|
|
703
|
+
if (maxHeight !== undefined) {
|
|
704
|
+
maxHeight = Math.max(1, Math.min(maxHeight, availHeight));
|
|
705
|
+
}
|
|
706
|
+
// Effective overlay height (may be clamped by maxHeight)
|
|
707
|
+
const effectiveHeight = maxHeight !== undefined ? Math.min(overlayHeight, maxHeight) : overlayHeight;
|
|
708
|
+
// === Resolve position ===
|
|
709
|
+
let row;
|
|
710
|
+
let col;
|
|
711
|
+
if (opt.row !== undefined) {
|
|
712
|
+
if (typeof opt.row === "string") {
|
|
713
|
+
// Percentage: 0% = top, 100% = bottom (overlay stays within bounds)
|
|
714
|
+
const match = opt.row.match(/^(\d+(?:\.\d+)?)%$/);
|
|
715
|
+
if (match) {
|
|
716
|
+
const maxRow = Math.max(0, availHeight - effectiveHeight);
|
|
717
|
+
const percent = parseFloat(match[1]) / 100;
|
|
718
|
+
row = marginTop + Math.floor(maxRow * percent);
|
|
719
|
+
}
|
|
720
|
+
else {
|
|
721
|
+
// Invalid format, fall back to center
|
|
722
|
+
row = this.resolveAnchorRow("center", effectiveHeight, availHeight, marginTop);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
else {
|
|
726
|
+
// Absolute row position
|
|
727
|
+
row = opt.row;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
else {
|
|
731
|
+
// Anchor-based (default: center)
|
|
732
|
+
const anchor = opt.anchor ?? "center";
|
|
733
|
+
row = this.resolveAnchorRow(anchor, effectiveHeight, availHeight, marginTop);
|
|
734
|
+
}
|
|
735
|
+
if (opt.col !== undefined) {
|
|
736
|
+
if (typeof opt.col === "string") {
|
|
737
|
+
// Percentage: 0% = left, 100% = right (overlay stays within bounds)
|
|
738
|
+
const match = opt.col.match(/^(\d+(?:\.\d+)?)%$/);
|
|
739
|
+
if (match) {
|
|
740
|
+
const maxCol = Math.max(0, availWidth - width);
|
|
741
|
+
const percent = parseFloat(match[1]) / 100;
|
|
742
|
+
col = marginLeft + Math.floor(maxCol * percent);
|
|
743
|
+
}
|
|
744
|
+
else {
|
|
745
|
+
// Invalid format, fall back to center
|
|
746
|
+
col = this.resolveAnchorCol("center", width, availWidth, marginLeft);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
else {
|
|
750
|
+
// Absolute column position
|
|
751
|
+
col = opt.col;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
else {
|
|
755
|
+
// Anchor-based (default: center)
|
|
756
|
+
const anchor = opt.anchor ?? "center";
|
|
757
|
+
col = this.resolveAnchorCol(anchor, width, availWidth, marginLeft);
|
|
758
|
+
}
|
|
759
|
+
// Apply offsets
|
|
760
|
+
if (opt.offsetY !== undefined)
|
|
761
|
+
row += opt.offsetY;
|
|
762
|
+
if (opt.offsetX !== undefined)
|
|
763
|
+
col += opt.offsetX;
|
|
764
|
+
// Clamp to terminal bounds (respecting margins)
|
|
765
|
+
row = Math.max(marginTop, Math.min(row, termHeight - marginBottom - effectiveHeight));
|
|
766
|
+
col = Math.max(marginLeft, Math.min(col, termWidth - marginRight - width));
|
|
767
|
+
return { width, row, col, maxHeight };
|
|
768
|
+
}
|
|
769
|
+
resolveAnchorRow(anchor, height, availHeight, marginTop) {
|
|
770
|
+
switch (anchor) {
|
|
771
|
+
case "top-left":
|
|
772
|
+
case "top-center":
|
|
773
|
+
case "top-right":
|
|
774
|
+
return marginTop;
|
|
775
|
+
case "bottom-left":
|
|
776
|
+
case "bottom-center":
|
|
777
|
+
case "bottom-right":
|
|
778
|
+
return marginTop + availHeight - height;
|
|
779
|
+
case "left-center":
|
|
780
|
+
case "center":
|
|
781
|
+
case "right-center":
|
|
782
|
+
return marginTop + Math.floor((availHeight - height) / 2);
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
resolveAnchorCol(anchor, width, availWidth, marginLeft) {
|
|
786
|
+
switch (anchor) {
|
|
787
|
+
case "top-left":
|
|
788
|
+
case "left-center":
|
|
789
|
+
case "bottom-left":
|
|
790
|
+
return marginLeft;
|
|
791
|
+
case "top-right":
|
|
792
|
+
case "right-center":
|
|
793
|
+
case "bottom-right":
|
|
794
|
+
return marginLeft + availWidth - width;
|
|
795
|
+
case "top-center":
|
|
796
|
+
case "center":
|
|
797
|
+
case "bottom-center":
|
|
798
|
+
return marginLeft + Math.floor((availWidth - width) / 2);
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
/** Composite all overlays into content lines (sorted by focusOrder, higher = on top). */
|
|
802
|
+
compositeOverlays(lines, termWidth, termHeight) {
|
|
803
|
+
if (this.overlayStack.length === 0)
|
|
804
|
+
return lines;
|
|
805
|
+
const result = [...lines];
|
|
806
|
+
// Pre-render all visible overlays and calculate positions
|
|
807
|
+
const rendered = [];
|
|
808
|
+
let minLinesNeeded = result.length;
|
|
809
|
+
const visibleEntries = this.overlayStack.filter((e) => this.isOverlayVisible(e));
|
|
810
|
+
visibleEntries.sort((a, b) => a.focusOrder - b.focusOrder);
|
|
811
|
+
for (const entry of visibleEntries) {
|
|
812
|
+
const { component, options } = entry;
|
|
813
|
+
// Get layout with height=0 first to determine width and maxHeight
|
|
814
|
+
// (width and maxHeight don't depend on overlay height)
|
|
815
|
+
const { width, maxHeight } = this.resolveOverlayLayout(options, 0, termWidth, termHeight);
|
|
816
|
+
// Render component at calculated width
|
|
817
|
+
let overlayLines = component.render(width);
|
|
818
|
+
// Apply maxHeight if specified
|
|
819
|
+
if (maxHeight !== undefined && overlayLines.length > maxHeight) {
|
|
820
|
+
overlayLines = overlayLines.slice(0, maxHeight);
|
|
821
|
+
}
|
|
822
|
+
// Get final row/col with actual overlay height
|
|
823
|
+
const { row, col } = this.resolveOverlayLayout(options, overlayLines.length, termWidth, termHeight);
|
|
824
|
+
rendered.push({ overlayLines, row, col, w: width });
|
|
825
|
+
minLinesNeeded = Math.max(minLinesNeeded, row + overlayLines.length);
|
|
826
|
+
}
|
|
827
|
+
// Pad to at least terminal height so overlays have screen-relative positions.
|
|
828
|
+
// Excludes maxLinesRendered: the historical high-water mark caused self-reinforcing
|
|
829
|
+
// inflation that pushed content into scrollback on terminal widen.
|
|
830
|
+
const workingHeight = Math.max(result.length, termHeight, minLinesNeeded);
|
|
831
|
+
// Extend result with empty lines if content is too short for overlay placement or working area
|
|
832
|
+
while (result.length < workingHeight) {
|
|
833
|
+
result.push("");
|
|
834
|
+
}
|
|
835
|
+
const viewportStart = Math.max(0, workingHeight - termHeight);
|
|
836
|
+
// Composite each overlay
|
|
837
|
+
for (const { overlayLines, row, col, w } of rendered) {
|
|
838
|
+
for (let i = 0; i < overlayLines.length; i++) {
|
|
839
|
+
const idx = viewportStart + row + i;
|
|
840
|
+
if (idx >= 0 && idx < result.length) {
|
|
841
|
+
// Defensive: truncate overlay line to declared width before compositing
|
|
842
|
+
// (components should already respect width, but this ensures it)
|
|
843
|
+
const truncatedOverlayLine = visibleWidth(overlayLines[i]) > w ? sliceByColumn(overlayLines[i], 0, w, true) : overlayLines[i];
|
|
844
|
+
result[idx] = this.compositeLineAt(result[idx], truncatedOverlayLine, col, w, termWidth);
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
return result;
|
|
849
|
+
}
|
|
850
|
+
applyLineResets(lines) {
|
|
851
|
+
const reset = SEGMENT_RESET;
|
|
852
|
+
for (let i = 0; i < lines.length; i++) {
|
|
853
|
+
const line = lines[i];
|
|
854
|
+
if (!isImageLine(line)) {
|
|
855
|
+
lines[i] = normalizeTerminalOutput(line) + reset;
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
return lines;
|
|
859
|
+
}
|
|
860
|
+
compositeLineAt(baseLine, overlayLine, startCol, overlayWidth, totalWidth) {
|
|
861
|
+
return compositeTuiLine(baseLine, overlayLine, startCol, overlayWidth, totalWidth);
|
|
862
|
+
}
|
|
863
|
+
/**
|
|
864
|
+
* Find and extract cursor position from rendered lines.
|
|
865
|
+
* Searches for CURSOR_MARKER, calculates its position, and strips it from the output.
|
|
866
|
+
* Only scans the bottom terminal height lines (visible viewport).
|
|
867
|
+
* @param lines - Rendered lines to search
|
|
868
|
+
* @param height - Terminal height (visible viewport size)
|
|
869
|
+
* @returns Cursor position { row, col } or null if no marker found
|
|
870
|
+
*/
|
|
871
|
+
extractCursorPosition(lines, height) {
|
|
872
|
+
// Only scan the bottom `height` lines (visible viewport)
|
|
873
|
+
const viewportTop = Math.max(0, lines.length - height);
|
|
874
|
+
for (let row = lines.length - 1; row >= viewportTop; row--) {
|
|
875
|
+
const line = lines[row];
|
|
876
|
+
const markerIndex = line.indexOf(CURSOR_MARKER);
|
|
877
|
+
if (markerIndex !== -1) {
|
|
878
|
+
// Calculate visual column (width of text before marker)
|
|
879
|
+
const beforeMarker = line.slice(0, markerIndex);
|
|
880
|
+
const col = visibleWidth(beforeMarker);
|
|
881
|
+
// Strip marker from the line
|
|
882
|
+
lines[row] = line.slice(0, markerIndex) + line.slice(markerIndex + CURSOR_MARKER.length);
|
|
883
|
+
return { row, col };
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
return null;
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* Query the terminal's default background color with OSC 11 (`ESC ] 11 ; ? BEL`).
|
|
890
|
+
* @param timeoutMs Query timeout in milliseconds.
|
|
891
|
+
* @returns Promise containing the parsed RGB color, or undefined if it times out or fails to parse.
|
|
892
|
+
*/
|
|
893
|
+
queryTerminalBackgroundColor({ timeoutMs }) {
|
|
894
|
+
return new Promise((resolve) => {
|
|
895
|
+
const query = {
|
|
896
|
+
settled: false,
|
|
897
|
+
resolve,
|
|
898
|
+
timer: undefined,
|
|
899
|
+
};
|
|
900
|
+
query.timer = setTimeout(() => {
|
|
901
|
+
if (query.settled) {
|
|
902
|
+
return;
|
|
903
|
+
}
|
|
904
|
+
query.settled = true;
|
|
905
|
+
query.timer = undefined;
|
|
906
|
+
query.resolve?.(undefined);
|
|
907
|
+
query.resolve = undefined;
|
|
908
|
+
}, timeoutMs);
|
|
909
|
+
this.pendingOsc11BackgroundQueries.push(query);
|
|
910
|
+
this.pendingOsc11BackgroundReplies += 1;
|
|
911
|
+
this.terminal.write("\x1b]11;?\x07");
|
|
912
|
+
});
|
|
913
|
+
}
|
|
914
|
+
/**
|
|
915
|
+
* Query the terminal's color-scheme preference with DSR (`CSI ? 996 n`).
|
|
916
|
+
* Terminals that support the color palette notification protocol reply with
|
|
917
|
+
* `CSI ? 997 ; 1 n` for dark or `CSI ? 997 ; 2 n` for light.
|
|
918
|
+
*/
|
|
919
|
+
queryTerminalColorScheme({ timeoutMs }) {
|
|
920
|
+
return new Promise((resolve) => {
|
|
921
|
+
let settled = false;
|
|
922
|
+
let timer;
|
|
923
|
+
let unsubscribe = () => { };
|
|
924
|
+
const settle = (scheme) => {
|
|
925
|
+
if (settled)
|
|
926
|
+
return;
|
|
927
|
+
settled = true;
|
|
928
|
+
if (timer) {
|
|
929
|
+
clearTimeout(timer);
|
|
930
|
+
timer = undefined;
|
|
931
|
+
}
|
|
932
|
+
unsubscribe();
|
|
933
|
+
resolve(scheme);
|
|
934
|
+
};
|
|
935
|
+
unsubscribe = this.onTerminalColorSchemeChange(settle);
|
|
936
|
+
timer = setTimeout(() => settle(undefined), timeoutMs);
|
|
937
|
+
this.terminal.write("\x1b[?996n");
|
|
938
|
+
});
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
//# sourceMappingURL=tui.js.map
|