@draht/tui 2026.3.2-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/README.md +761 -0
- package/dist/autocomplete.d.ts +50 -0
- package/dist/autocomplete.d.ts.map +1 -0
- package/dist/autocomplete.js +596 -0
- package/dist/autocomplete.js.map +1 -0
- package/dist/components/box.d.ts +22 -0
- package/dist/components/box.d.ts.map +1 -0
- package/dist/components/box.js +104 -0
- package/dist/components/box.js.map +1 -0
- package/dist/components/cancellable-loader.d.ts +22 -0
- package/dist/components/cancellable-loader.d.ts.map +1 -0
- package/dist/components/cancellable-loader.js +35 -0
- package/dist/components/cancellable-loader.js.map +1 -0
- package/dist/components/editor.d.ts +205 -0
- package/dist/components/editor.d.ts.map +1 -0
- package/dist/components/editor.js +1679 -0
- package/dist/components/editor.js.map +1 -0
- package/dist/components/image.d.ts +28 -0
- package/dist/components/image.d.ts.map +1 -0
- package/dist/components/image.js +69 -0
- package/dist/components/image.js.map +1 -0
- package/dist/components/input.d.ts +37 -0
- package/dist/components/input.d.ts.map +1 -0
- package/dist/components/input.js +433 -0
- package/dist/components/input.js.map +1 -0
- package/dist/components/loader.d.ts +21 -0
- package/dist/components/loader.d.ts.map +1 -0
- package/dist/components/loader.js +49 -0
- package/dist/components/loader.js.map +1 -0
- package/dist/components/markdown.d.ts +95 -0
- package/dist/components/markdown.d.ts.map +1 -0
- package/dist/components/markdown.js +629 -0
- package/dist/components/markdown.js.map +1 -0
- package/dist/components/select-list.d.ts +32 -0
- package/dist/components/select-list.d.ts.map +1 -0
- package/dist/components/select-list.js +152 -0
- package/dist/components/select-list.js.map +1 -0
- package/dist/components/settings-list.d.ts +50 -0
- package/dist/components/settings-list.d.ts.map +1 -0
- package/dist/components/settings-list.js +185 -0
- package/dist/components/settings-list.js.map +1 -0
- package/dist/components/spacer.d.ts +12 -0
- package/dist/components/spacer.d.ts.map +1 -0
- package/dist/components/spacer.js +23 -0
- package/dist/components/spacer.js.map +1 -0
- package/dist/components/text.d.ts +19 -0
- package/dist/components/text.d.ts.map +1 -0
- package/dist/components/text.js +89 -0
- package/dist/components/text.js.map +1 -0
- package/dist/components/truncated-text.d.ts +13 -0
- package/dist/components/truncated-text.d.ts.map +1 -0
- package/dist/components/truncated-text.js +51 -0
- package/dist/components/truncated-text.js.map +1 -0
- package/dist/editor-component.d.ts +39 -0
- package/dist/editor-component.d.ts.map +1 -0
- package/dist/editor-component.js +2 -0
- package/dist/editor-component.js.map +1 -0
- package/dist/fuzzy.d.ts +16 -0
- package/dist/fuzzy.d.ts.map +1 -0
- package/dist/fuzzy.js +107 -0
- package/dist/fuzzy.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/keybindings.d.ts +39 -0
- package/dist/keybindings.d.ts.map +1 -0
- package/dist/keybindings.js +114 -0
- package/dist/keybindings.js.map +1 -0
- package/dist/keys.d.ts +160 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/keys.js +959 -0
- package/dist/keys.js.map +1 -0
- package/dist/kill-ring.d.ts +28 -0
- package/dist/kill-ring.d.ts.map +1 -0
- package/dist/kill-ring.js +44 -0
- package/dist/kill-ring.js.map +1 -0
- package/dist/stdin-buffer.d.ts +48 -0
- package/dist/stdin-buffer.d.ts.map +1 -0
- package/dist/stdin-buffer.js +317 -0
- package/dist/stdin-buffer.js.map +1 -0
- package/dist/terminal-image.d.ts +68 -0
- package/dist/terminal-image.d.ts.map +1 -0
- package/dist/terminal-image.js +288 -0
- package/dist/terminal-image.js.map +1 -0
- package/dist/terminal.d.ts +78 -0
- package/dist/terminal.d.ts.map +1 -0
- package/dist/terminal.js +249 -0
- package/dist/terminal.js.map +1 -0
- package/dist/tui.d.ts +210 -0
- package/dist/tui.d.ts.map +1 -0
- package/dist/tui.js +955 -0
- package/dist/tui.js.map +1 -0
- package/dist/undo-stack.d.ts +17 -0
- package/dist/undo-stack.d.ts.map +1 -0
- package/dist/undo-stack.js +25 -0
- package/dist/undo-stack.js.map +1 -0
- package/dist/utils.d.ts +78 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +800 -0
- package/dist/utils.js.map +1 -0
- package/package.json +53 -0
package/dist/tui.js
ADDED
|
@@ -0,0 +1,955 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal TUI implementation with differential rendering
|
|
3
|
+
*/
|
|
4
|
+
import * as fs from "node:fs";
|
|
5
|
+
import * as os from "node:os";
|
|
6
|
+
import * as path from "node:path";
|
|
7
|
+
import { isKeyRelease, matchesKey } from "./keys.js";
|
|
8
|
+
import { getCapabilities, isImageLine, setCellDimensions } from "./terminal-image.js";
|
|
9
|
+
import { extractSegments, sliceByColumn, sliceWithWidth, visibleWidth } from "./utils.js";
|
|
10
|
+
/** Type guard to check if a component implements Focusable */
|
|
11
|
+
export function isFocusable(component) {
|
|
12
|
+
return component !== null && "focused" in component;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Cursor position marker - APC (Application Program Command) sequence.
|
|
16
|
+
* This is a zero-width escape sequence that terminals ignore.
|
|
17
|
+
* Components emit this at the cursor position when focused.
|
|
18
|
+
* TUI finds and strips this marker, then positions the hardware cursor there.
|
|
19
|
+
*/
|
|
20
|
+
export const CURSOR_MARKER = "\x1b_pi:c\x07";
|
|
21
|
+
export { visibleWidth };
|
|
22
|
+
/** Parse a SizeValue into absolute value given a reference size */
|
|
23
|
+
function parseSizeValue(value, referenceSize) {
|
|
24
|
+
if (value === undefined)
|
|
25
|
+
return undefined;
|
|
26
|
+
if (typeof value === "number")
|
|
27
|
+
return value;
|
|
28
|
+
// Parse percentage string like "50%"
|
|
29
|
+
const match = value.match(/^(\d+(?:\.\d+)?)%$/);
|
|
30
|
+
if (match) {
|
|
31
|
+
return Math.floor((referenceSize * parseFloat(match[1])) / 100);
|
|
32
|
+
}
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Container - a component that contains other components
|
|
37
|
+
*/
|
|
38
|
+
export class Container {
|
|
39
|
+
children = [];
|
|
40
|
+
addChild(component) {
|
|
41
|
+
this.children.push(component);
|
|
42
|
+
}
|
|
43
|
+
removeChild(component) {
|
|
44
|
+
const index = this.children.indexOf(component);
|
|
45
|
+
if (index !== -1) {
|
|
46
|
+
this.children.splice(index, 1);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
clear() {
|
|
50
|
+
this.children = [];
|
|
51
|
+
}
|
|
52
|
+
invalidate() {
|
|
53
|
+
for (const child of this.children) {
|
|
54
|
+
child.invalidate?.();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
render(width) {
|
|
58
|
+
const lines = [];
|
|
59
|
+
for (const child of this.children) {
|
|
60
|
+
lines.push(...child.render(width));
|
|
61
|
+
}
|
|
62
|
+
return lines;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* TUI - Main class for managing terminal UI with differential rendering
|
|
67
|
+
*/
|
|
68
|
+
export class TUI extends Container {
|
|
69
|
+
terminal;
|
|
70
|
+
previousLines = [];
|
|
71
|
+
previousWidth = 0;
|
|
72
|
+
focusedComponent = null;
|
|
73
|
+
inputListeners = new Set();
|
|
74
|
+
/** Global callback for debug key (Shift+Ctrl+D). Called before input is forwarded to focused component. */
|
|
75
|
+
onDebug;
|
|
76
|
+
renderRequested = false;
|
|
77
|
+
cursorRow = 0; // Logical cursor row (end of rendered content)
|
|
78
|
+
hardwareCursorRow = 0; // Actual terminal cursor row (may differ due to IME positioning)
|
|
79
|
+
inputBuffer = ""; // Buffer for parsing terminal responses
|
|
80
|
+
cellSizeQueryPending = false;
|
|
81
|
+
showHardwareCursor = process.env.PI_HARDWARE_CURSOR === "1";
|
|
82
|
+
clearOnShrink = process.env.PI_CLEAR_ON_SHRINK === "1"; // Clear empty rows when content shrinks (default: off)
|
|
83
|
+
maxLinesRendered = 0; // Track terminal's working area (max lines ever rendered)
|
|
84
|
+
previousViewportTop = 0; // Track previous viewport top for resize-aware cursor moves
|
|
85
|
+
fullRedrawCount = 0;
|
|
86
|
+
stopped = false;
|
|
87
|
+
// Overlay stack for modal components rendered on top of base content
|
|
88
|
+
overlayStack = [];
|
|
89
|
+
constructor(terminal, showHardwareCursor) {
|
|
90
|
+
super();
|
|
91
|
+
this.terminal = terminal;
|
|
92
|
+
if (showHardwareCursor !== undefined) {
|
|
93
|
+
this.showHardwareCursor = showHardwareCursor;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
get fullRedraws() {
|
|
97
|
+
return this.fullRedrawCount;
|
|
98
|
+
}
|
|
99
|
+
getShowHardwareCursor() {
|
|
100
|
+
return this.showHardwareCursor;
|
|
101
|
+
}
|
|
102
|
+
setShowHardwareCursor(enabled) {
|
|
103
|
+
if (this.showHardwareCursor === enabled)
|
|
104
|
+
return;
|
|
105
|
+
this.showHardwareCursor = enabled;
|
|
106
|
+
if (!enabled) {
|
|
107
|
+
this.terminal.hideCursor();
|
|
108
|
+
}
|
|
109
|
+
this.requestRender();
|
|
110
|
+
}
|
|
111
|
+
getClearOnShrink() {
|
|
112
|
+
return this.clearOnShrink;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Set whether to trigger full re-render when content shrinks.
|
|
116
|
+
* When true (default), empty rows are cleared when content shrinks.
|
|
117
|
+
* When false, empty rows remain (reduces redraws on slower terminals).
|
|
118
|
+
*/
|
|
119
|
+
setClearOnShrink(enabled) {
|
|
120
|
+
this.clearOnShrink = enabled;
|
|
121
|
+
}
|
|
122
|
+
setFocus(component) {
|
|
123
|
+
// Clear focused flag on old component
|
|
124
|
+
if (isFocusable(this.focusedComponent)) {
|
|
125
|
+
this.focusedComponent.focused = false;
|
|
126
|
+
}
|
|
127
|
+
this.focusedComponent = component;
|
|
128
|
+
// Set focused flag on new component
|
|
129
|
+
if (isFocusable(component)) {
|
|
130
|
+
component.focused = true;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Show an overlay component with configurable positioning and sizing.
|
|
135
|
+
* Returns a handle to control the overlay's visibility.
|
|
136
|
+
*/
|
|
137
|
+
showOverlay(component, options) {
|
|
138
|
+
const entry = { component, options, preFocus: this.focusedComponent, hidden: false };
|
|
139
|
+
this.overlayStack.push(entry);
|
|
140
|
+
// Only focus if overlay is actually visible
|
|
141
|
+
if (this.isOverlayVisible(entry)) {
|
|
142
|
+
this.setFocus(component);
|
|
143
|
+
}
|
|
144
|
+
this.terminal.hideCursor();
|
|
145
|
+
this.requestRender();
|
|
146
|
+
// Return handle for controlling this overlay
|
|
147
|
+
return {
|
|
148
|
+
hide: () => {
|
|
149
|
+
const index = this.overlayStack.indexOf(entry);
|
|
150
|
+
if (index !== -1) {
|
|
151
|
+
this.overlayStack.splice(index, 1);
|
|
152
|
+
// Restore focus if this overlay had focus
|
|
153
|
+
if (this.focusedComponent === component) {
|
|
154
|
+
const topVisible = this.getTopmostVisibleOverlay();
|
|
155
|
+
this.setFocus(topVisible?.component ?? entry.preFocus);
|
|
156
|
+
}
|
|
157
|
+
if (this.overlayStack.length === 0)
|
|
158
|
+
this.terminal.hideCursor();
|
|
159
|
+
this.requestRender();
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
setHidden: (hidden) => {
|
|
163
|
+
if (entry.hidden === hidden)
|
|
164
|
+
return;
|
|
165
|
+
entry.hidden = hidden;
|
|
166
|
+
// Update focus when hiding/showing
|
|
167
|
+
if (hidden) {
|
|
168
|
+
// If this overlay had focus, move focus to next visible or preFocus
|
|
169
|
+
if (this.focusedComponent === component) {
|
|
170
|
+
const topVisible = this.getTopmostVisibleOverlay();
|
|
171
|
+
this.setFocus(topVisible?.component ?? entry.preFocus);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
// Restore focus to this overlay when showing (if it's actually visible)
|
|
176
|
+
if (this.isOverlayVisible(entry)) {
|
|
177
|
+
this.setFocus(component);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
this.requestRender();
|
|
181
|
+
},
|
|
182
|
+
isHidden: () => entry.hidden,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
/** Hide the topmost overlay and restore previous focus. */
|
|
186
|
+
hideOverlay() {
|
|
187
|
+
const overlay = this.overlayStack.pop();
|
|
188
|
+
if (!overlay)
|
|
189
|
+
return;
|
|
190
|
+
// Find topmost visible overlay, or fall back to preFocus
|
|
191
|
+
const topVisible = this.getTopmostVisibleOverlay();
|
|
192
|
+
this.setFocus(topVisible?.component ?? overlay.preFocus);
|
|
193
|
+
if (this.overlayStack.length === 0)
|
|
194
|
+
this.terminal.hideCursor();
|
|
195
|
+
this.requestRender();
|
|
196
|
+
}
|
|
197
|
+
/** Check if there are any visible overlays */
|
|
198
|
+
hasOverlay() {
|
|
199
|
+
return this.overlayStack.some((o) => this.isOverlayVisible(o));
|
|
200
|
+
}
|
|
201
|
+
/** Check if an overlay entry is currently visible */
|
|
202
|
+
isOverlayVisible(entry) {
|
|
203
|
+
if (entry.hidden)
|
|
204
|
+
return false;
|
|
205
|
+
if (entry.options?.visible) {
|
|
206
|
+
return entry.options.visible(this.terminal.columns, this.terminal.rows);
|
|
207
|
+
}
|
|
208
|
+
return true;
|
|
209
|
+
}
|
|
210
|
+
/** Find the topmost visible overlay, if any */
|
|
211
|
+
getTopmostVisibleOverlay() {
|
|
212
|
+
for (let i = this.overlayStack.length - 1; i >= 0; i--) {
|
|
213
|
+
if (this.isOverlayVisible(this.overlayStack[i])) {
|
|
214
|
+
return this.overlayStack[i];
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return undefined;
|
|
218
|
+
}
|
|
219
|
+
invalidate() {
|
|
220
|
+
super.invalidate();
|
|
221
|
+
for (const overlay of this.overlayStack)
|
|
222
|
+
overlay.component.invalidate?.();
|
|
223
|
+
}
|
|
224
|
+
start() {
|
|
225
|
+
this.stopped = false;
|
|
226
|
+
this.terminal.start((data) => this.handleInput(data), () => this.requestRender());
|
|
227
|
+
this.terminal.hideCursor();
|
|
228
|
+
this.queryCellSize();
|
|
229
|
+
this.requestRender();
|
|
230
|
+
}
|
|
231
|
+
addInputListener(listener) {
|
|
232
|
+
this.inputListeners.add(listener);
|
|
233
|
+
return () => {
|
|
234
|
+
this.inputListeners.delete(listener);
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
removeInputListener(listener) {
|
|
238
|
+
this.inputListeners.delete(listener);
|
|
239
|
+
}
|
|
240
|
+
queryCellSize() {
|
|
241
|
+
// Only query if terminal supports images (cell size is only used for image rendering)
|
|
242
|
+
if (!getCapabilities().images) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
// Query terminal for cell size in pixels: CSI 16 t
|
|
246
|
+
// Response format: CSI 6 ; height ; width t
|
|
247
|
+
this.cellSizeQueryPending = true;
|
|
248
|
+
this.terminal.write("\x1b[16t");
|
|
249
|
+
}
|
|
250
|
+
stop() {
|
|
251
|
+
this.stopped = true;
|
|
252
|
+
// Move cursor to the end of the content to prevent overwriting/artifacts on exit
|
|
253
|
+
if (this.previousLines.length > 0) {
|
|
254
|
+
const targetRow = this.previousLines.length; // Line after the last content
|
|
255
|
+
const lineDiff = targetRow - this.hardwareCursorRow;
|
|
256
|
+
if (lineDiff > 0) {
|
|
257
|
+
this.terminal.write(`\x1b[${lineDiff}B`);
|
|
258
|
+
}
|
|
259
|
+
else if (lineDiff < 0) {
|
|
260
|
+
this.terminal.write(`\x1b[${-lineDiff}A`);
|
|
261
|
+
}
|
|
262
|
+
this.terminal.write("\r\n");
|
|
263
|
+
}
|
|
264
|
+
this.terminal.showCursor();
|
|
265
|
+
this.terminal.stop();
|
|
266
|
+
}
|
|
267
|
+
requestRender(force = false) {
|
|
268
|
+
if (force) {
|
|
269
|
+
this.previousLines = [];
|
|
270
|
+
this.previousWidth = -1; // -1 triggers widthChanged, forcing a full clear
|
|
271
|
+
this.cursorRow = 0;
|
|
272
|
+
this.hardwareCursorRow = 0;
|
|
273
|
+
this.maxLinesRendered = 0;
|
|
274
|
+
this.previousViewportTop = 0;
|
|
275
|
+
}
|
|
276
|
+
if (this.renderRequested)
|
|
277
|
+
return;
|
|
278
|
+
this.renderRequested = true;
|
|
279
|
+
process.nextTick(() => {
|
|
280
|
+
this.renderRequested = false;
|
|
281
|
+
this.doRender();
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
handleInput(data) {
|
|
285
|
+
if (this.inputListeners.size > 0) {
|
|
286
|
+
let current = data;
|
|
287
|
+
for (const listener of this.inputListeners) {
|
|
288
|
+
const result = listener(current);
|
|
289
|
+
if (result?.consume) {
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
if (result?.data !== undefined) {
|
|
293
|
+
current = result.data;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
if (current.length === 0) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
data = current;
|
|
300
|
+
}
|
|
301
|
+
// If we're waiting for cell size response, buffer input and parse
|
|
302
|
+
if (this.cellSizeQueryPending) {
|
|
303
|
+
this.inputBuffer += data;
|
|
304
|
+
const filtered = this.parseCellSizeResponse();
|
|
305
|
+
if (filtered.length === 0)
|
|
306
|
+
return;
|
|
307
|
+
data = filtered;
|
|
308
|
+
}
|
|
309
|
+
// Global debug key handler (Shift+Ctrl+D)
|
|
310
|
+
if (matchesKey(data, "shift+ctrl+d") && this.onDebug) {
|
|
311
|
+
this.onDebug();
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
// If focused component is an overlay, verify it's still visible
|
|
315
|
+
// (visibility can change due to terminal resize or visible() callback)
|
|
316
|
+
const focusedOverlay = this.overlayStack.find((o) => o.component === this.focusedComponent);
|
|
317
|
+
if (focusedOverlay && !this.isOverlayVisible(focusedOverlay)) {
|
|
318
|
+
// Focused overlay is no longer visible, redirect to topmost visible overlay
|
|
319
|
+
const topVisible = this.getTopmostVisibleOverlay();
|
|
320
|
+
if (topVisible) {
|
|
321
|
+
this.setFocus(topVisible.component);
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
// No visible overlays, restore to preFocus
|
|
325
|
+
this.setFocus(focusedOverlay.preFocus);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
// Pass input to focused component (including Ctrl+C)
|
|
329
|
+
// The focused component can decide how to handle Ctrl+C
|
|
330
|
+
if (this.focusedComponent?.handleInput) {
|
|
331
|
+
// Filter out key release events unless component opts in
|
|
332
|
+
if (isKeyRelease(data) && !this.focusedComponent.wantsKeyRelease) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
this.focusedComponent.handleInput(data);
|
|
336
|
+
this.requestRender();
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
parseCellSizeResponse() {
|
|
340
|
+
// Response format: ESC [ 6 ; height ; width t
|
|
341
|
+
// Match the response pattern
|
|
342
|
+
const responsePattern = /\x1b\[6;(\d+);(\d+)t/;
|
|
343
|
+
const match = this.inputBuffer.match(responsePattern);
|
|
344
|
+
if (match) {
|
|
345
|
+
const heightPx = parseInt(match[1], 10);
|
|
346
|
+
const widthPx = parseInt(match[2], 10);
|
|
347
|
+
if (heightPx > 0 && widthPx > 0) {
|
|
348
|
+
setCellDimensions({ widthPx, heightPx });
|
|
349
|
+
// Invalidate all components so images re-render with correct dimensions
|
|
350
|
+
this.invalidate();
|
|
351
|
+
this.requestRender();
|
|
352
|
+
}
|
|
353
|
+
// Remove the response from buffer
|
|
354
|
+
this.inputBuffer = this.inputBuffer.replace(responsePattern, "");
|
|
355
|
+
this.cellSizeQueryPending = false;
|
|
356
|
+
}
|
|
357
|
+
// Check if we have a partial cell size response starting (wait for more data)
|
|
358
|
+
// Patterns that could be incomplete cell size response: \x1b, \x1b[, \x1b[6, \x1b[6;...(no t yet)
|
|
359
|
+
const partialCellSizePattern = /\x1b(\[6?;?[\d;]*)?$/;
|
|
360
|
+
if (partialCellSizePattern.test(this.inputBuffer)) {
|
|
361
|
+
// Check if it's actually a complete different escape sequence (ends with a letter)
|
|
362
|
+
// Cell size response ends with 't', Kitty keyboard ends with 'u', arrows end with A-D, etc.
|
|
363
|
+
const lastChar = this.inputBuffer[this.inputBuffer.length - 1];
|
|
364
|
+
if (!/[a-zA-Z~]/.test(lastChar)) {
|
|
365
|
+
// Doesn't end with a terminator, might be incomplete - wait for more
|
|
366
|
+
return "";
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
// No cell size response found, return buffered data as user input
|
|
370
|
+
const result = this.inputBuffer;
|
|
371
|
+
this.inputBuffer = "";
|
|
372
|
+
this.cellSizeQueryPending = false; // Give up waiting
|
|
373
|
+
return result;
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Resolve overlay layout from options.
|
|
377
|
+
* Returns { width, row, col, maxHeight } for rendering.
|
|
378
|
+
*/
|
|
379
|
+
resolveOverlayLayout(options, overlayHeight, termWidth, termHeight) {
|
|
380
|
+
const opt = options ?? {};
|
|
381
|
+
// Parse margin (clamp to non-negative)
|
|
382
|
+
const margin = typeof opt.margin === "number"
|
|
383
|
+
? { top: opt.margin, right: opt.margin, bottom: opt.margin, left: opt.margin }
|
|
384
|
+
: (opt.margin ?? {});
|
|
385
|
+
const marginTop = Math.max(0, margin.top ?? 0);
|
|
386
|
+
const marginRight = Math.max(0, margin.right ?? 0);
|
|
387
|
+
const marginBottom = Math.max(0, margin.bottom ?? 0);
|
|
388
|
+
const marginLeft = Math.max(0, margin.left ?? 0);
|
|
389
|
+
// Available space after margins
|
|
390
|
+
const availWidth = Math.max(1, termWidth - marginLeft - marginRight);
|
|
391
|
+
const availHeight = Math.max(1, termHeight - marginTop - marginBottom);
|
|
392
|
+
// === Resolve width ===
|
|
393
|
+
let width = parseSizeValue(opt.width, termWidth) ?? Math.min(80, availWidth);
|
|
394
|
+
// Apply minWidth
|
|
395
|
+
if (opt.minWidth !== undefined) {
|
|
396
|
+
width = Math.max(width, opt.minWidth);
|
|
397
|
+
}
|
|
398
|
+
// Clamp to available space
|
|
399
|
+
width = Math.max(1, Math.min(width, availWidth));
|
|
400
|
+
// === Resolve maxHeight ===
|
|
401
|
+
let maxHeight = parseSizeValue(opt.maxHeight, termHeight);
|
|
402
|
+
// Clamp to available space
|
|
403
|
+
if (maxHeight !== undefined) {
|
|
404
|
+
maxHeight = Math.max(1, Math.min(maxHeight, availHeight));
|
|
405
|
+
}
|
|
406
|
+
// Effective overlay height (may be clamped by maxHeight)
|
|
407
|
+
const effectiveHeight = maxHeight !== undefined ? Math.min(overlayHeight, maxHeight) : overlayHeight;
|
|
408
|
+
// === Resolve position ===
|
|
409
|
+
let row;
|
|
410
|
+
let col;
|
|
411
|
+
if (opt.row !== undefined) {
|
|
412
|
+
if (typeof opt.row === "string") {
|
|
413
|
+
// Percentage: 0% = top, 100% = bottom (overlay stays within bounds)
|
|
414
|
+
const match = opt.row.match(/^(\d+(?:\.\d+)?)%$/);
|
|
415
|
+
if (match) {
|
|
416
|
+
const maxRow = Math.max(0, availHeight - effectiveHeight);
|
|
417
|
+
const percent = parseFloat(match[1]) / 100;
|
|
418
|
+
row = marginTop + Math.floor(maxRow * percent);
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
// Invalid format, fall back to center
|
|
422
|
+
row = this.resolveAnchorRow("center", effectiveHeight, availHeight, marginTop);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
// Absolute row position
|
|
427
|
+
row = opt.row;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
// Anchor-based (default: center)
|
|
432
|
+
const anchor = opt.anchor ?? "center";
|
|
433
|
+
row = this.resolveAnchorRow(anchor, effectiveHeight, availHeight, marginTop);
|
|
434
|
+
}
|
|
435
|
+
if (opt.col !== undefined) {
|
|
436
|
+
if (typeof opt.col === "string") {
|
|
437
|
+
// Percentage: 0% = left, 100% = right (overlay stays within bounds)
|
|
438
|
+
const match = opt.col.match(/^(\d+(?:\.\d+)?)%$/);
|
|
439
|
+
if (match) {
|
|
440
|
+
const maxCol = Math.max(0, availWidth - width);
|
|
441
|
+
const percent = parseFloat(match[1]) / 100;
|
|
442
|
+
col = marginLeft + Math.floor(maxCol * percent);
|
|
443
|
+
}
|
|
444
|
+
else {
|
|
445
|
+
// Invalid format, fall back to center
|
|
446
|
+
col = this.resolveAnchorCol("center", width, availWidth, marginLeft);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
else {
|
|
450
|
+
// Absolute column position
|
|
451
|
+
col = opt.col;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
// Anchor-based (default: center)
|
|
456
|
+
const anchor = opt.anchor ?? "center";
|
|
457
|
+
col = this.resolveAnchorCol(anchor, width, availWidth, marginLeft);
|
|
458
|
+
}
|
|
459
|
+
// Apply offsets
|
|
460
|
+
if (opt.offsetY !== undefined)
|
|
461
|
+
row += opt.offsetY;
|
|
462
|
+
if (opt.offsetX !== undefined)
|
|
463
|
+
col += opt.offsetX;
|
|
464
|
+
// Clamp to terminal bounds (respecting margins)
|
|
465
|
+
row = Math.max(marginTop, Math.min(row, termHeight - marginBottom - effectiveHeight));
|
|
466
|
+
col = Math.max(marginLeft, Math.min(col, termWidth - marginRight - width));
|
|
467
|
+
return { width, row, col, maxHeight };
|
|
468
|
+
}
|
|
469
|
+
resolveAnchorRow(anchor, height, availHeight, marginTop) {
|
|
470
|
+
switch (anchor) {
|
|
471
|
+
case "top-left":
|
|
472
|
+
case "top-center":
|
|
473
|
+
case "top-right":
|
|
474
|
+
return marginTop;
|
|
475
|
+
case "bottom-left":
|
|
476
|
+
case "bottom-center":
|
|
477
|
+
case "bottom-right":
|
|
478
|
+
return marginTop + availHeight - height;
|
|
479
|
+
case "left-center":
|
|
480
|
+
case "center":
|
|
481
|
+
case "right-center":
|
|
482
|
+
return marginTop + Math.floor((availHeight - height) / 2);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
resolveAnchorCol(anchor, width, availWidth, marginLeft) {
|
|
486
|
+
switch (anchor) {
|
|
487
|
+
case "top-left":
|
|
488
|
+
case "left-center":
|
|
489
|
+
case "bottom-left":
|
|
490
|
+
return marginLeft;
|
|
491
|
+
case "top-right":
|
|
492
|
+
case "right-center":
|
|
493
|
+
case "bottom-right":
|
|
494
|
+
return marginLeft + availWidth - width;
|
|
495
|
+
case "top-center":
|
|
496
|
+
case "center":
|
|
497
|
+
case "bottom-center":
|
|
498
|
+
return marginLeft + Math.floor((availWidth - width) / 2);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
/** Composite all overlays into content lines (in stack order, later = on top). */
|
|
502
|
+
compositeOverlays(lines, termWidth, termHeight) {
|
|
503
|
+
if (this.overlayStack.length === 0)
|
|
504
|
+
return lines;
|
|
505
|
+
const result = [...lines];
|
|
506
|
+
// Pre-render all visible overlays and calculate positions
|
|
507
|
+
const rendered = [];
|
|
508
|
+
let minLinesNeeded = result.length;
|
|
509
|
+
for (const entry of this.overlayStack) {
|
|
510
|
+
// Skip invisible overlays (hidden or visible() returns false)
|
|
511
|
+
if (!this.isOverlayVisible(entry))
|
|
512
|
+
continue;
|
|
513
|
+
const { component, options } = entry;
|
|
514
|
+
// Get layout with height=0 first to determine width and maxHeight
|
|
515
|
+
// (width and maxHeight don't depend on overlay height)
|
|
516
|
+
const { width, maxHeight } = this.resolveOverlayLayout(options, 0, termWidth, termHeight);
|
|
517
|
+
// Render component at calculated width
|
|
518
|
+
let overlayLines = component.render(width);
|
|
519
|
+
// Apply maxHeight if specified
|
|
520
|
+
if (maxHeight !== undefined && overlayLines.length > maxHeight) {
|
|
521
|
+
overlayLines = overlayLines.slice(0, maxHeight);
|
|
522
|
+
}
|
|
523
|
+
// Get final row/col with actual overlay height
|
|
524
|
+
const { row, col } = this.resolveOverlayLayout(options, overlayLines.length, termWidth, termHeight);
|
|
525
|
+
rendered.push({ overlayLines, row, col, w: width });
|
|
526
|
+
minLinesNeeded = Math.max(minLinesNeeded, row + overlayLines.length);
|
|
527
|
+
}
|
|
528
|
+
// Ensure result covers the terminal working area to keep overlay positioning stable across resizes.
|
|
529
|
+
// maxLinesRendered can exceed current content length after a shrink; pad to keep viewportStart consistent.
|
|
530
|
+
const workingHeight = Math.max(this.maxLinesRendered, minLinesNeeded);
|
|
531
|
+
// Extend result with empty lines if content is too short for overlay placement or working area
|
|
532
|
+
while (result.length < workingHeight) {
|
|
533
|
+
result.push("");
|
|
534
|
+
}
|
|
535
|
+
const viewportStart = Math.max(0, workingHeight - termHeight);
|
|
536
|
+
// Track which lines were modified for final verification
|
|
537
|
+
const modifiedLines = new Set();
|
|
538
|
+
// Composite each overlay
|
|
539
|
+
for (const { overlayLines, row, col, w } of rendered) {
|
|
540
|
+
for (let i = 0; i < overlayLines.length; i++) {
|
|
541
|
+
const idx = viewportStart + row + i;
|
|
542
|
+
if (idx >= 0 && idx < result.length) {
|
|
543
|
+
// Defensive: truncate overlay line to declared width before compositing
|
|
544
|
+
// (components should already respect width, but this ensures it)
|
|
545
|
+
const truncatedOverlayLine = visibleWidth(overlayLines[i]) > w ? sliceByColumn(overlayLines[i], 0, w, true) : overlayLines[i];
|
|
546
|
+
result[idx] = this.compositeLineAt(result[idx], truncatedOverlayLine, col, w, termWidth);
|
|
547
|
+
modifiedLines.add(idx);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
// Final verification: ensure no composited line exceeds terminal width
|
|
552
|
+
// This is a belt-and-suspenders safeguard - compositeLineAt should already
|
|
553
|
+
// guarantee this, but we verify here to prevent crashes from any edge cases
|
|
554
|
+
// Only check lines that were actually modified (optimization)
|
|
555
|
+
for (const idx of modifiedLines) {
|
|
556
|
+
const lineWidth = visibleWidth(result[idx]);
|
|
557
|
+
if (lineWidth > termWidth) {
|
|
558
|
+
result[idx] = sliceByColumn(result[idx], 0, termWidth, true);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return result;
|
|
562
|
+
}
|
|
563
|
+
static SEGMENT_RESET = "\x1b[0m\x1b]8;;\x07";
|
|
564
|
+
applyLineResets(lines) {
|
|
565
|
+
const reset = TUI.SEGMENT_RESET;
|
|
566
|
+
for (let i = 0; i < lines.length; i++) {
|
|
567
|
+
const line = lines[i];
|
|
568
|
+
if (!isImageLine(line)) {
|
|
569
|
+
lines[i] = line + reset;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
return lines;
|
|
573
|
+
}
|
|
574
|
+
/** Splice overlay content into a base line at a specific column. Single-pass optimized. */
|
|
575
|
+
compositeLineAt(baseLine, overlayLine, startCol, overlayWidth, totalWidth) {
|
|
576
|
+
if (isImageLine(baseLine))
|
|
577
|
+
return baseLine;
|
|
578
|
+
// Single pass through baseLine extracts both before and after segments
|
|
579
|
+
const afterStart = startCol + overlayWidth;
|
|
580
|
+
const base = extractSegments(baseLine, startCol, afterStart, totalWidth - afterStart, true);
|
|
581
|
+
// Extract overlay with width tracking (strict=true to exclude wide chars at boundary)
|
|
582
|
+
const overlay = sliceWithWidth(overlayLine, 0, overlayWidth, true);
|
|
583
|
+
// Pad segments to target widths
|
|
584
|
+
const beforePad = Math.max(0, startCol - base.beforeWidth);
|
|
585
|
+
const overlayPad = Math.max(0, overlayWidth - overlay.width);
|
|
586
|
+
const actualBeforeWidth = Math.max(startCol, base.beforeWidth);
|
|
587
|
+
const actualOverlayWidth = Math.max(overlayWidth, overlay.width);
|
|
588
|
+
const afterTarget = Math.max(0, totalWidth - actualBeforeWidth - actualOverlayWidth);
|
|
589
|
+
const afterPad = Math.max(0, afterTarget - base.afterWidth);
|
|
590
|
+
// Compose result
|
|
591
|
+
const r = TUI.SEGMENT_RESET;
|
|
592
|
+
const result = base.before +
|
|
593
|
+
" ".repeat(beforePad) +
|
|
594
|
+
r +
|
|
595
|
+
overlay.text +
|
|
596
|
+
" ".repeat(overlayPad) +
|
|
597
|
+
r +
|
|
598
|
+
base.after +
|
|
599
|
+
" ".repeat(afterPad);
|
|
600
|
+
// CRITICAL: Always verify and truncate to terminal width.
|
|
601
|
+
// This is the final safeguard against width overflow which would crash the TUI.
|
|
602
|
+
// Width tracking can drift from actual visible width due to:
|
|
603
|
+
// - Complex ANSI/OSC sequences (hyperlinks, colors)
|
|
604
|
+
// - Wide characters at segment boundaries
|
|
605
|
+
// - Edge cases in segment extraction
|
|
606
|
+
const resultWidth = visibleWidth(result);
|
|
607
|
+
if (resultWidth <= totalWidth) {
|
|
608
|
+
return result;
|
|
609
|
+
}
|
|
610
|
+
// Truncate with strict=true to ensure we don't exceed totalWidth
|
|
611
|
+
return sliceByColumn(result, 0, totalWidth, true);
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Find and extract cursor position from rendered lines.
|
|
615
|
+
* Searches for CURSOR_MARKER, calculates its position, and strips it from the output.
|
|
616
|
+
* Only scans the bottom terminal height lines (visible viewport).
|
|
617
|
+
* @param lines - Rendered lines to search
|
|
618
|
+
* @param height - Terminal height (visible viewport size)
|
|
619
|
+
* @returns Cursor position { row, col } or null if no marker found
|
|
620
|
+
*/
|
|
621
|
+
extractCursorPosition(lines, height) {
|
|
622
|
+
// Only scan the bottom `height` lines (visible viewport)
|
|
623
|
+
const viewportTop = Math.max(0, lines.length - height);
|
|
624
|
+
for (let row = lines.length - 1; row >= viewportTop; row--) {
|
|
625
|
+
const line = lines[row];
|
|
626
|
+
const markerIndex = line.indexOf(CURSOR_MARKER);
|
|
627
|
+
if (markerIndex !== -1) {
|
|
628
|
+
// Calculate visual column (width of text before marker)
|
|
629
|
+
const beforeMarker = line.slice(0, markerIndex);
|
|
630
|
+
const col = visibleWidth(beforeMarker);
|
|
631
|
+
// Strip marker from the line
|
|
632
|
+
lines[row] = line.slice(0, markerIndex) + line.slice(markerIndex + CURSOR_MARKER.length);
|
|
633
|
+
return { row, col };
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
return null;
|
|
637
|
+
}
|
|
638
|
+
doRender() {
|
|
639
|
+
if (this.stopped)
|
|
640
|
+
return;
|
|
641
|
+
const width = this.terminal.columns;
|
|
642
|
+
const height = this.terminal.rows;
|
|
643
|
+
let viewportTop = Math.max(0, this.maxLinesRendered - height);
|
|
644
|
+
let prevViewportTop = this.previousViewportTop;
|
|
645
|
+
let hardwareCursorRow = this.hardwareCursorRow;
|
|
646
|
+
const computeLineDiff = (targetRow) => {
|
|
647
|
+
const currentScreenRow = hardwareCursorRow - prevViewportTop;
|
|
648
|
+
const targetScreenRow = targetRow - viewportTop;
|
|
649
|
+
return targetScreenRow - currentScreenRow;
|
|
650
|
+
};
|
|
651
|
+
// Render all components to get new lines
|
|
652
|
+
let newLines = this.render(width);
|
|
653
|
+
// Composite overlays into the rendered lines (before differential compare)
|
|
654
|
+
if (this.overlayStack.length > 0) {
|
|
655
|
+
newLines = this.compositeOverlays(newLines, width, height);
|
|
656
|
+
}
|
|
657
|
+
// Extract cursor position before applying line resets (marker must be found first)
|
|
658
|
+
const cursorPos = this.extractCursorPosition(newLines, height);
|
|
659
|
+
newLines = this.applyLineResets(newLines);
|
|
660
|
+
// Width changed - need full re-render (line wrapping changes)
|
|
661
|
+
const widthChanged = this.previousWidth !== 0 && this.previousWidth !== width;
|
|
662
|
+
// Helper to clear scrollback and viewport and render all new lines
|
|
663
|
+
const fullRender = (clear) => {
|
|
664
|
+
this.fullRedrawCount += 1;
|
|
665
|
+
let buffer = "\x1b[?2026h"; // Begin synchronized output
|
|
666
|
+
if (clear)
|
|
667
|
+
buffer += "\x1b[3J\x1b[2J\x1b[H"; // Clear scrollback, screen, and home
|
|
668
|
+
for (let i = 0; i < newLines.length; i++) {
|
|
669
|
+
if (i > 0)
|
|
670
|
+
buffer += "\r\n";
|
|
671
|
+
buffer += newLines[i];
|
|
672
|
+
}
|
|
673
|
+
buffer += "\x1b[?2026l"; // End synchronized output
|
|
674
|
+
this.terminal.write(buffer);
|
|
675
|
+
this.cursorRow = Math.max(0, newLines.length - 1);
|
|
676
|
+
this.hardwareCursorRow = this.cursorRow;
|
|
677
|
+
// Reset max lines when clearing, otherwise track growth
|
|
678
|
+
if (clear) {
|
|
679
|
+
this.maxLinesRendered = newLines.length;
|
|
680
|
+
}
|
|
681
|
+
else {
|
|
682
|
+
this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);
|
|
683
|
+
}
|
|
684
|
+
this.previousViewportTop = Math.max(0, this.maxLinesRendered - height);
|
|
685
|
+
this.positionHardwareCursor(cursorPos, newLines.length);
|
|
686
|
+
this.previousLines = newLines;
|
|
687
|
+
this.previousWidth = width;
|
|
688
|
+
};
|
|
689
|
+
const debugRedraw = process.env.PI_DEBUG_REDRAW === "1";
|
|
690
|
+
const logRedraw = (reason) => {
|
|
691
|
+
if (!debugRedraw)
|
|
692
|
+
return;
|
|
693
|
+
const logPath = path.join(os.homedir(), ".pi", "agent", "pi-debug.log");
|
|
694
|
+
const msg = `[${new Date().toISOString()}] fullRender: ${reason} (prev=${this.previousLines.length}, new=${newLines.length}, height=${height})\n`;
|
|
695
|
+
fs.appendFileSync(logPath, msg);
|
|
696
|
+
};
|
|
697
|
+
// First render - just output everything without clearing (assumes clean screen)
|
|
698
|
+
if (this.previousLines.length === 0 && !widthChanged) {
|
|
699
|
+
logRedraw("first render");
|
|
700
|
+
fullRender(false);
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
// Width changed - full re-render (line wrapping changes)
|
|
704
|
+
if (widthChanged) {
|
|
705
|
+
logRedraw(`width changed (${this.previousWidth} -> ${width})`);
|
|
706
|
+
fullRender(true);
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
// Content shrunk below the working area and no overlays - re-render to clear empty rows
|
|
710
|
+
// (overlays need the padding, so only do this when no overlays are active)
|
|
711
|
+
// Configurable via setClearOnShrink() or PI_CLEAR_ON_SHRINK=0 env var
|
|
712
|
+
if (this.clearOnShrink && newLines.length < this.maxLinesRendered && this.overlayStack.length === 0) {
|
|
713
|
+
logRedraw(`clearOnShrink (maxLinesRendered=${this.maxLinesRendered})`);
|
|
714
|
+
fullRender(true);
|
|
715
|
+
return;
|
|
716
|
+
}
|
|
717
|
+
// Find first and last changed lines
|
|
718
|
+
let firstChanged = -1;
|
|
719
|
+
let lastChanged = -1;
|
|
720
|
+
const maxLines = Math.max(newLines.length, this.previousLines.length);
|
|
721
|
+
for (let i = 0; i < maxLines; i++) {
|
|
722
|
+
const oldLine = i < this.previousLines.length ? this.previousLines[i] : "";
|
|
723
|
+
const newLine = i < newLines.length ? newLines[i] : "";
|
|
724
|
+
if (oldLine !== newLine) {
|
|
725
|
+
if (firstChanged === -1) {
|
|
726
|
+
firstChanged = i;
|
|
727
|
+
}
|
|
728
|
+
lastChanged = i;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
const appendedLines = newLines.length > this.previousLines.length;
|
|
732
|
+
if (appendedLines) {
|
|
733
|
+
if (firstChanged === -1) {
|
|
734
|
+
firstChanged = this.previousLines.length;
|
|
735
|
+
}
|
|
736
|
+
lastChanged = newLines.length - 1;
|
|
737
|
+
}
|
|
738
|
+
const appendStart = appendedLines && firstChanged === this.previousLines.length && firstChanged > 0;
|
|
739
|
+
// No changes - but still need to update hardware cursor position if it moved
|
|
740
|
+
if (firstChanged === -1) {
|
|
741
|
+
this.positionHardwareCursor(cursorPos, newLines.length);
|
|
742
|
+
this.previousViewportTop = Math.max(0, this.maxLinesRendered - height);
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
// All changes are in deleted lines (nothing to render, just clear)
|
|
746
|
+
if (firstChanged >= newLines.length) {
|
|
747
|
+
if (this.previousLines.length > newLines.length) {
|
|
748
|
+
let buffer = "\x1b[?2026h";
|
|
749
|
+
// Move to end of new content (clamp to 0 for empty content)
|
|
750
|
+
const targetRow = Math.max(0, newLines.length - 1);
|
|
751
|
+
const lineDiff = computeLineDiff(targetRow);
|
|
752
|
+
if (lineDiff > 0)
|
|
753
|
+
buffer += `\x1b[${lineDiff}B`;
|
|
754
|
+
else if (lineDiff < 0)
|
|
755
|
+
buffer += `\x1b[${-lineDiff}A`;
|
|
756
|
+
buffer += "\r";
|
|
757
|
+
// Clear extra lines without scrolling
|
|
758
|
+
const extraLines = this.previousLines.length - newLines.length;
|
|
759
|
+
if (extraLines > height) {
|
|
760
|
+
logRedraw(`extraLines > height (${extraLines} > ${height})`);
|
|
761
|
+
fullRender(true);
|
|
762
|
+
return;
|
|
763
|
+
}
|
|
764
|
+
if (extraLines > 0) {
|
|
765
|
+
buffer += "\x1b[1B";
|
|
766
|
+
}
|
|
767
|
+
for (let i = 0; i < extraLines; i++) {
|
|
768
|
+
buffer += "\r\x1b[2K";
|
|
769
|
+
if (i < extraLines - 1)
|
|
770
|
+
buffer += "\x1b[1B";
|
|
771
|
+
}
|
|
772
|
+
if (extraLines > 0) {
|
|
773
|
+
buffer += `\x1b[${extraLines}A`;
|
|
774
|
+
}
|
|
775
|
+
buffer += "\x1b[?2026l";
|
|
776
|
+
this.terminal.write(buffer);
|
|
777
|
+
this.cursorRow = targetRow;
|
|
778
|
+
this.hardwareCursorRow = targetRow;
|
|
779
|
+
}
|
|
780
|
+
this.positionHardwareCursor(cursorPos, newLines.length);
|
|
781
|
+
this.previousLines = newLines;
|
|
782
|
+
this.previousWidth = width;
|
|
783
|
+
this.previousViewportTop = Math.max(0, this.maxLinesRendered - height);
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
786
|
+
// Check if firstChanged is above what was previously visible
|
|
787
|
+
// Use previousLines.length (not maxLinesRendered) to avoid false positives after content shrinks
|
|
788
|
+
const previousContentViewportTop = Math.max(0, this.previousLines.length - height);
|
|
789
|
+
if (firstChanged < previousContentViewportTop) {
|
|
790
|
+
// First change is above previous viewport - need full re-render
|
|
791
|
+
logRedraw(`firstChanged < viewportTop (${firstChanged} < ${previousContentViewportTop})`);
|
|
792
|
+
fullRender(true);
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
795
|
+
// Render from first changed line to end
|
|
796
|
+
// Build buffer with all updates wrapped in synchronized output
|
|
797
|
+
let buffer = "\x1b[?2026h"; // Begin synchronized output
|
|
798
|
+
const prevViewportBottom = prevViewportTop + height - 1;
|
|
799
|
+
const moveTargetRow = appendStart ? firstChanged - 1 : firstChanged;
|
|
800
|
+
if (moveTargetRow > prevViewportBottom) {
|
|
801
|
+
const currentScreenRow = Math.max(0, Math.min(height - 1, hardwareCursorRow - prevViewportTop));
|
|
802
|
+
const moveToBottom = height - 1 - currentScreenRow;
|
|
803
|
+
if (moveToBottom > 0) {
|
|
804
|
+
buffer += `\x1b[${moveToBottom}B`;
|
|
805
|
+
}
|
|
806
|
+
const scroll = moveTargetRow - prevViewportBottom;
|
|
807
|
+
buffer += "\r\n".repeat(scroll);
|
|
808
|
+
prevViewportTop += scroll;
|
|
809
|
+
viewportTop += scroll;
|
|
810
|
+
hardwareCursorRow = moveTargetRow;
|
|
811
|
+
}
|
|
812
|
+
// Move cursor to first changed line (use hardwareCursorRow for actual position)
|
|
813
|
+
const lineDiff = computeLineDiff(moveTargetRow);
|
|
814
|
+
if (lineDiff > 0) {
|
|
815
|
+
buffer += `\x1b[${lineDiff}B`; // Move down
|
|
816
|
+
}
|
|
817
|
+
else if (lineDiff < 0) {
|
|
818
|
+
buffer += `\x1b[${-lineDiff}A`; // Move up
|
|
819
|
+
}
|
|
820
|
+
buffer += appendStart ? "\r\n" : "\r"; // Move to column 0
|
|
821
|
+
// Only render changed lines (firstChanged to lastChanged), not all lines to end
|
|
822
|
+
// This reduces flicker when only a single line changes (e.g., spinner animation)
|
|
823
|
+
const renderEnd = Math.min(lastChanged, newLines.length - 1);
|
|
824
|
+
for (let i = firstChanged; i <= renderEnd; i++) {
|
|
825
|
+
if (i > firstChanged)
|
|
826
|
+
buffer += "\r\n";
|
|
827
|
+
buffer += "\x1b[2K"; // Clear current line
|
|
828
|
+
const line = newLines[i];
|
|
829
|
+
const isImage = isImageLine(line);
|
|
830
|
+
if (!isImage && visibleWidth(line) > width) {
|
|
831
|
+
// Log all lines to crash file for debugging
|
|
832
|
+
const crashLogPath = path.join(os.homedir(), ".pi", "agent", "pi-crash.log");
|
|
833
|
+
const crashData = [
|
|
834
|
+
`Crash at ${new Date().toISOString()}`,
|
|
835
|
+
`Terminal width: ${width}`,
|
|
836
|
+
`Line ${i} visible width: ${visibleWidth(line)}`,
|
|
837
|
+
"",
|
|
838
|
+
"=== All rendered lines ===",
|
|
839
|
+
...newLines.map((l, idx) => `[${idx}] (w=${visibleWidth(l)}) ${l}`),
|
|
840
|
+
"",
|
|
841
|
+
].join("\n");
|
|
842
|
+
fs.mkdirSync(path.dirname(crashLogPath), { recursive: true });
|
|
843
|
+
fs.writeFileSync(crashLogPath, crashData);
|
|
844
|
+
// Clean up terminal state before throwing
|
|
845
|
+
this.stop();
|
|
846
|
+
const errorMsg = [
|
|
847
|
+
`Rendered line ${i} exceeds terminal width (${visibleWidth(line)} > ${width}).`,
|
|
848
|
+
"",
|
|
849
|
+
"This is likely caused by a custom TUI component not truncating its output.",
|
|
850
|
+
"Use visibleWidth() to measure and truncateToWidth() to truncate lines.",
|
|
851
|
+
"",
|
|
852
|
+
`Debug log written to: ${crashLogPath}`,
|
|
853
|
+
].join("\n");
|
|
854
|
+
throw new Error(errorMsg);
|
|
855
|
+
}
|
|
856
|
+
buffer += line;
|
|
857
|
+
}
|
|
858
|
+
// Track where cursor ended up after rendering
|
|
859
|
+
let finalCursorRow = renderEnd;
|
|
860
|
+
// If we had more lines before, clear them and move cursor back
|
|
861
|
+
if (this.previousLines.length > newLines.length) {
|
|
862
|
+
// Move to end of new content first if we stopped before it
|
|
863
|
+
if (renderEnd < newLines.length - 1) {
|
|
864
|
+
const moveDown = newLines.length - 1 - renderEnd;
|
|
865
|
+
buffer += `\x1b[${moveDown}B`;
|
|
866
|
+
finalCursorRow = newLines.length - 1;
|
|
867
|
+
}
|
|
868
|
+
const extraLines = this.previousLines.length - newLines.length;
|
|
869
|
+
for (let i = newLines.length; i < this.previousLines.length; i++) {
|
|
870
|
+
buffer += "\r\n\x1b[2K";
|
|
871
|
+
}
|
|
872
|
+
// Move cursor back to end of new content
|
|
873
|
+
buffer += `\x1b[${extraLines}A`;
|
|
874
|
+
}
|
|
875
|
+
buffer += "\x1b[?2026l"; // End synchronized output
|
|
876
|
+
if (process.env.PI_TUI_DEBUG === "1") {
|
|
877
|
+
const debugDir = "/tmp/tui";
|
|
878
|
+
fs.mkdirSync(debugDir, { recursive: true });
|
|
879
|
+
const debugPath = path.join(debugDir, `render-${Date.now()}-${Math.random().toString(36).slice(2)}.log`);
|
|
880
|
+
const debugData = [
|
|
881
|
+
`firstChanged: ${firstChanged}`,
|
|
882
|
+
`viewportTop: ${viewportTop}`,
|
|
883
|
+
`cursorRow: ${this.cursorRow}`,
|
|
884
|
+
`height: ${height}`,
|
|
885
|
+
`lineDiff: ${lineDiff}`,
|
|
886
|
+
`hardwareCursorRow: ${hardwareCursorRow}`,
|
|
887
|
+
`renderEnd: ${renderEnd}`,
|
|
888
|
+
`finalCursorRow: ${finalCursorRow}`,
|
|
889
|
+
`cursorPos: ${JSON.stringify(cursorPos)}`,
|
|
890
|
+
`newLines.length: ${newLines.length}`,
|
|
891
|
+
`previousLines.length: ${this.previousLines.length}`,
|
|
892
|
+
"",
|
|
893
|
+
"=== newLines ===",
|
|
894
|
+
JSON.stringify(newLines, null, 2),
|
|
895
|
+
"",
|
|
896
|
+
"=== previousLines ===",
|
|
897
|
+
JSON.stringify(this.previousLines, null, 2),
|
|
898
|
+
"",
|
|
899
|
+
"=== buffer ===",
|
|
900
|
+
JSON.stringify(buffer),
|
|
901
|
+
].join("\n");
|
|
902
|
+
fs.writeFileSync(debugPath, debugData);
|
|
903
|
+
}
|
|
904
|
+
// Write entire buffer at once
|
|
905
|
+
this.terminal.write(buffer);
|
|
906
|
+
// Track cursor position for next render
|
|
907
|
+
// cursorRow tracks end of content (for viewport calculation)
|
|
908
|
+
// hardwareCursorRow tracks actual terminal cursor position (for movement)
|
|
909
|
+
this.cursorRow = Math.max(0, newLines.length - 1);
|
|
910
|
+
this.hardwareCursorRow = finalCursorRow;
|
|
911
|
+
// Track terminal's working area (grows but doesn't shrink unless cleared)
|
|
912
|
+
this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);
|
|
913
|
+
this.previousViewportTop = Math.max(0, this.maxLinesRendered - height);
|
|
914
|
+
// Position hardware cursor for IME
|
|
915
|
+
this.positionHardwareCursor(cursorPos, newLines.length);
|
|
916
|
+
this.previousLines = newLines;
|
|
917
|
+
this.previousWidth = width;
|
|
918
|
+
}
|
|
919
|
+
/**
|
|
920
|
+
* Position the hardware cursor for IME candidate window.
|
|
921
|
+
* @param cursorPos The cursor position extracted from rendered output, or null
|
|
922
|
+
* @param totalLines Total number of rendered lines
|
|
923
|
+
*/
|
|
924
|
+
positionHardwareCursor(cursorPos, totalLines) {
|
|
925
|
+
if (!cursorPos || totalLines <= 0) {
|
|
926
|
+
this.terminal.hideCursor();
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
// Clamp cursor position to valid range
|
|
930
|
+
const targetRow = Math.max(0, Math.min(cursorPos.row, totalLines - 1));
|
|
931
|
+
const targetCol = Math.max(0, cursorPos.col);
|
|
932
|
+
// Move cursor from current position to target
|
|
933
|
+
const rowDelta = targetRow - this.hardwareCursorRow;
|
|
934
|
+
let buffer = "";
|
|
935
|
+
if (rowDelta > 0) {
|
|
936
|
+
buffer += `\x1b[${rowDelta}B`; // Move down
|
|
937
|
+
}
|
|
938
|
+
else if (rowDelta < 0) {
|
|
939
|
+
buffer += `\x1b[${-rowDelta}A`; // Move up
|
|
940
|
+
}
|
|
941
|
+
// Move to absolute column (1-indexed)
|
|
942
|
+
buffer += `\x1b[${targetCol + 1}G`;
|
|
943
|
+
if (buffer) {
|
|
944
|
+
this.terminal.write(buffer);
|
|
945
|
+
}
|
|
946
|
+
this.hardwareCursorRow = targetRow;
|
|
947
|
+
if (this.showHardwareCursor) {
|
|
948
|
+
this.terminal.showCursor();
|
|
949
|
+
}
|
|
950
|
+
else {
|
|
951
|
+
this.terminal.hideCursor();
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
//# sourceMappingURL=tui.js.map
|