@draht/tui 2026.7.30 → 2026.10.4-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/alt-screen-search.d.ts +46 -0
- package/dist/alt-screen-search.d.ts.map +1 -0
- package/dist/alt-screen-search.js +282 -0
- package/dist/alt-screen-search.js.map +1 -0
- package/dist/autocomplete.d.ts +1 -0
- package/dist/autocomplete.d.ts.map +1 -1
- package/dist/autocomplete.js +81 -18
- package/dist/autocomplete.js.map +1 -1
- package/dist/colors.d.ts +73 -0
- package/dist/colors.d.ts.map +1 -0
- package/dist/colors.js +273 -0
- package/dist/colors.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 +3 -1
- package/dist/components/box.d.ts.map +1 -1
- package/dist/components/box.js +43 -9
- package/dist/components/box.js.map +1 -1
- package/dist/components/cancellable-loader.js +3 -4
- package/dist/components/cancellable-loader.js.map +1 -1
- package/dist/components/editor.d.ts +6 -1
- package/dist/components/editor.d.ts.map +1 -1
- package/dist/components/editor.js +175 -70
- package/dist/components/editor.js.map +1 -1
- 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.js +8 -0
- package/dist/components/image.js.map +1 -1
- package/dist/components/input.d.ts +12 -1
- package/dist/components/input.d.ts.map +1 -1
- package/dist/components/input.js +57 -18
- package/dist/components/input.js.map +1 -1
- package/dist/components/loader.d.ts +2 -0
- package/dist/components/loader.d.ts.map +1 -1
- package/dist/components/loader.js +19 -10
- package/dist/components/loader.js.map +1 -1
- package/dist/components/markdown.d.ts +5 -0
- package/dist/components/markdown.d.ts.map +1 -1
- package/dist/components/markdown.js +157 -9
- package/dist/components/markdown.js.map +1 -1
- package/dist/components/mouse-region.d.ts +12 -0
- package/dist/components/mouse-region.d.ts.map +1 -0
- package/dist/components/mouse-region.js +21 -0
- package/dist/components/mouse-region.js.map +1 -0
- package/dist/components/scroll-view.d.ts +59 -0
- package/dist/components/scroll-view.d.ts.map +1 -0
- package/dist/components/scroll-view.js +195 -0
- package/dist/components/scroll-view.js.map +1 -0
- package/dist/components/select-list.d.ts +12 -1
- package/dist/components/select-list.d.ts.map +1 -1
- package/dist/components/select-list.js +72 -10
- package/dist/components/select-list.js.map +1 -1
- package/dist/components/settings-list.d.ts +13 -3
- package/dist/components/settings-list.d.ts.map +1 -1
- package/dist/components/settings-list.js +94 -19
- package/dist/components/settings-list.js.map +1 -1
- package/dist/components/spacer.js +1 -0
- package/dist/components/spacer.js.map +1 -1
- package/dist/components/stack.d.ts +32 -0
- package/dist/components/stack.d.ts.map +1 -0
- package/dist/components/stack.js +111 -0
- package/dist/components/stack.js.map +1 -0
- package/dist/components/text.d.ts.map +1 -1
- package/dist/components/text.js +13 -4
- package/dist/components/text.js.map +1 -1
- package/dist/components/truncated-text.js +3 -0
- package/dist/components/truncated-text.js.map +1 -1
- 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 +26 -0
- package/dist/components/v-stack.js.map +1 -0
- package/dist/fuzzy.d.ts.map +1 -1
- package/dist/fuzzy.js +23 -22
- package/dist/fuzzy.js.map +1 -1
- package/dist/index.d.ts +16 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -4
- package/dist/index.js.map +1 -1
- package/dist/keybindings.d.ts +84 -4
- package/dist/keybindings.d.ts.map +1 -1
- package/dist/keybindings.js +67 -6
- package/dist/keybindings.js.map +1 -1
- package/dist/kill-ring.js +1 -3
- package/dist/kill-ring.js.map +1 -1
- package/dist/latex.d.ts +10 -0
- package/dist/latex.d.ts.map +1 -0
- package/dist/latex.js +1378 -0
- package/dist/latex.js.map +1 -0
- package/dist/layout-node.d.ts +40 -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 +42 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +344 -0
- package/dist/layout.js.map +1 -0
- package/dist/native-modifiers.d.ts +2 -1
- package/dist/native-modifiers.d.ts.map +1 -1
- package/dist/native-modifiers.js +3 -43
- package/dist/native-modifiers.js.map +1 -1
- package/dist/native-module-path.d.ts +7 -0
- package/dist/native-module-path.d.ts.map +1 -0
- package/dist/native-module-path.js +19 -0
- package/dist/native-module-path.js.map +1 -0
- package/dist/native-platform.d.ts +20 -0
- package/dist/native-platform.d.ts.map +1 -0
- package/dist/native-platform.js +42 -0
- package/dist/native-platform.js.map +1 -0
- package/dist/oklab.d.ts +43 -0
- package/dist/oklab.d.ts.map +1 -0
- package/dist/oklab.js +210 -0
- package/dist/oklab.js.map +1 -0
- package/dist/stdin-buffer.d.ts +8 -2
- package/dist/stdin-buffer.d.ts.map +1 -1
- package/dist/stdin-buffer.js +13 -6
- package/dist/stdin-buffer.js.map +1 -1
- package/dist/terminal-colors.d.ts +19 -2
- package/dist/terminal-colors.d.ts.map +1 -1
- package/dist/terminal-colors.js +13 -8
- package/dist/terminal-colors.js.map +1 -1
- package/dist/terminal-image.d.ts +26 -1
- package/dist/terminal-image.d.ts.map +1 -1
- package/dist/terminal-image.js +199 -18
- package/dist/terminal-image.js.map +1 -1
- package/dist/terminal.d.ts +16 -0
- package/dist/terminal.d.ts.map +1 -1
- package/dist/terminal.js +88 -68
- package/dist/terminal.js.map +1 -1
- package/dist/tui-alt-screen.d.ts +164 -0
- package/dist/tui-alt-screen.d.ts.map +1 -0
- package/dist/tui-alt-screen.js +1512 -0
- package/dist/tui-alt-screen.js.map +1 -0
- package/dist/tui-main-screen.d.ts +39 -0
- package/dist/tui-main-screen.d.ts.map +1 -0
- package/dist/tui-main-screen.js +594 -0
- package/dist/tui-main-screen.js.map +1 -0
- package/dist/tui.d.ts +178 -60
- package/dist/tui.d.ts.map +1 -1
- package/dist/tui.js +322 -658
- package/dist/tui.js.map +1 -1
- package/dist/undo-stack.js +1 -3
- package/dist/undo-stack.js.map +1 -1
- package/dist/utils.d.ts +16 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +245 -75
- package/dist/utils.js.map +1 -1
- package/dist/wheel-scroll.d.ts +23 -0
- package/dist/wheel-scroll.d.ts.map +1 -0
- package/dist/wheel-scroll.js +73 -0
- package/dist/wheel-scroll.js.map +1 -0
- package/native/clipboard.h +208 -0
- package/native/darwin/build.sh +69 -0
- package/native/darwin/prebuilds/darwin-arm64/darwin-platform.node +0 -0
- package/native/darwin/prebuilds/darwin-x64/darwin-platform.node +0 -0
- package/native/darwin/src/darwin-platform.m +132 -0
- package/native/linux/build.sh +65 -0
- package/native/linux/prebuilds/linux-arm64/linux-platform-x11.node +0 -0
- package/native/linux/prebuilds/linux-x64/linux-platform-x11.node +0 -0
- package/native/linux/src/clipboard-worker.h +70 -0
- package/native/linux/src/linux-platform-x11.c +274 -0
- package/native/napi.h +91 -0
- package/native/win32/build.mjs +232 -0
- package/native/win32/prebuilds/win32-arm64/win32-platform.node +0 -0
- package/native/win32/prebuilds/win32-x64/win32-platform.node +0 -0
- package/native/win32/src/win32-platform.c +241 -0
- package/package.json +16 -2
package/dist/tui.js
CHANGED
|
@@ -1,48 +1,61 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Minimal TUI implementation with differential rendering
|
|
3
3
|
*/
|
|
4
|
-
import * as fs from "node:fs";
|
|
5
|
-
import * as os from "node:os";
|
|
6
|
-
import * as path from "node:path";
|
|
7
4
|
import { performance } from "node:perf_hooks";
|
|
8
5
|
import { isKeyRelease, matchesKey } from "./keys.js";
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
6
|
+
import { parseOscColorResponse, parseTerminalColorSchemeReport, } from "./terminal-colors.js";
|
|
7
|
+
import { getCapabilities, isImageLine, setCellDimensions } from "./terminal-image.js";
|
|
11
8
|
import { extractSegments, normalizeTerminalOutput, sliceByColumn, sliceWithWidth, visibleWidth } from "./utils.js";
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
if (paramsEnd === -1)
|
|
9
|
+
/**
|
|
10
|
+
* Dispatch an event to a component and retain the exact target and coordinate
|
|
11
|
+
* transform. Containers use this when forwarding events to nested children.
|
|
12
|
+
*/
|
|
13
|
+
export function dispatchMouseEvent(component, event) {
|
|
14
|
+
const result = component.handleMouse?.(event);
|
|
15
|
+
if (!result)
|
|
20
16
|
return undefined;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
continue;
|
|
28
|
-
const numberValue = Number(value);
|
|
29
|
-
if (!Number.isInteger(numberValue) || numberValue <= 0 || numberValue > 0xffffffff)
|
|
30
|
-
continue;
|
|
31
|
-
if (key === "i") {
|
|
32
|
-
ids.push(numberValue);
|
|
33
|
-
}
|
|
34
|
-
else if (key === "r") {
|
|
35
|
-
rows = numberValue;
|
|
36
|
-
}
|
|
17
|
+
if ("target" in result) {
|
|
18
|
+
// The component forwarded the event to a child it hosts. Like a delegating container, it routes
|
|
19
|
+
// keys to that child itself, so it keeps keyboard focus. Focusing the child directly would leave
|
|
20
|
+
// focus on a detached component once the host removes it, e.g. a closed settings submenu.
|
|
21
|
+
const forwarded = result;
|
|
22
|
+
return forwarded.focus && component.handleInput ? { ...forwarded, focusTarget: component } : forwarded;
|
|
37
23
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
24
|
+
if (!result.handled && !result.capture && !result.focus)
|
|
25
|
+
return undefined;
|
|
26
|
+
return {
|
|
27
|
+
...result,
|
|
28
|
+
handled: true,
|
|
29
|
+
...(result.focus ? { focusTarget: component } : {}),
|
|
30
|
+
target: {
|
|
31
|
+
component,
|
|
32
|
+
originX: event.screenX - event.x,
|
|
33
|
+
originY: event.screenY - event.y,
|
|
34
|
+
width: event.width,
|
|
35
|
+
height: event.height,
|
|
36
|
+
},
|
|
37
|
+
};
|
|
42
38
|
}
|
|
43
|
-
|
|
44
|
-
|
|
39
|
+
/** Recreate local coordinates for a previously dispatched mouse target. */
|
|
40
|
+
export function retargetMouseEvent(event, target) {
|
|
41
|
+
return {
|
|
42
|
+
...event,
|
|
43
|
+
x: event.screenX - target.originX,
|
|
44
|
+
y: event.screenY - target.originY,
|
|
45
|
+
width: target.width,
|
|
46
|
+
height: target.height,
|
|
47
|
+
};
|
|
45
48
|
}
|
|
49
|
+
const TERMINAL_PALETTE_SIZE = 16;
|
|
50
|
+
/** OSC 10 and 11 plus OSC 4 for every palette color. */
|
|
51
|
+
const TERMINAL_COLOR_REPLY_COUNT = 2 + TERMINAL_PALETTE_SIZE;
|
|
52
|
+
/**
|
|
53
|
+
* Default colors, palette colors 0-15, and a trailing primary device attributes (DA1) request.
|
|
54
|
+
* Every terminal answers DA1 and terminals answer in order, so the DA1 reply marks the end of
|
|
55
|
+
* the color replies, including for terminals that ignore the color queries.
|
|
56
|
+
*/
|
|
57
|
+
const TERMINAL_COLOR_QUERY = `\x1b]10;?\x07\x1b]11;?\x07${Array.from({ length: TERMINAL_PALETTE_SIZE }, (_, index) => `\x1b]4;${index};?\x07`).join("")}\x1b[c`;
|
|
58
|
+
const DEVICE_ATTRIBUTES_RESPONSE_PATTERN = /^\x1b\[\?[\d;]*c$/;
|
|
46
59
|
/** Type guard to check if a component implements Focusable */
|
|
47
60
|
export function isFocusable(component) {
|
|
48
61
|
return component !== null && "focused" in component;
|
|
@@ -68,16 +81,12 @@ function parseSizeValue(value, referenceSize) {
|
|
|
68
81
|
}
|
|
69
82
|
return undefined;
|
|
70
83
|
}
|
|
71
|
-
function isTermuxSession() {
|
|
72
|
-
return Boolean(process.env.TERMUX_VERSION);
|
|
73
|
-
}
|
|
74
84
|
/**
|
|
75
85
|
* Container - a component that contains other components
|
|
76
86
|
*/
|
|
77
87
|
export class Container {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
88
|
+
children = [];
|
|
89
|
+
mouseLayout;
|
|
81
90
|
addChild(component) {
|
|
82
91
|
this.children.push(component);
|
|
83
92
|
}
|
|
@@ -95,55 +104,118 @@ export class Container {
|
|
|
95
104
|
child.invalidate?.();
|
|
96
105
|
}
|
|
97
106
|
}
|
|
107
|
+
handleMouse(event) {
|
|
108
|
+
if (event.y < 0 || event.y >= event.height)
|
|
109
|
+
return undefined;
|
|
110
|
+
const mouseChildren = this.mouseLayout?.width === event.width
|
|
111
|
+
? this.mouseLayout.children
|
|
112
|
+
: this.children.map((component) => ({ component, height: component.render(event.width).length }));
|
|
113
|
+
let childY = 0;
|
|
114
|
+
for (const { component: child, height: childHeight } of mouseChildren) {
|
|
115
|
+
if (event.y >= childY && event.y < childY + childHeight) {
|
|
116
|
+
const result = dispatchMouseEvent(child, {
|
|
117
|
+
...event,
|
|
118
|
+
y: event.y - childY,
|
|
119
|
+
height: childHeight,
|
|
120
|
+
});
|
|
121
|
+
if (result?.focus && this.handleInput)
|
|
122
|
+
return { ...result, focusTarget: this };
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
childY += childHeight;
|
|
126
|
+
}
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
98
129
|
render(width) {
|
|
99
130
|
const lines = [];
|
|
131
|
+
const mouseChildren = [];
|
|
100
132
|
for (const child of this.children) {
|
|
101
133
|
const childLines = child.render(width);
|
|
134
|
+
mouseChildren.push({ component: child, height: childLines.length });
|
|
102
135
|
for (const line of childLines) {
|
|
103
136
|
lines.push(line);
|
|
104
137
|
}
|
|
105
138
|
}
|
|
139
|
+
this.mouseLayout = { width, children: mouseChildren };
|
|
106
140
|
return lines;
|
|
107
141
|
}
|
|
108
142
|
}
|
|
109
143
|
/**
|
|
110
144
|
* TUI - Main class for managing terminal UI with differential rendering
|
|
111
145
|
*/
|
|
112
|
-
|
|
113
|
-
|
|
146
|
+
const SEGMENT_RESET = "\x1b[0m\x1b]8;;\x07";
|
|
147
|
+
/** Composite overlay content into a terminal line at a fixed column. */
|
|
148
|
+
export function compositeTuiLine(baseLine, overlayLine, startCol, overlayWidth, totalWidth) {
|
|
149
|
+
if (isImageLine(baseLine))
|
|
150
|
+
return baseLine;
|
|
151
|
+
const afterStart = startCol + overlayWidth;
|
|
152
|
+
const base = extractSegments(baseLine, startCol, afterStart, totalWidth - afterStart, true);
|
|
153
|
+
const overlay = sliceWithWidth(overlayLine, 0, overlayWidth, true);
|
|
154
|
+
const beforePad = Math.max(0, startCol - base.beforeWidth);
|
|
155
|
+
const overlayPad = Math.max(0, overlayWidth - overlay.width);
|
|
156
|
+
const actualBeforeWidth = Math.max(startCol, base.beforeWidth);
|
|
157
|
+
const actualOverlayWidth = Math.max(overlayWidth, overlay.width);
|
|
158
|
+
const afterTarget = Math.max(0, totalWidth - actualBeforeWidth - actualOverlayWidth);
|
|
159
|
+
const afterPad = Math.max(0, afterTarget - base.afterWidth);
|
|
160
|
+
const result = base.before +
|
|
161
|
+
" ".repeat(beforePad) +
|
|
162
|
+
SEGMENT_RESET +
|
|
163
|
+
overlay.text +
|
|
164
|
+
" ".repeat(overlayPad) +
|
|
165
|
+
SEGMENT_RESET +
|
|
166
|
+
base.after +
|
|
167
|
+
" ".repeat(afterPad);
|
|
168
|
+
return visibleWidth(result) <= totalWidth ? result : sliceByColumn(result, 0, totalWidth, true);
|
|
169
|
+
}
|
|
170
|
+
export const VIEWPORT_TUI = Symbol.for("@draht/tui/viewport");
|
|
171
|
+
export function isViewportTUI(tui) {
|
|
172
|
+
return tui[VIEWPORT_TUI] === true;
|
|
173
|
+
}
|
|
174
|
+
export class TuiBase extends Container {
|
|
175
|
+
terminal;
|
|
176
|
+
focusedComponent = null;
|
|
177
|
+
inputListeners = new Set();
|
|
178
|
+
/** Global callback for debug key (Shift+Ctrl+D). Called before input is forwarded to focused component. */
|
|
179
|
+
onDebug;
|
|
180
|
+
renderRequested = false;
|
|
181
|
+
immediateRenderScheduled = false;
|
|
182
|
+
renderTimer;
|
|
183
|
+
lastRenderAt = 0;
|
|
184
|
+
static MIN_RENDER_INTERVAL_MS = 16;
|
|
185
|
+
showHardwareCursor = false;
|
|
186
|
+
clearOnShrink = false;
|
|
187
|
+
fullRedrawCount = 0;
|
|
188
|
+
stopped = false;
|
|
189
|
+
/**
|
|
190
|
+
* Color queries waiting for their DA1 reply, oldest first. Terminals answer in order, so color
|
|
191
|
+
* replies belong to the oldest one. Queries stay here after a timeout to collect late replies.
|
|
192
|
+
*/
|
|
193
|
+
pendingTerminalColorQueries = [];
|
|
194
|
+
terminalColorSchemeListeners = new Set();
|
|
195
|
+
terminalColorSchemeNotificationsEnabled = false;
|
|
196
|
+
/** Directory for debug/crash logs. When undefined, debug logging is disabled and crash dumps fall back to the OS temp directory. */
|
|
197
|
+
logDirectory;
|
|
198
|
+
// Overlay stack for modal components rendered on top of base content
|
|
199
|
+
focusOrderCounter = 0;
|
|
200
|
+
overlayStack = [];
|
|
201
|
+
renderedOverlayLayouts = [];
|
|
202
|
+
get hasOverlayEntries() {
|
|
203
|
+
return this.overlayStack.length > 0;
|
|
204
|
+
}
|
|
205
|
+
overlayFocusRestore = { status: "inactive" };
|
|
114
206
|
constructor(terminal, showHardwareCursor, logDirectory) {
|
|
115
207
|
super();
|
|
116
|
-
this.previousLines = [];
|
|
117
|
-
this.previousKittyImageIds = new Set();
|
|
118
|
-
this.previousWidth = 0;
|
|
119
|
-
this.previousHeight = 0;
|
|
120
|
-
this.focusedComponent = null;
|
|
121
|
-
this.inputListeners = new Set();
|
|
122
|
-
this.renderRequested = false;
|
|
123
|
-
this.lastRenderAt = 0;
|
|
124
|
-
this.cursorRow = 0; // Logical cursor row (end of rendered content)
|
|
125
|
-
this.hardwareCursorRow = 0; // Actual terminal cursor row (may differ due to IME positioning)
|
|
126
|
-
this.showHardwareCursor = process.env.PI_HARDWARE_CURSOR === "1";
|
|
127
|
-
this.clearOnShrink = process.env.PI_CLEAR_ON_SHRINK === "1"; // Clear empty rows when content shrinks (default: off)
|
|
128
|
-
this.maxLinesRendered = 0; // Track terminal's working area (max lines ever rendered)
|
|
129
|
-
this.previousViewportTop = 0; // Track previous viewport top for resize-aware cursor moves
|
|
130
|
-
this.fullRedrawCount = 0;
|
|
131
|
-
this.stopped = false;
|
|
132
|
-
this.pendingOsc11BackgroundReplies = 0;
|
|
133
|
-
this.pendingOsc11BackgroundQueries = [];
|
|
134
|
-
this.terminalColorSchemeListeners = new Set();
|
|
135
|
-
this.terminalColorSchemeNotificationsEnabled = false;
|
|
136
|
-
// Overlay stack for modal components rendered on top of base content
|
|
137
|
-
this.focusOrderCounter = 0;
|
|
138
|
-
this.overlayStack = [];
|
|
139
|
-
this.overlayFocusRestore = { status: "inactive" };
|
|
140
208
|
this.terminal = terminal;
|
|
141
|
-
this.logDirectory =
|
|
142
|
-
logDirectory ?? process.env.DRAHT_CODING_AGENT_DIR ?? path.join(os.homedir(), ".draht", "agent");
|
|
209
|
+
this.logDirectory = logDirectory;
|
|
143
210
|
if (showHardwareCursor !== undefined) {
|
|
144
211
|
this.showHardwareCursor = showHardwareCursor;
|
|
145
212
|
}
|
|
146
213
|
}
|
|
214
|
+
resetRenderState() { }
|
|
215
|
+
beforeTerminalStart() { }
|
|
216
|
+
afterTerminalStart() { }
|
|
217
|
+
beforeTerminalStop(_options) { }
|
|
218
|
+
afterTerminalStop(_options) { }
|
|
147
219
|
get fullRedraws() {
|
|
148
220
|
return this.fullRedrawCount;
|
|
149
221
|
}
|
|
@@ -155,7 +227,7 @@ export class TUI extends Container {
|
|
|
155
227
|
return;
|
|
156
228
|
this.showHardwareCursor = enabled;
|
|
157
229
|
if (!enabled) {
|
|
158
|
-
this.
|
|
230
|
+
this.hideTerminalCursor();
|
|
159
231
|
}
|
|
160
232
|
this.requestRender();
|
|
161
233
|
}
|
|
@@ -164,12 +236,15 @@ export class TUI extends Container {
|
|
|
164
236
|
}
|
|
165
237
|
/**
|
|
166
238
|
* Set whether to trigger full re-render when content shrinks.
|
|
167
|
-
* When true
|
|
168
|
-
* When false, empty rows remain (reduces redraws on slower terminals).
|
|
239
|
+
* When true, empty rows are cleared when content shrinks.
|
|
240
|
+
* When false (default), empty rows remain (reduces redraws on slower terminals).
|
|
169
241
|
*/
|
|
170
242
|
setClearOnShrink(enabled) {
|
|
171
243
|
this.clearOnShrink = enabled;
|
|
172
244
|
}
|
|
245
|
+
getFocusedComponent() {
|
|
246
|
+
return this.focusedComponent;
|
|
247
|
+
}
|
|
173
248
|
setFocus(component) {
|
|
174
249
|
this.setFocusInternal({ component, overlayFocusRestore: "clear" });
|
|
175
250
|
}
|
|
@@ -270,8 +345,11 @@ export class TUI extends Container {
|
|
|
270
345
|
}
|
|
271
346
|
}
|
|
272
347
|
}
|
|
348
|
+
getMountedRoots() {
|
|
349
|
+
return this.children;
|
|
350
|
+
}
|
|
273
351
|
isComponentMounted(component) {
|
|
274
|
-
return this.
|
|
352
|
+
return this.getMountedRoots().some((child) => this.containsComponent(child, component));
|
|
275
353
|
}
|
|
276
354
|
containsComponent(root, target) {
|
|
277
355
|
if (root === target)
|
|
@@ -297,7 +375,7 @@ export class TUI extends Container {
|
|
|
297
375
|
if (!options?.nonCapturing && this.isOverlayVisible(entry)) {
|
|
298
376
|
this.setFocus(component);
|
|
299
377
|
}
|
|
300
|
-
this.
|
|
378
|
+
this.hideTerminalCursor();
|
|
301
379
|
this.requestRender();
|
|
302
380
|
// Return handle for controlling this overlay
|
|
303
381
|
return {
|
|
@@ -313,7 +391,7 @@ export class TUI extends Container {
|
|
|
313
391
|
this.setFocus(topVisible?.component ?? entry.preFocus);
|
|
314
392
|
}
|
|
315
393
|
if (this.overlayStack.length === 0)
|
|
316
|
-
this.
|
|
394
|
+
this.hideTerminalCursor();
|
|
317
395
|
this.requestRender();
|
|
318
396
|
}
|
|
319
397
|
},
|
|
@@ -379,6 +457,11 @@ export class TUI extends Container {
|
|
|
379
457
|
this.requestRender();
|
|
380
458
|
},
|
|
381
459
|
isFocused: () => this.focusedComponent === component,
|
|
460
|
+
getBounds: () => {
|
|
461
|
+
if (!this.overlayStack.includes(entry) || !this.isOverlayVisible(entry) || !entry.bounds)
|
|
462
|
+
return undefined;
|
|
463
|
+
return { ...entry.bounds };
|
|
464
|
+
},
|
|
382
465
|
};
|
|
383
466
|
}
|
|
384
467
|
/** Hide the topmost overlay and restore previous focus. */
|
|
@@ -395,13 +478,58 @@ export class TUI extends Container {
|
|
|
395
478
|
this.setFocus(topVisible?.component ?? overlay.preFocus);
|
|
396
479
|
}
|
|
397
480
|
if (this.overlayStack.length === 0)
|
|
398
|
-
this.
|
|
481
|
+
this.hideTerminalCursor();
|
|
399
482
|
this.requestRender();
|
|
400
483
|
}
|
|
484
|
+
/** Hide the cursor while running. After stop(), the shell owns the cursor and it must stay visible. */
|
|
485
|
+
hideTerminalCursor() {
|
|
486
|
+
if (!this.stopped)
|
|
487
|
+
this.terminal.hideCursor();
|
|
488
|
+
}
|
|
401
489
|
/** Check if there are any visible overlays */
|
|
402
490
|
hasOverlay() {
|
|
403
491
|
return this.overlayStack.some((o) => this.isOverlayVisible(o));
|
|
404
492
|
}
|
|
493
|
+
/** Check if the focused component is a visible overlay */
|
|
494
|
+
isOverlayFocused() {
|
|
495
|
+
return this.overlayStack.some((entry) => entry.component === this.focusedComponent && this.isOverlayVisible(entry));
|
|
496
|
+
}
|
|
497
|
+
/** Keep overlay containers as keyboard focus owners when a nested control is clicked. */
|
|
498
|
+
resolveMouseFocusTarget(component) {
|
|
499
|
+
for (let index = this.overlayStack.length - 1; index >= 0; index--) {
|
|
500
|
+
const overlay = this.overlayStack[index];
|
|
501
|
+
if (this.isOverlayVisible(overlay) && this.containsComponent(overlay.component, component)) {
|
|
502
|
+
return overlay.component;
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
return component;
|
|
506
|
+
}
|
|
507
|
+
/** Dispatch to the visually topmost overlay under the pointer. */
|
|
508
|
+
dispatchMouseToOverlay(event) {
|
|
509
|
+
for (let index = this.renderedOverlayLayouts.length - 1; index >= 0; index--) {
|
|
510
|
+
const layout = this.renderedOverlayLayouts[index];
|
|
511
|
+
if (event.screenX < layout.col ||
|
|
512
|
+
event.screenX >= layout.col + layout.width ||
|
|
513
|
+
event.screenY < layout.row ||
|
|
514
|
+
event.screenY >= layout.row + layout.height) {
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
const result = dispatchMouseEvent(layout.entry.component, {
|
|
518
|
+
...event,
|
|
519
|
+
x: event.screenX - layout.col,
|
|
520
|
+
y: event.screenY - layout.row,
|
|
521
|
+
width: layout.width,
|
|
522
|
+
height: layout.height,
|
|
523
|
+
});
|
|
524
|
+
return result
|
|
525
|
+
? {
|
|
526
|
+
hit: true,
|
|
527
|
+
result: result.focus ? { ...result, focusTarget: layout.entry.component } : result,
|
|
528
|
+
}
|
|
529
|
+
: { hit: true };
|
|
530
|
+
}
|
|
531
|
+
return { hit: false };
|
|
532
|
+
}
|
|
405
533
|
/** Check if an overlay entry is currently visible */
|
|
406
534
|
isOverlayVisible(entry) {
|
|
407
535
|
if (entry.hidden)
|
|
@@ -424,13 +552,16 @@ export class TUI extends Container {
|
|
|
424
552
|
return topmost;
|
|
425
553
|
}
|
|
426
554
|
invalidate() {
|
|
427
|
-
|
|
555
|
+
for (const root of this.getMountedRoots())
|
|
556
|
+
root.invalidate();
|
|
428
557
|
for (const overlay of this.overlayStack)
|
|
429
|
-
overlay.component.invalidate
|
|
558
|
+
overlay.component.invalidate();
|
|
430
559
|
}
|
|
431
560
|
start() {
|
|
432
561
|
this.stopped = false;
|
|
433
|
-
this.
|
|
562
|
+
this.beforeTerminalStart();
|
|
563
|
+
this.terminal.start((data) => this.handleTerminalInput(data), () => this.requestRender());
|
|
564
|
+
this.afterTerminalStart();
|
|
434
565
|
this.terminal.hideCursor();
|
|
435
566
|
if (this.terminalColorSchemeNotificationsEnabled) {
|
|
436
567
|
this.terminal.write("\x1b[?2031h");
|
|
@@ -471,54 +602,29 @@ export class TUI extends Container {
|
|
|
471
602
|
// Response format: CSI 6 ; height ; width t
|
|
472
603
|
this.terminal.write("\x1b[16t");
|
|
473
604
|
}
|
|
474
|
-
stop() {
|
|
605
|
+
stop(options = {}) {
|
|
475
606
|
this.stopped = true;
|
|
476
|
-
|
|
477
|
-
clearTimeout(this.renderTimer);
|
|
478
|
-
this.renderTimer = undefined;
|
|
479
|
-
}
|
|
607
|
+
this.cancelRenderTimer();
|
|
480
608
|
if (this.terminalColorSchemeNotificationsEnabled) {
|
|
481
609
|
this.terminal.write("\x1b[?2031l");
|
|
482
610
|
}
|
|
483
|
-
|
|
484
|
-
if (this.previousLines.length > 0) {
|
|
485
|
-
// Overwrite the inverted cursor with a normal space to clear the artifact
|
|
486
|
-
this.terminal.write(" ");
|
|
487
|
-
const targetRow = this.previousLines.length; // Line after the last content
|
|
488
|
-
const lineDiff = targetRow - this.hardwareCursorRow;
|
|
489
|
-
if (lineDiff > 0) {
|
|
490
|
-
this.terminal.write(`\x1b[${lineDiff}B`);
|
|
491
|
-
}
|
|
492
|
-
else if (lineDiff < 0) {
|
|
493
|
-
this.terminal.write(`\x1b[${-lineDiff}A`);
|
|
494
|
-
}
|
|
495
|
-
this.terminal.write("\r\n");
|
|
496
|
-
}
|
|
611
|
+
this.beforeTerminalStop(options);
|
|
497
612
|
this.terminal.showCursor();
|
|
498
613
|
this.terminal.stop();
|
|
614
|
+
this.afterTerminalStop(options);
|
|
615
|
+
}
|
|
616
|
+
renderNow(force = false) {
|
|
617
|
+
if (force)
|
|
618
|
+
this.resetRenderState();
|
|
619
|
+
this.renderRequested = false;
|
|
620
|
+
this.cancelRenderTimer();
|
|
621
|
+
this.lastRenderAt = performance.now();
|
|
622
|
+
this.doRender();
|
|
499
623
|
}
|
|
500
624
|
requestRender(force = false) {
|
|
501
625
|
if (force) {
|
|
502
|
-
this.
|
|
503
|
-
this.
|
|
504
|
-
this.previousHeight = -1; // -1 triggers heightChanged, forcing a full clear
|
|
505
|
-
this.cursorRow = 0;
|
|
506
|
-
this.hardwareCursorRow = 0;
|
|
507
|
-
this.maxLinesRendered = 0;
|
|
508
|
-
this.previousViewportTop = 0;
|
|
509
|
-
if (this.renderTimer) {
|
|
510
|
-
clearTimeout(this.renderTimer);
|
|
511
|
-
this.renderTimer = undefined;
|
|
512
|
-
}
|
|
513
|
-
this.renderRequested = true;
|
|
514
|
-
process.nextTick(() => {
|
|
515
|
-
if (this.stopped || !this.renderRequested) {
|
|
516
|
-
return;
|
|
517
|
-
}
|
|
518
|
-
this.renderRequested = false;
|
|
519
|
-
this.lastRenderAt = performance.now();
|
|
520
|
-
this.doRender();
|
|
521
|
-
});
|
|
626
|
+
this.resetRenderState();
|
|
627
|
+
this.requestImmediateRender();
|
|
522
628
|
return;
|
|
523
629
|
}
|
|
524
630
|
if (this.renderRequested)
|
|
@@ -526,12 +632,36 @@ export class TUI extends Container {
|
|
|
526
632
|
this.renderRequested = true;
|
|
527
633
|
process.nextTick(() => this.scheduleRender());
|
|
528
634
|
}
|
|
635
|
+
requestImmediateRender() {
|
|
636
|
+
this.cancelRenderTimer();
|
|
637
|
+
this.renderRequested = true;
|
|
638
|
+
if (this.immediateRenderScheduled)
|
|
639
|
+
return;
|
|
640
|
+
this.immediateRenderScheduled = true;
|
|
641
|
+
process.nextTick(() => {
|
|
642
|
+
this.immediateRenderScheduled = false;
|
|
643
|
+
if (this.stopped || !this.renderRequested)
|
|
644
|
+
return;
|
|
645
|
+
// A previously queued scheduleRender() can create a timer before this
|
|
646
|
+
// callback runs. User input must preempt that throttled frame.
|
|
647
|
+
this.cancelRenderTimer();
|
|
648
|
+
this.renderRequested = false;
|
|
649
|
+
this.lastRenderAt = performance.now();
|
|
650
|
+
this.doRender();
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
cancelRenderTimer() {
|
|
654
|
+
if (!this.renderTimer)
|
|
655
|
+
return;
|
|
656
|
+
clearTimeout(this.renderTimer);
|
|
657
|
+
this.renderTimer = undefined;
|
|
658
|
+
}
|
|
529
659
|
scheduleRender() {
|
|
530
660
|
if (this.stopped || this.renderTimer || !this.renderRequested) {
|
|
531
661
|
return;
|
|
532
662
|
}
|
|
533
663
|
const elapsed = performance.now() - this.lastRenderAt;
|
|
534
|
-
const delay = Math.max(0,
|
|
664
|
+
const delay = Math.max(0, TuiBase.MIN_RENDER_INTERVAL_MS - elapsed);
|
|
535
665
|
this.renderTimer = setTimeout(() => {
|
|
536
666
|
this.renderTimer = undefined;
|
|
537
667
|
if (this.stopped || !this.renderRequested) {
|
|
@@ -545,8 +675,8 @@ export class TUI extends Container {
|
|
|
545
675
|
}
|
|
546
676
|
}, delay);
|
|
547
677
|
}
|
|
548
|
-
|
|
549
|
-
if (this.
|
|
678
|
+
handleTerminalInput(data) {
|
|
679
|
+
if (this.consumeTerminalColorResponse(data)) {
|
|
550
680
|
return;
|
|
551
681
|
}
|
|
552
682
|
if (this.consumeTerminalColorSchemeReport(data)) {
|
|
@@ -614,30 +744,55 @@ export class TUI extends Container {
|
|
|
614
744
|
return;
|
|
615
745
|
}
|
|
616
746
|
this.focusedComponent.handleInput(data);
|
|
617
|
-
|
|
747
|
+
// Keyboard input is latency-sensitive. Avoid the throttled timer path,
|
|
748
|
+
// where even setTimeout(0) can take a full 16 ms tick on Windows.
|
|
749
|
+
this.requestImmediateRender();
|
|
618
750
|
}
|
|
619
751
|
}
|
|
620
|
-
|
|
621
|
-
|
|
752
|
+
consumeTerminalColorResponse(data) {
|
|
753
|
+
const query = this.pendingTerminalColorQueries[0];
|
|
754
|
+
if (!query) {
|
|
622
755
|
return false;
|
|
623
756
|
}
|
|
624
|
-
if (
|
|
757
|
+
if (DEVICE_ATTRIBUTES_RESPONSE_PATTERN.test(data)) {
|
|
758
|
+
this.pendingTerminalColorQueries.shift();
|
|
759
|
+
this.completeTerminalColorQuery(query);
|
|
760
|
+
return true;
|
|
761
|
+
}
|
|
762
|
+
const response = parseOscColorResponse(data);
|
|
763
|
+
if (!response) {
|
|
625
764
|
return false;
|
|
626
765
|
}
|
|
627
|
-
const rgb =
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
query.
|
|
766
|
+
const { target, rgb } = response;
|
|
767
|
+
const key = String(target);
|
|
768
|
+
if (!query.deliver || query.replied.has(key)) {
|
|
769
|
+
return true;
|
|
770
|
+
}
|
|
771
|
+
query.replied.add(key);
|
|
772
|
+
if (target === "foreground") {
|
|
773
|
+
query.foreground = rgb;
|
|
774
|
+
}
|
|
775
|
+
else if (target === "background") {
|
|
776
|
+
query.background = rgb;
|
|
777
|
+
}
|
|
778
|
+
else if (target < TERMINAL_PALETTE_SIZE) {
|
|
779
|
+
query.palette[target] = rgb;
|
|
780
|
+
}
|
|
781
|
+
if (query.replied.size === TERMINAL_COLOR_REPLY_COUNT) {
|
|
782
|
+
this.completeTerminalColorQuery(query);
|
|
638
783
|
}
|
|
639
784
|
return true;
|
|
640
785
|
}
|
|
786
|
+
terminalColorQueryResult(query) {
|
|
787
|
+
const palette = query.palette.every((color) => color !== undefined) ? query.palette : undefined;
|
|
788
|
+
return { foreground: query.foreground, background: query.background, palette };
|
|
789
|
+
}
|
|
790
|
+
completeTerminalColorQuery(query) {
|
|
791
|
+
const deliver = query.deliver;
|
|
792
|
+
query.deliver = undefined;
|
|
793
|
+
clearTimeout(query.timer);
|
|
794
|
+
deliver?.(this.terminalColorQueryResult(query));
|
|
795
|
+
}
|
|
641
796
|
consumeTerminalColorSchemeReport(data) {
|
|
642
797
|
const scheme = parseTerminalColorSchemeReport(data);
|
|
643
798
|
if (!scheme) {
|
|
@@ -793,9 +948,13 @@ export class TUI extends Container {
|
|
|
793
948
|
}
|
|
794
949
|
/** Composite all overlays into content lines (sorted by focusOrder, higher = on top). */
|
|
795
950
|
compositeOverlays(lines, termWidth, termHeight) {
|
|
796
|
-
if (this.overlayStack.length === 0)
|
|
951
|
+
if (this.overlayStack.length === 0) {
|
|
952
|
+
this.renderedOverlayLayouts = [];
|
|
797
953
|
return lines;
|
|
954
|
+
}
|
|
798
955
|
const result = [...lines];
|
|
956
|
+
for (const entry of this.overlayStack)
|
|
957
|
+
entry.bounds = undefined;
|
|
799
958
|
// Pre-render all visible overlays and calculate positions
|
|
800
959
|
const rendered = [];
|
|
801
960
|
let minLinesNeeded = result.length;
|
|
@@ -814,9 +973,17 @@ export class TUI extends Container {
|
|
|
814
973
|
}
|
|
815
974
|
// Get final row/col with actual overlay height
|
|
816
975
|
const { row, col } = this.resolveOverlayLayout(options, overlayLines.length, termWidth, termHeight);
|
|
817
|
-
|
|
976
|
+
entry.bounds = { row, col, width, height: overlayLines.length };
|
|
977
|
+
rendered.push({ entry, overlayLines, row, col, w: width });
|
|
818
978
|
minLinesNeeded = Math.max(minLinesNeeded, row + overlayLines.length);
|
|
819
979
|
}
|
|
980
|
+
this.renderedOverlayLayouts = rendered.map(({ entry, row, col, w, overlayLines }) => ({
|
|
981
|
+
entry,
|
|
982
|
+
row,
|
|
983
|
+
col,
|
|
984
|
+
width: w,
|
|
985
|
+
height: overlayLines.length,
|
|
986
|
+
}));
|
|
820
987
|
// Pad to at least terminal height so overlays have screen-relative positions.
|
|
821
988
|
// Excludes maxLinesRendered: the historical high-water mark caused self-reinforcing
|
|
822
989
|
// inflation that pushed content into scrollback on terminal widen.
|
|
@@ -840,9 +1007,8 @@ export class TUI extends Container {
|
|
|
840
1007
|
}
|
|
841
1008
|
return result;
|
|
842
1009
|
}
|
|
843
|
-
static { this.SEGMENT_RESET = "\x1b[0m\x1b]8;;\x07"; }
|
|
844
1010
|
applyLineResets(lines) {
|
|
845
|
-
const reset =
|
|
1011
|
+
const reset = SEGMENT_RESET;
|
|
846
1012
|
for (let i = 0; i < lines.length; i++) {
|
|
847
1013
|
const line = lines[i];
|
|
848
1014
|
if (!isImageLine(line)) {
|
|
@@ -851,104 +1017,8 @@ export class TUI extends Container {
|
|
|
851
1017
|
}
|
|
852
1018
|
return lines;
|
|
853
1019
|
}
|
|
854
|
-
collectKittyImageIds(lines) {
|
|
855
|
-
const ids = new Set();
|
|
856
|
-
for (const line of lines) {
|
|
857
|
-
for (const id of extractKittyImageIds(line)) {
|
|
858
|
-
ids.add(id);
|
|
859
|
-
}
|
|
860
|
-
}
|
|
861
|
-
return ids;
|
|
862
|
-
}
|
|
863
|
-
deleteKittyImages(ids) {
|
|
864
|
-
let buffer = "";
|
|
865
|
-
for (const id of ids) {
|
|
866
|
-
buffer += deleteKittyImage(id);
|
|
867
|
-
}
|
|
868
|
-
return buffer;
|
|
869
|
-
}
|
|
870
|
-
getKittyImageReservedRows(lines, index, maxIndex = lines.length - 1) {
|
|
871
|
-
const rows = extractKittyImageRows(lines[index] ?? "");
|
|
872
|
-
if (rows <= 1)
|
|
873
|
-
return 1;
|
|
874
|
-
const maxRows = Math.min(rows, maxIndex - index + 1, lines.length - index);
|
|
875
|
-
let reservedRows = 1;
|
|
876
|
-
while (reservedRows < maxRows) {
|
|
877
|
-
const line = lines[index + reservedRows] ?? "";
|
|
878
|
-
if (isImageLine(line) || visibleWidth(line) > 0)
|
|
879
|
-
break;
|
|
880
|
-
reservedRows++;
|
|
881
|
-
}
|
|
882
|
-
return reservedRows;
|
|
883
|
-
}
|
|
884
|
-
expandChangedRangeForKittyImages(firstChanged, lastChanged, newLines) {
|
|
885
|
-
let expandedFirstChanged = firstChanged;
|
|
886
|
-
let expandedLastChanged = lastChanged;
|
|
887
|
-
const expandForLines = (lines) => {
|
|
888
|
-
for (let i = 0; i < lines.length; i++) {
|
|
889
|
-
if (extractKittyImageIds(lines[i]).length === 0)
|
|
890
|
-
continue;
|
|
891
|
-
const blockEnd = i + this.getKittyImageReservedRows(lines, i) - 1;
|
|
892
|
-
if (i >= firstChanged || (i <= lastChanged && blockEnd >= firstChanged)) {
|
|
893
|
-
expandedFirstChanged = Math.min(expandedFirstChanged, i);
|
|
894
|
-
expandedLastChanged = Math.max(expandedLastChanged, blockEnd);
|
|
895
|
-
}
|
|
896
|
-
}
|
|
897
|
-
};
|
|
898
|
-
expandForLines(this.previousLines);
|
|
899
|
-
expandForLines(newLines);
|
|
900
|
-
return { firstChanged: expandedFirstChanged, lastChanged: expandedLastChanged };
|
|
901
|
-
}
|
|
902
|
-
deleteChangedKittyImages(firstChanged, lastChanged) {
|
|
903
|
-
if (firstChanged < 0 || lastChanged < firstChanged)
|
|
904
|
-
return "";
|
|
905
|
-
const ids = new Set();
|
|
906
|
-
const maxLine = Math.min(lastChanged, this.previousLines.length - 1);
|
|
907
|
-
for (let i = firstChanged; i <= maxLine; i++) {
|
|
908
|
-
for (const id of extractKittyImageIds(this.previousLines[i] ?? "")) {
|
|
909
|
-
ids.add(id);
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
return this.deleteKittyImages(ids);
|
|
913
|
-
}
|
|
914
|
-
/** Splice overlay content into a base line at a specific column. Single-pass optimized. */
|
|
915
1020
|
compositeLineAt(baseLine, overlayLine, startCol, overlayWidth, totalWidth) {
|
|
916
|
-
|
|
917
|
-
return baseLine;
|
|
918
|
-
// Single pass through baseLine extracts both before and after segments
|
|
919
|
-
const afterStart = startCol + overlayWidth;
|
|
920
|
-
const base = extractSegments(baseLine, startCol, afterStart, totalWidth - afterStart, true);
|
|
921
|
-
// Extract overlay with width tracking (strict=true to exclude wide chars at boundary)
|
|
922
|
-
const overlay = sliceWithWidth(overlayLine, 0, overlayWidth, true);
|
|
923
|
-
// Pad segments to target widths
|
|
924
|
-
const beforePad = Math.max(0, startCol - base.beforeWidth);
|
|
925
|
-
const overlayPad = Math.max(0, overlayWidth - overlay.width);
|
|
926
|
-
const actualBeforeWidth = Math.max(startCol, base.beforeWidth);
|
|
927
|
-
const actualOverlayWidth = Math.max(overlayWidth, overlay.width);
|
|
928
|
-
const afterTarget = Math.max(0, totalWidth - actualBeforeWidth - actualOverlayWidth);
|
|
929
|
-
const afterPad = Math.max(0, afterTarget - base.afterWidth);
|
|
930
|
-
// Compose result
|
|
931
|
-
const r = TUI.SEGMENT_RESET;
|
|
932
|
-
const result = base.before +
|
|
933
|
-
" ".repeat(beforePad) +
|
|
934
|
-
r +
|
|
935
|
-
overlay.text +
|
|
936
|
-
" ".repeat(overlayPad) +
|
|
937
|
-
r +
|
|
938
|
-
base.after +
|
|
939
|
-
" ".repeat(afterPad);
|
|
940
|
-
// CRITICAL: Always verify and truncate to terminal width.
|
|
941
|
-
// This is the final safeguard against width overflow which would crash the TUI.
|
|
942
|
-
// Width tracking can drift from actual visible width due to:
|
|
943
|
-
// - Complex ANSI/OSC sequences (hyperlinks, colors)
|
|
944
|
-
// - Wide characters at segment boundaries
|
|
945
|
-
// - Edge cases in segment extraction
|
|
946
|
-
const resultWidth = visibleWidth(result);
|
|
947
|
-
if (resultWidth <= totalWidth) {
|
|
948
|
-
return result;
|
|
949
|
-
}
|
|
950
|
-
// Truncate with strict=true to ensure we don't exceed totalWidth
|
|
951
|
-
return sliceByColumn(result, 0, totalWidth, true);
|
|
1021
|
+
return compositeTuiLine(baseLine, overlayLine, startCol, overlayWidth, totalWidth);
|
|
952
1022
|
}
|
|
953
1023
|
/**
|
|
954
1024
|
* Find and extract cursor position from rendered lines.
|
|
@@ -975,435 +1045,29 @@ export class TUI extends Container {
|
|
|
975
1045
|
}
|
|
976
1046
|
return null;
|
|
977
1047
|
}
|
|
978
|
-
doRender() {
|
|
979
|
-
if (this.stopped)
|
|
980
|
-
return;
|
|
981
|
-
const width = this.terminal.columns;
|
|
982
|
-
const height = this.terminal.rows;
|
|
983
|
-
const widthChanged = this.previousWidth !== 0 && this.previousWidth !== width;
|
|
984
|
-
const heightChanged = this.previousHeight !== 0 && this.previousHeight !== height;
|
|
985
|
-
const previousBufferLength = this.previousHeight > 0 ? this.previousViewportTop + this.previousHeight : height;
|
|
986
|
-
let prevViewportTop = heightChanged ? Math.max(0, previousBufferLength - height) : this.previousViewportTop;
|
|
987
|
-
let viewportTop = prevViewportTop;
|
|
988
|
-
let hardwareCursorRow = this.hardwareCursorRow;
|
|
989
|
-
const computeLineDiff = (targetRow) => {
|
|
990
|
-
const currentScreenRow = hardwareCursorRow - prevViewportTop;
|
|
991
|
-
const targetScreenRow = targetRow - viewportTop;
|
|
992
|
-
return targetScreenRow - currentScreenRow;
|
|
993
|
-
};
|
|
994
|
-
// Render all components to get new lines
|
|
995
|
-
let newLines = this.render(width);
|
|
996
|
-
// Composite overlays into the rendered lines (before differential compare)
|
|
997
|
-
if (this.overlayStack.length > 0) {
|
|
998
|
-
newLines = this.compositeOverlays(newLines, width, height);
|
|
999
|
-
}
|
|
1000
|
-
// Extract cursor position before applying line resets (marker must be found first)
|
|
1001
|
-
const cursorPos = this.extractCursorPosition(newLines, height);
|
|
1002
|
-
newLines = this.applyLineResets(newLines);
|
|
1003
|
-
// Helper to clear scrollback and viewport and render all new lines
|
|
1004
|
-
const fullRender = (clear) => {
|
|
1005
|
-
this.fullRedrawCount += 1;
|
|
1006
|
-
let buffer = "\x1b[?2026h"; // Begin synchronized output
|
|
1007
|
-
if (clear) {
|
|
1008
|
-
buffer += this.deleteKittyImages(this.previousKittyImageIds);
|
|
1009
|
-
buffer += "\x1b[2J\x1b[H\x1b[3J"; // Clear screen, home, then clear scrollback
|
|
1010
|
-
}
|
|
1011
|
-
for (let i = 0; i < newLines.length; i++) {
|
|
1012
|
-
if (i > 0)
|
|
1013
|
-
buffer += "\r\n";
|
|
1014
|
-
const line = newLines[i];
|
|
1015
|
-
const isImage = isImageLine(line);
|
|
1016
|
-
const imageReservedRows = isImage ? this.getKittyImageReservedRows(newLines, i) : 1;
|
|
1017
|
-
if (imageReservedRows > 1 && imageReservedRows <= height) {
|
|
1018
|
-
for (let row = 1; row < imageReservedRows; row++) {
|
|
1019
|
-
buffer += "\r\n";
|
|
1020
|
-
}
|
|
1021
|
-
buffer += `\x1b[${imageReservedRows - 1}A`;
|
|
1022
|
-
buffer += line;
|
|
1023
|
-
buffer += `\x1b[${imageReservedRows - 1}B`;
|
|
1024
|
-
i += imageReservedRows - 1;
|
|
1025
|
-
continue;
|
|
1026
|
-
}
|
|
1027
|
-
buffer += line;
|
|
1028
|
-
}
|
|
1029
|
-
buffer += "\x1b[?2026l"; // End synchronized output
|
|
1030
|
-
this.terminal.write(buffer);
|
|
1031
|
-
this.cursorRow = Math.max(0, newLines.length - 1);
|
|
1032
|
-
this.hardwareCursorRow = this.cursorRow;
|
|
1033
|
-
// Reset max lines when clearing, otherwise track growth
|
|
1034
|
-
if (clear) {
|
|
1035
|
-
this.maxLinesRendered = newLines.length;
|
|
1036
|
-
}
|
|
1037
|
-
else {
|
|
1038
|
-
this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);
|
|
1039
|
-
}
|
|
1040
|
-
const bufferLength = Math.max(height, newLines.length);
|
|
1041
|
-
this.previousViewportTop = Math.max(0, bufferLength - height);
|
|
1042
|
-
this.positionHardwareCursor(cursorPos, newLines.length);
|
|
1043
|
-
this.previousLines = newLines;
|
|
1044
|
-
this.previousKittyImageIds = this.collectKittyImageIds(newLines);
|
|
1045
|
-
this.previousWidth = width;
|
|
1046
|
-
this.previousHeight = height;
|
|
1047
|
-
};
|
|
1048
|
-
const debugRedraw = process.env.PI_DEBUG_REDRAW === "1";
|
|
1049
|
-
const logRedraw = (reason) => {
|
|
1050
|
-
if (!debugRedraw)
|
|
1051
|
-
return;
|
|
1052
|
-
const logPath = path.join(this.logDirectory, "pi-debug.log");
|
|
1053
|
-
const msg = `[${new Date().toISOString()}] fullRender: ${reason} (prev=${this.previousLines.length}, new=${newLines.length}, height=${height})\n`;
|
|
1054
|
-
fs.mkdirSync(path.dirname(logPath), { recursive: true });
|
|
1055
|
-
fs.appendFileSync(logPath, msg);
|
|
1056
|
-
};
|
|
1057
|
-
// First render - just output everything without clearing (assumes clean screen)
|
|
1058
|
-
if (this.previousLines.length === 0 && !widthChanged && !heightChanged) {
|
|
1059
|
-
logRedraw("first render");
|
|
1060
|
-
fullRender(false);
|
|
1061
|
-
return;
|
|
1062
|
-
}
|
|
1063
|
-
// Width changes always need a full re-render because wrapping changes.
|
|
1064
|
-
if (widthChanged) {
|
|
1065
|
-
logRedraw(`terminal width changed (${this.previousWidth} -> ${width})`);
|
|
1066
|
-
fullRender(true);
|
|
1067
|
-
return;
|
|
1068
|
-
}
|
|
1069
|
-
// Height changes normally need a full re-render to keep the visible viewport aligned,
|
|
1070
|
-
// but Termux changes height when the software keyboard shows or hides.
|
|
1071
|
-
// In that environment, a full redraw causes the entire history to replay on every toggle.
|
|
1072
|
-
if (heightChanged && !isTermuxSession()) {
|
|
1073
|
-
logRedraw(`terminal height changed (${this.previousHeight} -> ${height})`);
|
|
1074
|
-
fullRender(true);
|
|
1075
|
-
return;
|
|
1076
|
-
}
|
|
1077
|
-
// Content shrunk below the working area and no overlays - re-render to clear empty rows
|
|
1078
|
-
// (overlays need the padding, so only do this when no overlays are active)
|
|
1079
|
-
// Configurable via setClearOnShrink() or PI_CLEAR_ON_SHRINK=0 env var
|
|
1080
|
-
if (this.clearOnShrink && newLines.length < this.maxLinesRendered && this.overlayStack.length === 0) {
|
|
1081
|
-
logRedraw(`clearOnShrink (maxLinesRendered=${this.maxLinesRendered})`);
|
|
1082
|
-
fullRender(true);
|
|
1083
|
-
return;
|
|
1084
|
-
}
|
|
1085
|
-
// Find first and last changed lines
|
|
1086
|
-
let firstChanged = -1;
|
|
1087
|
-
let lastChanged = -1;
|
|
1088
|
-
const maxLines = Math.max(newLines.length, this.previousLines.length);
|
|
1089
|
-
for (let i = 0; i < maxLines; i++) {
|
|
1090
|
-
const oldLine = i < this.previousLines.length ? this.previousLines[i] : "";
|
|
1091
|
-
const newLine = i < newLines.length ? newLines[i] : "";
|
|
1092
|
-
if (oldLine !== newLine) {
|
|
1093
|
-
if (firstChanged === -1) {
|
|
1094
|
-
firstChanged = i;
|
|
1095
|
-
}
|
|
1096
|
-
lastChanged = i;
|
|
1097
|
-
}
|
|
1098
|
-
}
|
|
1099
|
-
const appendedLines = newLines.length > this.previousLines.length;
|
|
1100
|
-
if (appendedLines) {
|
|
1101
|
-
if (firstChanged === -1) {
|
|
1102
|
-
firstChanged = this.previousLines.length;
|
|
1103
|
-
}
|
|
1104
|
-
lastChanged = newLines.length - 1;
|
|
1105
|
-
}
|
|
1106
|
-
if (firstChanged !== -1) {
|
|
1107
|
-
const expandedRange = this.expandChangedRangeForKittyImages(firstChanged, lastChanged, newLines);
|
|
1108
|
-
firstChanged = expandedRange.firstChanged;
|
|
1109
|
-
lastChanged = expandedRange.lastChanged;
|
|
1110
|
-
}
|
|
1111
|
-
const appendStart = appendedLines && firstChanged === this.previousLines.length && firstChanged > 0;
|
|
1112
|
-
// No changes - but still need to update hardware cursor position if it moved
|
|
1113
|
-
if (firstChanged === -1) {
|
|
1114
|
-
this.positionHardwareCursor(cursorPos, newLines.length);
|
|
1115
|
-
this.previousViewportTop = prevViewportTop;
|
|
1116
|
-
this.previousHeight = height;
|
|
1117
|
-
return;
|
|
1118
|
-
}
|
|
1119
|
-
// All changes are in deleted lines (nothing to render, just clear)
|
|
1120
|
-
if (firstChanged >= newLines.length) {
|
|
1121
|
-
if (this.previousLines.length > newLines.length) {
|
|
1122
|
-
let buffer = "\x1b[?2026h";
|
|
1123
|
-
buffer += this.deleteChangedKittyImages(firstChanged, lastChanged);
|
|
1124
|
-
// Move to end of new content (clamp to 0 for empty content)
|
|
1125
|
-
const targetRow = Math.max(0, newLines.length - 1);
|
|
1126
|
-
if (targetRow < prevViewportTop) {
|
|
1127
|
-
logRedraw(`deleted lines moved viewport up (${targetRow} < ${prevViewportTop})`);
|
|
1128
|
-
fullRender(true);
|
|
1129
|
-
return;
|
|
1130
|
-
}
|
|
1131
|
-
const lineDiff = computeLineDiff(targetRow);
|
|
1132
|
-
if (lineDiff > 0)
|
|
1133
|
-
buffer += `\x1b[${lineDiff}B`;
|
|
1134
|
-
else if (lineDiff < 0)
|
|
1135
|
-
buffer += `\x1b[${-lineDiff}A`;
|
|
1136
|
-
buffer += "\r";
|
|
1137
|
-
// Clear extra lines without scrolling
|
|
1138
|
-
const extraLines = this.previousLines.length - newLines.length;
|
|
1139
|
-
if (extraLines > height) {
|
|
1140
|
-
logRedraw(`extraLines > height (${extraLines} > ${height})`);
|
|
1141
|
-
fullRender(true);
|
|
1142
|
-
return;
|
|
1143
|
-
}
|
|
1144
|
-
const clearStartOffset = newLines.length === 0 ? 0 : 1;
|
|
1145
|
-
if (extraLines > 0 && clearStartOffset > 0) {
|
|
1146
|
-
buffer += `\x1b[${clearStartOffset}B`;
|
|
1147
|
-
}
|
|
1148
|
-
for (let i = 0; i < extraLines; i++) {
|
|
1149
|
-
buffer += "\r\x1b[2K";
|
|
1150
|
-
if (i < extraLines - 1)
|
|
1151
|
-
buffer += "\x1b[1B";
|
|
1152
|
-
}
|
|
1153
|
-
const moveBack = Math.max(0, extraLines - 1 + clearStartOffset);
|
|
1154
|
-
if (moveBack > 0) {
|
|
1155
|
-
buffer += `\x1b[${moveBack}A`;
|
|
1156
|
-
}
|
|
1157
|
-
buffer += "\x1b[?2026l";
|
|
1158
|
-
this.terminal.write(buffer);
|
|
1159
|
-
this.cursorRow = targetRow;
|
|
1160
|
-
this.hardwareCursorRow = targetRow;
|
|
1161
|
-
}
|
|
1162
|
-
this.positionHardwareCursor(cursorPos, newLines.length);
|
|
1163
|
-
this.previousLines = newLines;
|
|
1164
|
-
this.previousKittyImageIds = this.collectKittyImageIds(newLines);
|
|
1165
|
-
this.previousWidth = width;
|
|
1166
|
-
this.previousHeight = height;
|
|
1167
|
-
this.previousViewportTop = prevViewportTop;
|
|
1168
|
-
return;
|
|
1169
|
-
}
|
|
1170
|
-
// Differential rendering can only touch what was actually visible.
|
|
1171
|
-
// If the first changed line is above the previous viewport, we need a full redraw.
|
|
1172
|
-
if (firstChanged < prevViewportTop) {
|
|
1173
|
-
logRedraw(`firstChanged < viewportTop (${firstChanged} < ${prevViewportTop})`);
|
|
1174
|
-
fullRender(true);
|
|
1175
|
-
return;
|
|
1176
|
-
}
|
|
1177
|
-
// Render from first changed line to end
|
|
1178
|
-
// Build buffer with all updates wrapped in synchronized output
|
|
1179
|
-
let buffer = "\x1b[?2026h"; // Begin synchronized output
|
|
1180
|
-
buffer += this.deleteChangedKittyImages(firstChanged, lastChanged);
|
|
1181
|
-
const prevViewportBottom = prevViewportTop + height - 1;
|
|
1182
|
-
const moveTargetRow = appendStart ? firstChanged - 1 : firstChanged;
|
|
1183
|
-
if (moveTargetRow > prevViewportBottom) {
|
|
1184
|
-
const currentScreenRow = Math.max(0, Math.min(height - 1, hardwareCursorRow - prevViewportTop));
|
|
1185
|
-
const moveToBottom = height - 1 - currentScreenRow;
|
|
1186
|
-
if (moveToBottom > 0) {
|
|
1187
|
-
buffer += `\x1b[${moveToBottom}B`;
|
|
1188
|
-
}
|
|
1189
|
-
const scroll = moveTargetRow - prevViewportBottom;
|
|
1190
|
-
buffer += "\r\n".repeat(scroll);
|
|
1191
|
-
prevViewportTop += scroll;
|
|
1192
|
-
viewportTop += scroll;
|
|
1193
|
-
hardwareCursorRow = moveTargetRow;
|
|
1194
|
-
}
|
|
1195
|
-
// Move cursor to first changed line (use hardwareCursorRow for actual position)
|
|
1196
|
-
const lineDiff = computeLineDiff(moveTargetRow);
|
|
1197
|
-
if (lineDiff > 0) {
|
|
1198
|
-
buffer += `\x1b[${lineDiff}B`; // Move down
|
|
1199
|
-
}
|
|
1200
|
-
else if (lineDiff < 0) {
|
|
1201
|
-
buffer += `\x1b[${-lineDiff}A`; // Move up
|
|
1202
|
-
}
|
|
1203
|
-
buffer += appendStart ? "\r\n" : "\r"; // Move to column 0
|
|
1204
|
-
// Only render changed lines (firstChanged to lastChanged), not all lines to end
|
|
1205
|
-
// This reduces flicker when only a single line changes (e.g., spinner animation)
|
|
1206
|
-
const renderEnd = Math.min(lastChanged, newLines.length - 1);
|
|
1207
|
-
for (let i = firstChanged; i <= renderEnd; i++) {
|
|
1208
|
-
if (i > firstChanged)
|
|
1209
|
-
buffer += "\r\n";
|
|
1210
|
-
const line = newLines[i];
|
|
1211
|
-
const isImage = isImageLine(line);
|
|
1212
|
-
const imageReservedRows = isImage ? this.getKittyImageReservedRows(newLines, i, renderEnd) : 1;
|
|
1213
|
-
if (imageReservedRows > 1) {
|
|
1214
|
-
const imageStartScreenRow = i - viewportTop;
|
|
1215
|
-
if (imageStartScreenRow < 0 || imageStartScreenRow + imageReservedRows > height) {
|
|
1216
|
-
logRedraw(`kitty image pre-clear would scroll (${imageStartScreenRow} + ${imageReservedRows} > ${height})`);
|
|
1217
|
-
fullRender(true);
|
|
1218
|
-
return;
|
|
1219
|
-
}
|
|
1220
|
-
buffer += "\x1b[2K";
|
|
1221
|
-
for (let row = 1; row < imageReservedRows; row++) {
|
|
1222
|
-
buffer += "\r\n\x1b[2K";
|
|
1223
|
-
}
|
|
1224
|
-
buffer += `\x1b[${imageReservedRows - 1}A`;
|
|
1225
|
-
buffer += line;
|
|
1226
|
-
buffer += `\x1b[${imageReservedRows - 1}B`;
|
|
1227
|
-
i += imageReservedRows - 1;
|
|
1228
|
-
continue;
|
|
1229
|
-
}
|
|
1230
|
-
buffer += "\x1b[2K"; // Clear current line
|
|
1231
|
-
if (!isImage && visibleWidth(line) > width) {
|
|
1232
|
-
// Log all lines to crash file for debugging
|
|
1233
|
-
const crashLogPath = path.join(this.logDirectory, "pi-crash.log");
|
|
1234
|
-
const crashData = [
|
|
1235
|
-
`Crash at ${new Date().toISOString()}`,
|
|
1236
|
-
`Terminal width: ${width}`,
|
|
1237
|
-
`Line ${i} visible width: ${visibleWidth(line)}`,
|
|
1238
|
-
"",
|
|
1239
|
-
"=== All rendered lines ===",
|
|
1240
|
-
...newLines.map((l, idx) => `[${idx}] (w=${visibleWidth(l)}) ${l}`),
|
|
1241
|
-
"",
|
|
1242
|
-
].join("\n");
|
|
1243
|
-
fs.mkdirSync(path.dirname(crashLogPath), { recursive: true });
|
|
1244
|
-
fs.writeFileSync(crashLogPath, crashData);
|
|
1245
|
-
// Clean up terminal state before throwing
|
|
1246
|
-
this.stop();
|
|
1247
|
-
const errorMsg = [
|
|
1248
|
-
`Rendered line ${i} exceeds terminal width (${visibleWidth(line)} > ${width}).`,
|
|
1249
|
-
"",
|
|
1250
|
-
"This is likely caused by a custom TUI component not truncating its output.",
|
|
1251
|
-
"Use visibleWidth() to measure and truncateToWidth() to truncate lines.",
|
|
1252
|
-
"",
|
|
1253
|
-
`Debug log written to: ${crashLogPath}`,
|
|
1254
|
-
].join("\n");
|
|
1255
|
-
throw new Error(errorMsg);
|
|
1256
|
-
}
|
|
1257
|
-
buffer += line;
|
|
1258
|
-
}
|
|
1259
|
-
// Track where cursor ended up after rendering
|
|
1260
|
-
let finalCursorRow = renderEnd;
|
|
1261
|
-
// If we had more lines before, clear them and move cursor back
|
|
1262
|
-
if (this.previousLines.length > newLines.length) {
|
|
1263
|
-
// Move to end of new content first if we stopped before it
|
|
1264
|
-
if (renderEnd < newLines.length - 1) {
|
|
1265
|
-
const moveDown = newLines.length - 1 - renderEnd;
|
|
1266
|
-
buffer += `\x1b[${moveDown}B`;
|
|
1267
|
-
finalCursorRow = newLines.length - 1;
|
|
1268
|
-
}
|
|
1269
|
-
const extraLines = this.previousLines.length - newLines.length;
|
|
1270
|
-
for (let i = newLines.length; i < this.previousLines.length; i++) {
|
|
1271
|
-
buffer += "\r\n\x1b[2K";
|
|
1272
|
-
}
|
|
1273
|
-
// Move cursor back to end of new content
|
|
1274
|
-
buffer += `\x1b[${extraLines}A`;
|
|
1275
|
-
}
|
|
1276
|
-
buffer += "\x1b[?2026l"; // End synchronized output
|
|
1277
|
-
if (process.env.PI_TUI_DEBUG === "1") {
|
|
1278
|
-
const debugDir = "/tmp/tui";
|
|
1279
|
-
fs.mkdirSync(debugDir, { recursive: true });
|
|
1280
|
-
const debugPath = path.join(debugDir, `render-${Date.now()}-${Math.random().toString(36).slice(2)}.log`);
|
|
1281
|
-
const debugData = [
|
|
1282
|
-
`firstChanged: ${firstChanged}`,
|
|
1283
|
-
`viewportTop: ${viewportTop}`,
|
|
1284
|
-
`cursorRow: ${this.cursorRow}`,
|
|
1285
|
-
`height: ${height}`,
|
|
1286
|
-
`lineDiff: ${lineDiff}`,
|
|
1287
|
-
`hardwareCursorRow: ${hardwareCursorRow}`,
|
|
1288
|
-
`renderEnd: ${renderEnd}`,
|
|
1289
|
-
`finalCursorRow: ${finalCursorRow}`,
|
|
1290
|
-
`cursorPos: ${JSON.stringify(cursorPos)}`,
|
|
1291
|
-
`newLines.length: ${newLines.length}`,
|
|
1292
|
-
`previousLines.length: ${this.previousLines.length}`,
|
|
1293
|
-
"",
|
|
1294
|
-
"=== newLines ===",
|
|
1295
|
-
JSON.stringify(newLines, null, 2),
|
|
1296
|
-
"",
|
|
1297
|
-
"=== previousLines ===",
|
|
1298
|
-
JSON.stringify(this.previousLines, null, 2),
|
|
1299
|
-
"",
|
|
1300
|
-
"=== buffer ===",
|
|
1301
|
-
JSON.stringify(buffer),
|
|
1302
|
-
].join("\n");
|
|
1303
|
-
fs.writeFileSync(debugPath, debugData);
|
|
1304
|
-
}
|
|
1305
|
-
// Write entire buffer at once
|
|
1306
|
-
this.terminal.write(buffer);
|
|
1307
|
-
// Track cursor position for next render
|
|
1308
|
-
// cursorRow tracks end of content (for viewport calculation)
|
|
1309
|
-
// hardwareCursorRow tracks actual terminal cursor position (for movement)
|
|
1310
|
-
this.cursorRow = Math.max(0, newLines.length - 1);
|
|
1311
|
-
this.hardwareCursorRow = finalCursorRow;
|
|
1312
|
-
// Track terminal's working area (grows but doesn't shrink unless cleared)
|
|
1313
|
-
this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);
|
|
1314
|
-
this.previousViewportTop = Math.max(prevViewportTop, finalCursorRow - height + 1);
|
|
1315
|
-
// Position hardware cursor for IME
|
|
1316
|
-
this.positionHardwareCursor(cursorPos, newLines.length);
|
|
1317
|
-
this.previousLines = newLines;
|
|
1318
|
-
this.previousKittyImageIds = this.collectKittyImageIds(newLines);
|
|
1319
|
-
this.previousWidth = width;
|
|
1320
|
-
this.previousHeight = height;
|
|
1321
|
-
}
|
|
1322
|
-
/**
|
|
1323
|
-
* Position the hardware cursor for IME candidate window.
|
|
1324
|
-
* @param cursorPos The cursor position extracted from rendered output, or null
|
|
1325
|
-
* @param totalLines Total number of rendered lines
|
|
1326
|
-
*/
|
|
1327
|
-
positionHardwareCursor(cursorPos, totalLines) {
|
|
1328
|
-
if (!cursorPos || totalLines <= 0) {
|
|
1329
|
-
this.terminal.hideCursor();
|
|
1330
|
-
return;
|
|
1331
|
-
}
|
|
1332
|
-
// Clamp cursor position to valid range
|
|
1333
|
-
const targetRow = Math.max(0, Math.min(cursorPos.row, totalLines - 1));
|
|
1334
|
-
const targetCol = Math.max(0, cursorPos.col);
|
|
1335
|
-
// Move cursor from current position to target
|
|
1336
|
-
const rowDelta = targetRow - this.hardwareCursorRow;
|
|
1337
|
-
let buffer = "";
|
|
1338
|
-
if (rowDelta > 0) {
|
|
1339
|
-
buffer += `\x1b[${rowDelta}B`; // Move down
|
|
1340
|
-
}
|
|
1341
|
-
else if (rowDelta < 0) {
|
|
1342
|
-
buffer += `\x1b[${-rowDelta}A`; // Move up
|
|
1343
|
-
}
|
|
1344
|
-
// Move to absolute column (1-indexed)
|
|
1345
|
-
buffer += `\x1b[${targetCol + 1}G`;
|
|
1346
|
-
if (buffer) {
|
|
1347
|
-
this.terminal.write(buffer);
|
|
1348
|
-
}
|
|
1349
|
-
this.hardwareCursorRow = targetRow;
|
|
1350
|
-
if (this.showHardwareCursor) {
|
|
1351
|
-
this.terminal.showCursor();
|
|
1352
|
-
}
|
|
1353
|
-
else {
|
|
1354
|
-
this.terminal.hideCursor();
|
|
1355
|
-
}
|
|
1356
|
-
}
|
|
1357
1048
|
/**
|
|
1358
|
-
* Query the terminal's
|
|
1359
|
-
*
|
|
1360
|
-
*
|
|
1049
|
+
* Query the terminal's theme colors: the default foreground (OSC 10), the default background
|
|
1050
|
+
* (OSC 11), and ANSI colors 0-15 (OSC 4), followed by a DA1 request that marks the end of the
|
|
1051
|
+
* replies. Resolves when the DA1 reply or all color replies arrive, or when the timeout expires.
|
|
1052
|
+
* Colors the terminal did not report are undefined; the palette is only set when all 16 arrived.
|
|
1053
|
+
* @param timeoutMs Query timeout in milliseconds, for terminals that do not answer DA1 either.
|
|
1054
|
+
* @param onLateReply Receives the replies if the query completes after the timeout, e.g. over slow links.
|
|
1361
1055
|
*/
|
|
1362
|
-
|
|
1056
|
+
queryTerminalColors({ timeoutMs, onLateReply, }) {
|
|
1363
1057
|
return new Promise((resolve) => {
|
|
1364
1058
|
const query = {
|
|
1365
|
-
|
|
1366
|
-
|
|
1059
|
+
palette: Array.from({ length: TERMINAL_PALETTE_SIZE }, () => undefined),
|
|
1060
|
+
replied: new Set(),
|
|
1061
|
+
deliver: resolve,
|
|
1367
1062
|
timer: undefined,
|
|
1368
1063
|
};
|
|
1064
|
+
// Resolve with the replies so far, and keep collecting late replies for `onLateReply`.
|
|
1369
1065
|
query.timer = setTimeout(() => {
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
}
|
|
1373
|
-
query.settled = true;
|
|
1374
|
-
query.timer = undefined;
|
|
1375
|
-
query.resolve?.(undefined);
|
|
1376
|
-
query.resolve = undefined;
|
|
1066
|
+
query.deliver = onLateReply;
|
|
1067
|
+
resolve(this.terminalColorQueryResult(query));
|
|
1377
1068
|
}, timeoutMs);
|
|
1378
|
-
this.
|
|
1379
|
-
this.
|
|
1380
|
-
this.terminal.write("\x1b]11;?\x07");
|
|
1381
|
-
});
|
|
1382
|
-
}
|
|
1383
|
-
/**
|
|
1384
|
-
* Query the terminal's color-scheme preference with DSR (`CSI ? 996 n`).
|
|
1385
|
-
* Terminals that support the color palette notification protocol reply with
|
|
1386
|
-
* `CSI ? 997 ; 1 n` for dark or `CSI ? 997 ; 2 n` for light.
|
|
1387
|
-
*/
|
|
1388
|
-
queryTerminalColorScheme({ timeoutMs }) {
|
|
1389
|
-
return new Promise((resolve) => {
|
|
1390
|
-
let settled = false;
|
|
1391
|
-
let timer;
|
|
1392
|
-
let unsubscribe = () => { };
|
|
1393
|
-
const settle = (scheme) => {
|
|
1394
|
-
if (settled)
|
|
1395
|
-
return;
|
|
1396
|
-
settled = true;
|
|
1397
|
-
if (timer) {
|
|
1398
|
-
clearTimeout(timer);
|
|
1399
|
-
timer = undefined;
|
|
1400
|
-
}
|
|
1401
|
-
unsubscribe();
|
|
1402
|
-
resolve(scheme);
|
|
1403
|
-
};
|
|
1404
|
-
unsubscribe = this.onTerminalColorSchemeChange(settle);
|
|
1405
|
-
timer = setTimeout(() => settle(undefined), timeoutMs);
|
|
1406
|
-
this.terminal.write("\x1b[?996n");
|
|
1069
|
+
this.pendingTerminalColorQueries.push(query);
|
|
1070
|
+
this.terminal.write(TERMINAL_COLOR_QUERY);
|
|
1407
1071
|
});
|
|
1408
1072
|
}
|
|
1409
1073
|
}
|