@earendil-works/pi-tui 0.82.1 → 0.84.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -14
- 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/editor.d.ts.map +1 -1
- package/dist/components/editor.js +11 -0
- 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.d.ts.map +1 -1
- package/dist/components/image.js +3 -2
- package/dist/components/image.js.map +1 -1
- package/dist/components/markdown.d.ts +4 -0
- package/dist/components/markdown.d.ts.map +1 -1
- package/dist/components/markdown.js +128 -2
- package/dist/components/markdown.js.map +1 -1
- package/dist/components/scroll-view.d.ts +51 -0
- package/dist/components/scroll-view.d.ts.map +1 -0
- package/dist/components/scroll-view.js +173 -0
- package/dist/components/scroll-view.js.map +1 -0
- package/dist/components/settings-list.d.ts.map +1 -1
- package/dist/components/settings-list.js +3 -6
- package/dist/components/settings-list.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/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/index.d.ts +9 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -2
- package/dist/index.js.map +1 -1
- package/dist/keybindings.d.ts +44 -4
- package/dist/keybindings.d.ts.map +1 -1
- package/dist/keybindings.js +31 -4
- package/dist/keybindings.js.map +1 -1
- package/dist/latex.d.ts +10 -0
- package/dist/latex.d.ts.map +1 -0
- package/dist/latex.js +1124 -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 +40 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +314 -0
- package/dist/layout.js.map +1 -0
- package/dist/native-modifiers.d.ts.map +1 -1
- package/dist/native-modifiers.js +10 -3
- package/dist/native-modifiers.js.map +1 -1
- package/dist/terminal-colors.d.ts.map +1 -1
- package/dist/terminal-colors.js +1 -1
- package/dist/terminal-colors.js.map +1 -1
- package/dist/terminal-image.d.ts +26 -0
- package/dist/terminal-image.d.ts.map +1 -1
- package/dist/terminal-image.js +153 -5
- package/dist/terminal-image.js.map +1 -1
- package/dist/terminal.d.ts +1 -0
- package/dist/terminal.d.ts.map +1 -1
- package/dist/terminal.js +10 -7
- package/dist/terminal.js.map +1 -1
- package/dist/tui-alt-screen.d.ts +85 -0
- package/dist/tui-alt-screen.d.ts.map +1 -0
- package/dist/tui-alt-screen.js +768 -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 +533 -0
- package/dist/tui-main-screen.js.map +1 -0
- package/dist/tui.d.ts +75 -38
- package/dist/tui.d.ts.map +1 -1
- package/dist/tui.js +98 -573
- package/dist/tui.js.map +1 -1
- package/dist/utils.d.ts +11 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +119 -6
- package/dist/utils.js.map +1 -1
- package/native/darwin/README.md +20 -0
- package/native/darwin/build.sh +62 -0
- package/native/darwin/src/darwin-modifiers.c +70 -0
- package/native/win32/README.md +22 -0
- package/native/win32/build.mjs +229 -0
- package/native/win32/prebuilds/win32-arm64/win32-console-mode.node +0 -0
- package/native/win32/prebuilds/win32-x64/win32-console-mode.node +0 -0
- package/native/win32/src/win32-console-mode.c +119 -0
- package/package.json +9 -1
package/dist/tui.js
CHANGED
|
@@ -1,48 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Minimal TUI implementation with differential rendering
|
|
3
3
|
*/
|
|
4
|
-
import * as fs from "node:fs";
|
|
5
4
|
import * as os from "node:os";
|
|
6
5
|
import * as path from "node:path";
|
|
7
6
|
import { performance } from "node:perf_hooks";
|
|
8
7
|
import { isKeyRelease, matchesKey } from "./keys.js";
|
|
9
8
|
import { isOsc11BackgroundColorResponse, parseOsc11BackgroundColor, parseTerminalColorSchemeReport, } from "./terminal-colors.js";
|
|
10
|
-
import {
|
|
9
|
+
import { getCapabilities, isImageLine, setCellDimensions } from "./terminal-image.js";
|
|
11
10
|
import { extractSegments, normalizeTerminalOutput, sliceByColumn, sliceWithWidth, visibleWidth } from "./utils.js";
|
|
12
|
-
const KITTY_SEQUENCE_PREFIX = "\x1b_G";
|
|
13
|
-
function parseKittyImageHeader(line) {
|
|
14
|
-
const sequenceStart = line.indexOf(KITTY_SEQUENCE_PREFIX);
|
|
15
|
-
if (sequenceStart === -1)
|
|
16
|
-
return undefined;
|
|
17
|
-
const paramsStart = sequenceStart + KITTY_SEQUENCE_PREFIX.length;
|
|
18
|
-
const paramsEnd = line.indexOf(";", paramsStart);
|
|
19
|
-
if (paramsEnd === -1)
|
|
20
|
-
return undefined;
|
|
21
|
-
const ids = [];
|
|
22
|
-
let rows = 1;
|
|
23
|
-
const params = line.slice(paramsStart, paramsEnd);
|
|
24
|
-
for (const param of params.split(",")) {
|
|
25
|
-
const [key, value] = param.split("=", 2);
|
|
26
|
-
if (value === undefined)
|
|
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
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return { ids, rows };
|
|
39
|
-
}
|
|
40
|
-
function extractKittyImageIds(line) {
|
|
41
|
-
return parseKittyImageHeader(line)?.ids ?? [];
|
|
42
|
-
}
|
|
43
|
-
function extractKittyImageRows(line) {
|
|
44
|
-
return parseKittyImageHeader(line)?.rows ?? 1;
|
|
45
|
-
}
|
|
46
11
|
/** Type guard to check if a component implements Focusable */
|
|
47
12
|
export function isFocusable(component) {
|
|
48
13
|
return component !== null && "focused" in component;
|
|
@@ -68,9 +33,6 @@ function parseSizeValue(value, referenceSize) {
|
|
|
68
33
|
}
|
|
69
34
|
return undefined;
|
|
70
35
|
}
|
|
71
|
-
function isTermuxSession() {
|
|
72
|
-
return Boolean(process.env.TERMUX_VERSION);
|
|
73
|
-
}
|
|
74
36
|
/**
|
|
75
37
|
* Container - a component that contains other components
|
|
76
38
|
*/
|
|
@@ -107,26 +69,47 @@ export class Container {
|
|
|
107
69
|
/**
|
|
108
70
|
* TUI - Main class for managing terminal UI with differential rendering
|
|
109
71
|
*/
|
|
110
|
-
|
|
72
|
+
const SEGMENT_RESET = "\x1b[0m\x1b]8;;\x07";
|
|
73
|
+
/** Composite overlay content into a terminal line at a fixed column. */
|
|
74
|
+
export function compositeTuiLine(baseLine, overlayLine, startCol, overlayWidth, totalWidth) {
|
|
75
|
+
if (isImageLine(baseLine))
|
|
76
|
+
return baseLine;
|
|
77
|
+
const afterStart = startCol + overlayWidth;
|
|
78
|
+
const base = extractSegments(baseLine, startCol, afterStart, totalWidth - afterStart, true);
|
|
79
|
+
const overlay = sliceWithWidth(overlayLine, 0, overlayWidth, true);
|
|
80
|
+
const beforePad = Math.max(0, startCol - base.beforeWidth);
|
|
81
|
+
const overlayPad = Math.max(0, overlayWidth - overlay.width);
|
|
82
|
+
const actualBeforeWidth = Math.max(startCol, base.beforeWidth);
|
|
83
|
+
const actualOverlayWidth = Math.max(overlayWidth, overlay.width);
|
|
84
|
+
const afterTarget = Math.max(0, totalWidth - actualBeforeWidth - actualOverlayWidth);
|
|
85
|
+
const afterPad = Math.max(0, afterTarget - base.afterWidth);
|
|
86
|
+
const result = base.before +
|
|
87
|
+
" ".repeat(beforePad) +
|
|
88
|
+
SEGMENT_RESET +
|
|
89
|
+
overlay.text +
|
|
90
|
+
" ".repeat(overlayPad) +
|
|
91
|
+
SEGMENT_RESET +
|
|
92
|
+
base.after +
|
|
93
|
+
" ".repeat(afterPad);
|
|
94
|
+
return visibleWidth(result) <= totalWidth ? result : sliceByColumn(result, 0, totalWidth, true);
|
|
95
|
+
}
|
|
96
|
+
export const VIEWPORT_TUI = Symbol.for("@earendil-works/pi-tui/viewport");
|
|
97
|
+
export function isViewportTUI(tui) {
|
|
98
|
+
return tui[VIEWPORT_TUI] === true;
|
|
99
|
+
}
|
|
100
|
+
export class TuiBase extends Container {
|
|
111
101
|
terminal;
|
|
112
|
-
previousLines = [];
|
|
113
|
-
previousKittyImageIds = new Set();
|
|
114
|
-
previousWidth = 0;
|
|
115
|
-
previousHeight = 0;
|
|
116
102
|
focusedComponent = null;
|
|
117
103
|
inputListeners = new Set();
|
|
118
104
|
/** Global callback for debug key (Shift+Ctrl+D). Called before input is forwarded to focused component. */
|
|
119
105
|
onDebug;
|
|
120
106
|
renderRequested = false;
|
|
107
|
+
immediateRenderScheduled = false;
|
|
121
108
|
renderTimer;
|
|
122
109
|
lastRenderAt = 0;
|
|
123
110
|
static MIN_RENDER_INTERVAL_MS = 16;
|
|
124
|
-
cursorRow = 0; // Logical cursor row (end of rendered content)
|
|
125
|
-
hardwareCursorRow = 0; // Actual terminal cursor row (may differ due to IME positioning)
|
|
126
111
|
showHardwareCursor = process.env.PI_HARDWARE_CURSOR === "1";
|
|
127
|
-
clearOnShrink = process.env.PI_CLEAR_ON_SHRINK === "1";
|
|
128
|
-
maxLinesRendered = 0; // Track terminal's working area (max lines ever rendered)
|
|
129
|
-
previousViewportTop = 0; // Track previous viewport top for resize-aware cursor moves
|
|
112
|
+
clearOnShrink = process.env.PI_CLEAR_ON_SHRINK === "1";
|
|
130
113
|
fullRedrawCount = 0;
|
|
131
114
|
stopped = false;
|
|
132
115
|
pendingOsc11BackgroundReplies = 0;
|
|
@@ -137,6 +120,9 @@ export class TUI extends Container {
|
|
|
137
120
|
// Overlay stack for modal components rendered on top of base content
|
|
138
121
|
focusOrderCounter = 0;
|
|
139
122
|
overlayStack = [];
|
|
123
|
+
get hasOverlayEntries() {
|
|
124
|
+
return this.overlayStack.length > 0;
|
|
125
|
+
}
|
|
140
126
|
overlayFocusRestore = { status: "inactive" };
|
|
141
127
|
constructor(terminal, showHardwareCursor, logDirectory) {
|
|
142
128
|
super();
|
|
@@ -146,6 +132,11 @@ export class TUI extends Container {
|
|
|
146
132
|
this.showHardwareCursor = showHardwareCursor;
|
|
147
133
|
}
|
|
148
134
|
}
|
|
135
|
+
resetRenderState() { }
|
|
136
|
+
beforeTerminalStart() { }
|
|
137
|
+
afterTerminalStart() { }
|
|
138
|
+
beforeTerminalStop(_options) { }
|
|
139
|
+
afterTerminalStop(_options) { }
|
|
149
140
|
get fullRedraws() {
|
|
150
141
|
return this.fullRedrawCount;
|
|
151
142
|
}
|
|
@@ -172,6 +163,9 @@ export class TUI extends Container {
|
|
|
172
163
|
setClearOnShrink(enabled) {
|
|
173
164
|
this.clearOnShrink = enabled;
|
|
174
165
|
}
|
|
166
|
+
getFocusedComponent() {
|
|
167
|
+
return this.focusedComponent;
|
|
168
|
+
}
|
|
175
169
|
setFocus(component) {
|
|
176
170
|
this.setFocusInternal({ component, overlayFocusRestore: "clear" });
|
|
177
171
|
}
|
|
@@ -272,8 +266,11 @@ export class TUI extends Container {
|
|
|
272
266
|
}
|
|
273
267
|
}
|
|
274
268
|
}
|
|
269
|
+
getMountedRoots() {
|
|
270
|
+
return this.children;
|
|
271
|
+
}
|
|
275
272
|
isComponentMounted(component) {
|
|
276
|
-
return this.
|
|
273
|
+
return this.getMountedRoots().some((child) => this.containsComponent(child, component));
|
|
277
274
|
}
|
|
278
275
|
containsComponent(root, target) {
|
|
279
276
|
if (root === target)
|
|
@@ -426,13 +423,16 @@ export class TUI extends Container {
|
|
|
426
423
|
return topmost;
|
|
427
424
|
}
|
|
428
425
|
invalidate() {
|
|
429
|
-
|
|
426
|
+
for (const root of this.getMountedRoots())
|
|
427
|
+
root.invalidate();
|
|
430
428
|
for (const overlay of this.overlayStack)
|
|
431
|
-
overlay.component.invalidate
|
|
429
|
+
overlay.component.invalidate();
|
|
432
430
|
}
|
|
433
431
|
start() {
|
|
434
432
|
this.stopped = false;
|
|
435
|
-
this.
|
|
433
|
+
this.beforeTerminalStart();
|
|
434
|
+
this.terminal.start((data) => this.handleTerminalInput(data), () => this.requestRender());
|
|
435
|
+
this.afterTerminalStart();
|
|
436
436
|
this.terminal.hideCursor();
|
|
437
437
|
if (this.terminalColorSchemeNotificationsEnabled) {
|
|
438
438
|
this.terminal.write("\x1b[?2031h");
|
|
@@ -473,54 +473,29 @@ export class TUI extends Container {
|
|
|
473
473
|
// Response format: CSI 6 ; height ; width t
|
|
474
474
|
this.terminal.write("\x1b[16t");
|
|
475
475
|
}
|
|
476
|
-
stop() {
|
|
476
|
+
stop(options = {}) {
|
|
477
477
|
this.stopped = true;
|
|
478
|
-
|
|
479
|
-
clearTimeout(this.renderTimer);
|
|
480
|
-
this.renderTimer = undefined;
|
|
481
|
-
}
|
|
478
|
+
this.cancelRenderTimer();
|
|
482
479
|
if (this.terminalColorSchemeNotificationsEnabled) {
|
|
483
480
|
this.terminal.write("\x1b[?2031l");
|
|
484
481
|
}
|
|
485
|
-
|
|
486
|
-
if (this.previousLines.length > 0) {
|
|
487
|
-
// Overwrite the inverted cursor with a normal space to clear the artifact
|
|
488
|
-
this.terminal.write(" ");
|
|
489
|
-
const targetRow = this.previousLines.length; // Line after the last content
|
|
490
|
-
const lineDiff = targetRow - this.hardwareCursorRow;
|
|
491
|
-
if (lineDiff > 0) {
|
|
492
|
-
this.terminal.write(`\x1b[${lineDiff}B`);
|
|
493
|
-
}
|
|
494
|
-
else if (lineDiff < 0) {
|
|
495
|
-
this.terminal.write(`\x1b[${-lineDiff}A`);
|
|
496
|
-
}
|
|
497
|
-
this.terminal.write("\r\n");
|
|
498
|
-
}
|
|
482
|
+
this.beforeTerminalStop(options);
|
|
499
483
|
this.terminal.showCursor();
|
|
500
484
|
this.terminal.stop();
|
|
485
|
+
this.afterTerminalStop(options);
|
|
486
|
+
}
|
|
487
|
+
renderNow(force = false) {
|
|
488
|
+
if (force)
|
|
489
|
+
this.resetRenderState();
|
|
490
|
+
this.renderRequested = false;
|
|
491
|
+
this.cancelRenderTimer();
|
|
492
|
+
this.lastRenderAt = performance.now();
|
|
493
|
+
this.doRender();
|
|
501
494
|
}
|
|
502
495
|
requestRender(force = false) {
|
|
503
496
|
if (force) {
|
|
504
|
-
this.
|
|
505
|
-
this.
|
|
506
|
-
this.previousHeight = -1; // -1 triggers heightChanged, forcing a full clear
|
|
507
|
-
this.cursorRow = 0;
|
|
508
|
-
this.hardwareCursorRow = 0;
|
|
509
|
-
this.maxLinesRendered = 0;
|
|
510
|
-
this.previousViewportTop = 0;
|
|
511
|
-
if (this.renderTimer) {
|
|
512
|
-
clearTimeout(this.renderTimer);
|
|
513
|
-
this.renderTimer = undefined;
|
|
514
|
-
}
|
|
515
|
-
this.renderRequested = true;
|
|
516
|
-
process.nextTick(() => {
|
|
517
|
-
if (this.stopped || !this.renderRequested) {
|
|
518
|
-
return;
|
|
519
|
-
}
|
|
520
|
-
this.renderRequested = false;
|
|
521
|
-
this.lastRenderAt = performance.now();
|
|
522
|
-
this.doRender();
|
|
523
|
-
});
|
|
497
|
+
this.resetRenderState();
|
|
498
|
+
this.requestImmediateRender();
|
|
524
499
|
return;
|
|
525
500
|
}
|
|
526
501
|
if (this.renderRequested)
|
|
@@ -528,12 +503,36 @@ export class TUI extends Container {
|
|
|
528
503
|
this.renderRequested = true;
|
|
529
504
|
process.nextTick(() => this.scheduleRender());
|
|
530
505
|
}
|
|
506
|
+
requestImmediateRender() {
|
|
507
|
+
this.cancelRenderTimer();
|
|
508
|
+
this.renderRequested = true;
|
|
509
|
+
if (this.immediateRenderScheduled)
|
|
510
|
+
return;
|
|
511
|
+
this.immediateRenderScheduled = true;
|
|
512
|
+
process.nextTick(() => {
|
|
513
|
+
this.immediateRenderScheduled = false;
|
|
514
|
+
if (this.stopped || !this.renderRequested)
|
|
515
|
+
return;
|
|
516
|
+
// A previously queued scheduleRender() can create a timer before this
|
|
517
|
+
// callback runs. User input must preempt that throttled frame.
|
|
518
|
+
this.cancelRenderTimer();
|
|
519
|
+
this.renderRequested = false;
|
|
520
|
+
this.lastRenderAt = performance.now();
|
|
521
|
+
this.doRender();
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
cancelRenderTimer() {
|
|
525
|
+
if (!this.renderTimer)
|
|
526
|
+
return;
|
|
527
|
+
clearTimeout(this.renderTimer);
|
|
528
|
+
this.renderTimer = undefined;
|
|
529
|
+
}
|
|
531
530
|
scheduleRender() {
|
|
532
531
|
if (this.stopped || this.renderTimer || !this.renderRequested) {
|
|
533
532
|
return;
|
|
534
533
|
}
|
|
535
534
|
const elapsed = performance.now() - this.lastRenderAt;
|
|
536
|
-
const delay = Math.max(0,
|
|
535
|
+
const delay = Math.max(0, TuiBase.MIN_RENDER_INTERVAL_MS - elapsed);
|
|
537
536
|
this.renderTimer = setTimeout(() => {
|
|
538
537
|
this.renderTimer = undefined;
|
|
539
538
|
if (this.stopped || !this.renderRequested) {
|
|
@@ -547,7 +546,7 @@ export class TUI extends Container {
|
|
|
547
546
|
}
|
|
548
547
|
}, delay);
|
|
549
548
|
}
|
|
550
|
-
|
|
549
|
+
handleTerminalInput(data) {
|
|
551
550
|
if (this.consumeOsc11BackgroundResponse(data)) {
|
|
552
551
|
return;
|
|
553
552
|
}
|
|
@@ -616,7 +615,9 @@ export class TUI extends Container {
|
|
|
616
615
|
return;
|
|
617
616
|
}
|
|
618
617
|
this.focusedComponent.handleInput(data);
|
|
619
|
-
|
|
618
|
+
// Keyboard input is latency-sensitive. Avoid the throttled timer path,
|
|
619
|
+
// where even setTimeout(0) can take a full 16 ms tick on Windows.
|
|
620
|
+
this.requestImmediateRender();
|
|
620
621
|
}
|
|
621
622
|
}
|
|
622
623
|
consumeOsc11BackgroundResponse(data) {
|
|
@@ -842,9 +843,8 @@ export class TUI extends Container {
|
|
|
842
843
|
}
|
|
843
844
|
return result;
|
|
844
845
|
}
|
|
845
|
-
static SEGMENT_RESET = "\x1b[0m\x1b]8;;\x07";
|
|
846
846
|
applyLineResets(lines) {
|
|
847
|
-
const reset =
|
|
847
|
+
const reset = SEGMENT_RESET;
|
|
848
848
|
for (let i = 0; i < lines.length; i++) {
|
|
849
849
|
const line = lines[i];
|
|
850
850
|
if (!isImageLine(line)) {
|
|
@@ -853,104 +853,8 @@ export class TUI extends Container {
|
|
|
853
853
|
}
|
|
854
854
|
return lines;
|
|
855
855
|
}
|
|
856
|
-
collectKittyImageIds(lines) {
|
|
857
|
-
const ids = new Set();
|
|
858
|
-
for (const line of lines) {
|
|
859
|
-
for (const id of extractKittyImageIds(line)) {
|
|
860
|
-
ids.add(id);
|
|
861
|
-
}
|
|
862
|
-
}
|
|
863
|
-
return ids;
|
|
864
|
-
}
|
|
865
|
-
deleteKittyImages(ids) {
|
|
866
|
-
let buffer = "";
|
|
867
|
-
for (const id of ids) {
|
|
868
|
-
buffer += deleteKittyImage(id);
|
|
869
|
-
}
|
|
870
|
-
return buffer;
|
|
871
|
-
}
|
|
872
|
-
getKittyImageReservedRows(lines, index, maxIndex = lines.length - 1) {
|
|
873
|
-
const rows = extractKittyImageRows(lines[index] ?? "");
|
|
874
|
-
if (rows <= 1)
|
|
875
|
-
return 1;
|
|
876
|
-
const maxRows = Math.min(rows, maxIndex - index + 1, lines.length - index);
|
|
877
|
-
let reservedRows = 1;
|
|
878
|
-
while (reservedRows < maxRows) {
|
|
879
|
-
const line = lines[index + reservedRows] ?? "";
|
|
880
|
-
if (isImageLine(line) || visibleWidth(line) > 0)
|
|
881
|
-
break;
|
|
882
|
-
reservedRows++;
|
|
883
|
-
}
|
|
884
|
-
return reservedRows;
|
|
885
|
-
}
|
|
886
|
-
expandChangedRangeForKittyImages(firstChanged, lastChanged, newLines) {
|
|
887
|
-
let expandedFirstChanged = firstChanged;
|
|
888
|
-
let expandedLastChanged = lastChanged;
|
|
889
|
-
const expandForLines = (lines) => {
|
|
890
|
-
for (let i = 0; i < lines.length; i++) {
|
|
891
|
-
if (extractKittyImageIds(lines[i]).length === 0)
|
|
892
|
-
continue;
|
|
893
|
-
const blockEnd = i + this.getKittyImageReservedRows(lines, i) - 1;
|
|
894
|
-
if (i >= firstChanged || (i <= lastChanged && blockEnd >= firstChanged)) {
|
|
895
|
-
expandedFirstChanged = Math.min(expandedFirstChanged, i);
|
|
896
|
-
expandedLastChanged = Math.max(expandedLastChanged, blockEnd);
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
};
|
|
900
|
-
expandForLines(this.previousLines);
|
|
901
|
-
expandForLines(newLines);
|
|
902
|
-
return { firstChanged: expandedFirstChanged, lastChanged: expandedLastChanged };
|
|
903
|
-
}
|
|
904
|
-
deleteChangedKittyImages(firstChanged, lastChanged) {
|
|
905
|
-
if (firstChanged < 0 || lastChanged < firstChanged)
|
|
906
|
-
return "";
|
|
907
|
-
const ids = new Set();
|
|
908
|
-
const maxLine = Math.min(lastChanged, this.previousLines.length - 1);
|
|
909
|
-
for (let i = firstChanged; i <= maxLine; i++) {
|
|
910
|
-
for (const id of extractKittyImageIds(this.previousLines[i] ?? "")) {
|
|
911
|
-
ids.add(id);
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
return this.deleteKittyImages(ids);
|
|
915
|
-
}
|
|
916
|
-
/** Splice overlay content into a base line at a specific column. Single-pass optimized. */
|
|
917
856
|
compositeLineAt(baseLine, overlayLine, startCol, overlayWidth, totalWidth) {
|
|
918
|
-
|
|
919
|
-
return baseLine;
|
|
920
|
-
// Single pass through baseLine extracts both before and after segments
|
|
921
|
-
const afterStart = startCol + overlayWidth;
|
|
922
|
-
const base = extractSegments(baseLine, startCol, afterStart, totalWidth - afterStart, true);
|
|
923
|
-
// Extract overlay with width tracking (strict=true to exclude wide chars at boundary)
|
|
924
|
-
const overlay = sliceWithWidth(overlayLine, 0, overlayWidth, true);
|
|
925
|
-
// Pad segments to target widths
|
|
926
|
-
const beforePad = Math.max(0, startCol - base.beforeWidth);
|
|
927
|
-
const overlayPad = Math.max(0, overlayWidth - overlay.width);
|
|
928
|
-
const actualBeforeWidth = Math.max(startCol, base.beforeWidth);
|
|
929
|
-
const actualOverlayWidth = Math.max(overlayWidth, overlay.width);
|
|
930
|
-
const afterTarget = Math.max(0, totalWidth - actualBeforeWidth - actualOverlayWidth);
|
|
931
|
-
const afterPad = Math.max(0, afterTarget - base.afterWidth);
|
|
932
|
-
// Compose result
|
|
933
|
-
const r = TUI.SEGMENT_RESET;
|
|
934
|
-
const result = base.before +
|
|
935
|
-
" ".repeat(beforePad) +
|
|
936
|
-
r +
|
|
937
|
-
overlay.text +
|
|
938
|
-
" ".repeat(overlayPad) +
|
|
939
|
-
r +
|
|
940
|
-
base.after +
|
|
941
|
-
" ".repeat(afterPad);
|
|
942
|
-
// CRITICAL: Always verify and truncate to terminal width.
|
|
943
|
-
// This is the final safeguard against width overflow which would crash the TUI.
|
|
944
|
-
// Width tracking can drift from actual visible width due to:
|
|
945
|
-
// - Complex ANSI/OSC sequences (hyperlinks, colors)
|
|
946
|
-
// - Wide characters at segment boundaries
|
|
947
|
-
// - Edge cases in segment extraction
|
|
948
|
-
const resultWidth = visibleWidth(result);
|
|
949
|
-
if (resultWidth <= totalWidth) {
|
|
950
|
-
return result;
|
|
951
|
-
}
|
|
952
|
-
// Truncate with strict=true to ensure we don't exceed totalWidth
|
|
953
|
-
return sliceByColumn(result, 0, totalWidth, true);
|
|
857
|
+
return compositeTuiLine(baseLine, overlayLine, startCol, overlayWidth, totalWidth);
|
|
954
858
|
}
|
|
955
859
|
/**
|
|
956
860
|
* Find and extract cursor position from rendered lines.
|
|
@@ -977,385 +881,6 @@ export class TUI extends Container {
|
|
|
977
881
|
}
|
|
978
882
|
return null;
|
|
979
883
|
}
|
|
980
|
-
doRender() {
|
|
981
|
-
if (this.stopped)
|
|
982
|
-
return;
|
|
983
|
-
const width = this.terminal.columns;
|
|
984
|
-
const height = this.terminal.rows;
|
|
985
|
-
const widthChanged = this.previousWidth !== 0 && this.previousWidth !== width;
|
|
986
|
-
const heightChanged = this.previousHeight !== 0 && this.previousHeight !== height;
|
|
987
|
-
const previousBufferLength = this.previousHeight > 0 ? this.previousViewportTop + this.previousHeight : height;
|
|
988
|
-
let prevViewportTop = heightChanged ? Math.max(0, previousBufferLength - height) : this.previousViewportTop;
|
|
989
|
-
let viewportTop = prevViewportTop;
|
|
990
|
-
let hardwareCursorRow = this.hardwareCursorRow;
|
|
991
|
-
const computeLineDiff = (targetRow) => {
|
|
992
|
-
const currentScreenRow = hardwareCursorRow - prevViewportTop;
|
|
993
|
-
const targetScreenRow = targetRow - viewportTop;
|
|
994
|
-
return targetScreenRow - currentScreenRow;
|
|
995
|
-
};
|
|
996
|
-
// Render all components to get new lines
|
|
997
|
-
let newLines = this.render(width);
|
|
998
|
-
// Composite overlays into the rendered lines (before differential compare)
|
|
999
|
-
if (this.overlayStack.length > 0) {
|
|
1000
|
-
newLines = this.compositeOverlays(newLines, width, height);
|
|
1001
|
-
}
|
|
1002
|
-
// Extract cursor position before applying line resets (marker must be found first)
|
|
1003
|
-
const cursorPos = this.extractCursorPosition(newLines, height);
|
|
1004
|
-
newLines = this.applyLineResets(newLines);
|
|
1005
|
-
// Helper to clear scrollback and viewport and render all new lines
|
|
1006
|
-
const fullRender = (clear) => {
|
|
1007
|
-
this.fullRedrawCount += 1;
|
|
1008
|
-
let buffer = "\x1b[?2026h"; // Begin synchronized output
|
|
1009
|
-
if (clear) {
|
|
1010
|
-
buffer += this.deleteKittyImages(this.previousKittyImageIds);
|
|
1011
|
-
buffer += "\x1b[2J\x1b[H\x1b[3J"; // Clear screen, home, then clear scrollback
|
|
1012
|
-
}
|
|
1013
|
-
for (let i = 0; i < newLines.length; i++) {
|
|
1014
|
-
if (i > 0)
|
|
1015
|
-
buffer += "\r\n";
|
|
1016
|
-
const line = newLines[i];
|
|
1017
|
-
const isImage = isImageLine(line);
|
|
1018
|
-
const imageReservedRows = isImage ? this.getKittyImageReservedRows(newLines, i) : 1;
|
|
1019
|
-
if (imageReservedRows > 1 && imageReservedRows <= height) {
|
|
1020
|
-
for (let row = 1; row < imageReservedRows; row++) {
|
|
1021
|
-
buffer += "\r\n";
|
|
1022
|
-
}
|
|
1023
|
-
buffer += `\x1b[${imageReservedRows - 1}A`;
|
|
1024
|
-
buffer += line;
|
|
1025
|
-
buffer += `\x1b[${imageReservedRows - 1}B`;
|
|
1026
|
-
i += imageReservedRows - 1;
|
|
1027
|
-
continue;
|
|
1028
|
-
}
|
|
1029
|
-
buffer += line;
|
|
1030
|
-
}
|
|
1031
|
-
buffer += "\x1b[?2026l"; // End synchronized output
|
|
1032
|
-
this.terminal.write(buffer);
|
|
1033
|
-
this.cursorRow = Math.max(0, newLines.length - 1);
|
|
1034
|
-
this.hardwareCursorRow = this.cursorRow;
|
|
1035
|
-
// Reset max lines when clearing, otherwise track growth
|
|
1036
|
-
if (clear) {
|
|
1037
|
-
this.maxLinesRendered = newLines.length;
|
|
1038
|
-
}
|
|
1039
|
-
else {
|
|
1040
|
-
this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);
|
|
1041
|
-
}
|
|
1042
|
-
const bufferLength = Math.max(height, newLines.length);
|
|
1043
|
-
this.previousViewportTop = Math.max(0, bufferLength - height);
|
|
1044
|
-
this.positionHardwareCursor(cursorPos, newLines.length);
|
|
1045
|
-
this.previousLines = newLines;
|
|
1046
|
-
this.previousKittyImageIds = this.collectKittyImageIds(newLines);
|
|
1047
|
-
this.previousWidth = width;
|
|
1048
|
-
this.previousHeight = height;
|
|
1049
|
-
};
|
|
1050
|
-
const debugRedraw = process.env.PI_DEBUG_REDRAW === "1";
|
|
1051
|
-
const logRedraw = (reason) => {
|
|
1052
|
-
if (!debugRedraw)
|
|
1053
|
-
return;
|
|
1054
|
-
const logPath = path.join(this.logDirectory, "pi-debug.log");
|
|
1055
|
-
const msg = `[${new Date().toISOString()}] fullRender: ${reason} (prev=${this.previousLines.length}, new=${newLines.length}, height=${height})\n`;
|
|
1056
|
-
fs.mkdirSync(path.dirname(logPath), { recursive: true });
|
|
1057
|
-
fs.appendFileSync(logPath, msg);
|
|
1058
|
-
};
|
|
1059
|
-
// First render - just output everything without clearing (assumes clean screen)
|
|
1060
|
-
if (this.previousLines.length === 0 && !widthChanged && !heightChanged) {
|
|
1061
|
-
logRedraw("first render");
|
|
1062
|
-
fullRender(false);
|
|
1063
|
-
return;
|
|
1064
|
-
}
|
|
1065
|
-
// Width changes always need a full re-render because wrapping changes.
|
|
1066
|
-
if (widthChanged) {
|
|
1067
|
-
logRedraw(`terminal width changed (${this.previousWidth} -> ${width})`);
|
|
1068
|
-
fullRender(true);
|
|
1069
|
-
return;
|
|
1070
|
-
}
|
|
1071
|
-
// Height changes normally need a full re-render to keep the visible viewport aligned,
|
|
1072
|
-
// but Termux changes height when the software keyboard shows or hides.
|
|
1073
|
-
// In that environment, a full redraw causes the entire history to replay on every toggle.
|
|
1074
|
-
if (heightChanged && !isTermuxSession()) {
|
|
1075
|
-
logRedraw(`terminal height changed (${this.previousHeight} -> ${height})`);
|
|
1076
|
-
fullRender(true);
|
|
1077
|
-
return;
|
|
1078
|
-
}
|
|
1079
|
-
// Content shrunk below the working area and no overlays - re-render to clear empty rows
|
|
1080
|
-
// (overlays need the padding, so only do this when no overlays are active)
|
|
1081
|
-
// Configurable via setClearOnShrink() or PI_CLEAR_ON_SHRINK=0 env var
|
|
1082
|
-
if (this.clearOnShrink && newLines.length < this.maxLinesRendered && this.overlayStack.length === 0) {
|
|
1083
|
-
logRedraw(`clearOnShrink (maxLinesRendered=${this.maxLinesRendered})`);
|
|
1084
|
-
fullRender(true);
|
|
1085
|
-
return;
|
|
1086
|
-
}
|
|
1087
|
-
// Find first and last changed lines
|
|
1088
|
-
let firstChanged = -1;
|
|
1089
|
-
let lastChanged = -1;
|
|
1090
|
-
const maxLines = Math.max(newLines.length, this.previousLines.length);
|
|
1091
|
-
for (let i = 0; i < maxLines; i++) {
|
|
1092
|
-
const oldLine = i < this.previousLines.length ? this.previousLines[i] : "";
|
|
1093
|
-
const newLine = i < newLines.length ? newLines[i] : "";
|
|
1094
|
-
if (oldLine !== newLine) {
|
|
1095
|
-
if (firstChanged === -1) {
|
|
1096
|
-
firstChanged = i;
|
|
1097
|
-
}
|
|
1098
|
-
lastChanged = i;
|
|
1099
|
-
}
|
|
1100
|
-
}
|
|
1101
|
-
const appendedLines = newLines.length > this.previousLines.length;
|
|
1102
|
-
if (appendedLines) {
|
|
1103
|
-
if (firstChanged === -1) {
|
|
1104
|
-
firstChanged = this.previousLines.length;
|
|
1105
|
-
}
|
|
1106
|
-
lastChanged = newLines.length - 1;
|
|
1107
|
-
}
|
|
1108
|
-
if (firstChanged !== -1) {
|
|
1109
|
-
const expandedRange = this.expandChangedRangeForKittyImages(firstChanged, lastChanged, newLines);
|
|
1110
|
-
firstChanged = expandedRange.firstChanged;
|
|
1111
|
-
lastChanged = expandedRange.lastChanged;
|
|
1112
|
-
}
|
|
1113
|
-
const appendStart = appendedLines && firstChanged === this.previousLines.length && firstChanged > 0;
|
|
1114
|
-
// No changes - but still need to update hardware cursor position if it moved
|
|
1115
|
-
if (firstChanged === -1) {
|
|
1116
|
-
this.positionHardwareCursor(cursorPos, newLines.length);
|
|
1117
|
-
this.previousViewportTop = prevViewportTop;
|
|
1118
|
-
this.previousHeight = height;
|
|
1119
|
-
return;
|
|
1120
|
-
}
|
|
1121
|
-
// All changes are in deleted lines (nothing to render, just clear)
|
|
1122
|
-
if (firstChanged >= newLines.length) {
|
|
1123
|
-
if (this.previousLines.length > newLines.length) {
|
|
1124
|
-
let buffer = "\x1b[?2026h";
|
|
1125
|
-
buffer += this.deleteChangedKittyImages(firstChanged, lastChanged);
|
|
1126
|
-
// Move to end of new content (clamp to 0 for empty content)
|
|
1127
|
-
const targetRow = Math.max(0, newLines.length - 1);
|
|
1128
|
-
if (targetRow < prevViewportTop) {
|
|
1129
|
-
logRedraw(`deleted lines moved viewport up (${targetRow} < ${prevViewportTop})`);
|
|
1130
|
-
fullRender(true);
|
|
1131
|
-
return;
|
|
1132
|
-
}
|
|
1133
|
-
const lineDiff = computeLineDiff(targetRow);
|
|
1134
|
-
if (lineDiff > 0)
|
|
1135
|
-
buffer += `\x1b[${lineDiff}B`;
|
|
1136
|
-
else if (lineDiff < 0)
|
|
1137
|
-
buffer += `\x1b[${-lineDiff}A`;
|
|
1138
|
-
buffer += "\r";
|
|
1139
|
-
// Clear extra lines without scrolling
|
|
1140
|
-
const extraLines = this.previousLines.length - newLines.length;
|
|
1141
|
-
if (extraLines > height) {
|
|
1142
|
-
logRedraw(`extraLines > height (${extraLines} > ${height})`);
|
|
1143
|
-
fullRender(true);
|
|
1144
|
-
return;
|
|
1145
|
-
}
|
|
1146
|
-
const clearStartOffset = newLines.length === 0 ? 0 : 1;
|
|
1147
|
-
if (extraLines > 0 && clearStartOffset > 0) {
|
|
1148
|
-
buffer += `\x1b[${clearStartOffset}B`;
|
|
1149
|
-
}
|
|
1150
|
-
for (let i = 0; i < extraLines; i++) {
|
|
1151
|
-
buffer += "\r\x1b[2K";
|
|
1152
|
-
if (i < extraLines - 1)
|
|
1153
|
-
buffer += "\x1b[1B";
|
|
1154
|
-
}
|
|
1155
|
-
const moveBack = Math.max(0, extraLines - 1 + clearStartOffset);
|
|
1156
|
-
if (moveBack > 0) {
|
|
1157
|
-
buffer += `\x1b[${moveBack}A`;
|
|
1158
|
-
}
|
|
1159
|
-
buffer += "\x1b[?2026l";
|
|
1160
|
-
this.terminal.write(buffer);
|
|
1161
|
-
this.cursorRow = targetRow;
|
|
1162
|
-
this.hardwareCursorRow = targetRow;
|
|
1163
|
-
}
|
|
1164
|
-
this.positionHardwareCursor(cursorPos, newLines.length);
|
|
1165
|
-
this.previousLines = newLines;
|
|
1166
|
-
this.previousKittyImageIds = this.collectKittyImageIds(newLines);
|
|
1167
|
-
this.previousWidth = width;
|
|
1168
|
-
this.previousHeight = height;
|
|
1169
|
-
this.previousViewportTop = prevViewportTop;
|
|
1170
|
-
return;
|
|
1171
|
-
}
|
|
1172
|
-
// Differential rendering can only touch what was actually visible.
|
|
1173
|
-
// If the first changed line is above the previous viewport, we need a full redraw.
|
|
1174
|
-
if (firstChanged < prevViewportTop) {
|
|
1175
|
-
logRedraw(`firstChanged < viewportTop (${firstChanged} < ${prevViewportTop})`);
|
|
1176
|
-
fullRender(true);
|
|
1177
|
-
return;
|
|
1178
|
-
}
|
|
1179
|
-
// Render from first changed line to end
|
|
1180
|
-
// Build buffer with all updates wrapped in synchronized output
|
|
1181
|
-
let buffer = "\x1b[?2026h"; // Begin synchronized output
|
|
1182
|
-
buffer += this.deleteChangedKittyImages(firstChanged, lastChanged);
|
|
1183
|
-
const prevViewportBottom = prevViewportTop + height - 1;
|
|
1184
|
-
const moveTargetRow = appendStart ? firstChanged - 1 : firstChanged;
|
|
1185
|
-
if (moveTargetRow > prevViewportBottom) {
|
|
1186
|
-
const currentScreenRow = Math.max(0, Math.min(height - 1, hardwareCursorRow - prevViewportTop));
|
|
1187
|
-
const moveToBottom = height - 1 - currentScreenRow;
|
|
1188
|
-
if (moveToBottom > 0) {
|
|
1189
|
-
buffer += `\x1b[${moveToBottom}B`;
|
|
1190
|
-
}
|
|
1191
|
-
const scroll = moveTargetRow - prevViewportBottom;
|
|
1192
|
-
buffer += "\r\n".repeat(scroll);
|
|
1193
|
-
prevViewportTop += scroll;
|
|
1194
|
-
viewportTop += scroll;
|
|
1195
|
-
hardwareCursorRow = moveTargetRow;
|
|
1196
|
-
}
|
|
1197
|
-
// Move cursor to first changed line (use hardwareCursorRow for actual position)
|
|
1198
|
-
const lineDiff = computeLineDiff(moveTargetRow);
|
|
1199
|
-
if (lineDiff > 0) {
|
|
1200
|
-
buffer += `\x1b[${lineDiff}B`; // Move down
|
|
1201
|
-
}
|
|
1202
|
-
else if (lineDiff < 0) {
|
|
1203
|
-
buffer += `\x1b[${-lineDiff}A`; // Move up
|
|
1204
|
-
}
|
|
1205
|
-
buffer += appendStart ? "\r\n" : "\r"; // Move to column 0
|
|
1206
|
-
// Only render changed lines (firstChanged to lastChanged), not all lines to end
|
|
1207
|
-
// This reduces flicker when only a single line changes (e.g., spinner animation)
|
|
1208
|
-
const renderEnd = Math.min(lastChanged, newLines.length - 1);
|
|
1209
|
-
for (let i = firstChanged; i <= renderEnd; i++) {
|
|
1210
|
-
if (i > firstChanged)
|
|
1211
|
-
buffer += "\r\n";
|
|
1212
|
-
const line = newLines[i];
|
|
1213
|
-
const isImage = isImageLine(line);
|
|
1214
|
-
const imageReservedRows = isImage ? this.getKittyImageReservedRows(newLines, i, renderEnd) : 1;
|
|
1215
|
-
if (imageReservedRows > 1) {
|
|
1216
|
-
const imageStartScreenRow = i - viewportTop;
|
|
1217
|
-
if (imageStartScreenRow < 0 || imageStartScreenRow + imageReservedRows > height) {
|
|
1218
|
-
logRedraw(`kitty image pre-clear would scroll (${imageStartScreenRow} + ${imageReservedRows} > ${height})`);
|
|
1219
|
-
fullRender(true);
|
|
1220
|
-
return;
|
|
1221
|
-
}
|
|
1222
|
-
buffer += "\x1b[2K";
|
|
1223
|
-
for (let row = 1; row < imageReservedRows; row++) {
|
|
1224
|
-
buffer += "\r\n\x1b[2K";
|
|
1225
|
-
}
|
|
1226
|
-
buffer += `\x1b[${imageReservedRows - 1}A`;
|
|
1227
|
-
buffer += line;
|
|
1228
|
-
buffer += `\x1b[${imageReservedRows - 1}B`;
|
|
1229
|
-
i += imageReservedRows - 1;
|
|
1230
|
-
continue;
|
|
1231
|
-
}
|
|
1232
|
-
buffer += "\x1b[2K"; // Clear current line
|
|
1233
|
-
if (!isImage && visibleWidth(line) > width) {
|
|
1234
|
-
// Log all lines to crash file for debugging
|
|
1235
|
-
const crashLogPath = path.join(this.logDirectory, "pi-crash.log");
|
|
1236
|
-
const crashData = [
|
|
1237
|
-
`Crash at ${new Date().toISOString()}`,
|
|
1238
|
-
`Terminal width: ${width}`,
|
|
1239
|
-
`Line ${i} visible width: ${visibleWidth(line)}`,
|
|
1240
|
-
"",
|
|
1241
|
-
"=== All rendered lines ===",
|
|
1242
|
-
...newLines.map((l, idx) => `[${idx}] (w=${visibleWidth(l)}) ${l}`),
|
|
1243
|
-
"",
|
|
1244
|
-
].join("\n");
|
|
1245
|
-
fs.mkdirSync(path.dirname(crashLogPath), { recursive: true });
|
|
1246
|
-
fs.writeFileSync(crashLogPath, crashData);
|
|
1247
|
-
// Clean up terminal state before throwing
|
|
1248
|
-
this.stop();
|
|
1249
|
-
const errorMsg = [
|
|
1250
|
-
`Rendered line ${i} exceeds terminal width (${visibleWidth(line)} > ${width}).`,
|
|
1251
|
-
"",
|
|
1252
|
-
"This is likely caused by a custom TUI component not truncating its output.",
|
|
1253
|
-
"Use visibleWidth() to measure and truncateToWidth() to truncate lines.",
|
|
1254
|
-
"",
|
|
1255
|
-
`Debug log written to: ${crashLogPath}`,
|
|
1256
|
-
].join("\n");
|
|
1257
|
-
throw new Error(errorMsg);
|
|
1258
|
-
}
|
|
1259
|
-
buffer += line;
|
|
1260
|
-
}
|
|
1261
|
-
// Track where cursor ended up after rendering
|
|
1262
|
-
let finalCursorRow = renderEnd;
|
|
1263
|
-
// If we had more lines before, clear them and move cursor back
|
|
1264
|
-
if (this.previousLines.length > newLines.length) {
|
|
1265
|
-
// Move to end of new content first if we stopped before it
|
|
1266
|
-
if (renderEnd < newLines.length - 1) {
|
|
1267
|
-
const moveDown = newLines.length - 1 - renderEnd;
|
|
1268
|
-
buffer += `\x1b[${moveDown}B`;
|
|
1269
|
-
finalCursorRow = newLines.length - 1;
|
|
1270
|
-
}
|
|
1271
|
-
const extraLines = this.previousLines.length - newLines.length;
|
|
1272
|
-
for (let i = newLines.length; i < this.previousLines.length; i++) {
|
|
1273
|
-
buffer += "\r\n\x1b[2K";
|
|
1274
|
-
}
|
|
1275
|
-
// Move cursor back to end of new content
|
|
1276
|
-
buffer += `\x1b[${extraLines}A`;
|
|
1277
|
-
}
|
|
1278
|
-
buffer += "\x1b[?2026l"; // End synchronized output
|
|
1279
|
-
if (process.env.PI_TUI_DEBUG === "1") {
|
|
1280
|
-
const debugDir = "/tmp/tui";
|
|
1281
|
-
fs.mkdirSync(debugDir, { recursive: true });
|
|
1282
|
-
const debugPath = path.join(debugDir, `render-${Date.now()}-${Math.random().toString(36).slice(2)}.log`);
|
|
1283
|
-
const debugData = [
|
|
1284
|
-
`firstChanged: ${firstChanged}`,
|
|
1285
|
-
`viewportTop: ${viewportTop}`,
|
|
1286
|
-
`cursorRow: ${this.cursorRow}`,
|
|
1287
|
-
`height: ${height}`,
|
|
1288
|
-
`lineDiff: ${lineDiff}`,
|
|
1289
|
-
`hardwareCursorRow: ${hardwareCursorRow}`,
|
|
1290
|
-
`renderEnd: ${renderEnd}`,
|
|
1291
|
-
`finalCursorRow: ${finalCursorRow}`,
|
|
1292
|
-
`cursorPos: ${JSON.stringify(cursorPos)}`,
|
|
1293
|
-
`newLines.length: ${newLines.length}`,
|
|
1294
|
-
`previousLines.length: ${this.previousLines.length}`,
|
|
1295
|
-
"",
|
|
1296
|
-
"=== newLines ===",
|
|
1297
|
-
JSON.stringify(newLines, null, 2),
|
|
1298
|
-
"",
|
|
1299
|
-
"=== previousLines ===",
|
|
1300
|
-
JSON.stringify(this.previousLines, null, 2),
|
|
1301
|
-
"",
|
|
1302
|
-
"=== buffer ===",
|
|
1303
|
-
JSON.stringify(buffer),
|
|
1304
|
-
].join("\n");
|
|
1305
|
-
fs.writeFileSync(debugPath, debugData);
|
|
1306
|
-
}
|
|
1307
|
-
// Write entire buffer at once
|
|
1308
|
-
this.terminal.write(buffer);
|
|
1309
|
-
// Track cursor position for next render
|
|
1310
|
-
// cursorRow tracks end of content (for viewport calculation)
|
|
1311
|
-
// hardwareCursorRow tracks actual terminal cursor position (for movement)
|
|
1312
|
-
this.cursorRow = Math.max(0, newLines.length - 1);
|
|
1313
|
-
this.hardwareCursorRow = finalCursorRow;
|
|
1314
|
-
// Track terminal's working area (grows but doesn't shrink unless cleared)
|
|
1315
|
-
this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);
|
|
1316
|
-
this.previousViewportTop = Math.max(prevViewportTop, finalCursorRow - height + 1);
|
|
1317
|
-
// Position hardware cursor for IME
|
|
1318
|
-
this.positionHardwareCursor(cursorPos, newLines.length);
|
|
1319
|
-
this.previousLines = newLines;
|
|
1320
|
-
this.previousKittyImageIds = this.collectKittyImageIds(newLines);
|
|
1321
|
-
this.previousWidth = width;
|
|
1322
|
-
this.previousHeight = height;
|
|
1323
|
-
}
|
|
1324
|
-
/**
|
|
1325
|
-
* Position the hardware cursor for IME candidate window.
|
|
1326
|
-
* @param cursorPos The cursor position extracted from rendered output, or null
|
|
1327
|
-
* @param totalLines Total number of rendered lines
|
|
1328
|
-
*/
|
|
1329
|
-
positionHardwareCursor(cursorPos, totalLines) {
|
|
1330
|
-
if (!cursorPos || totalLines <= 0) {
|
|
1331
|
-
this.terminal.hideCursor();
|
|
1332
|
-
return;
|
|
1333
|
-
}
|
|
1334
|
-
// Clamp cursor position to valid range
|
|
1335
|
-
const targetRow = Math.max(0, Math.min(cursorPos.row, totalLines - 1));
|
|
1336
|
-
const targetCol = Math.max(0, cursorPos.col);
|
|
1337
|
-
// Move cursor from current position to target
|
|
1338
|
-
const rowDelta = targetRow - this.hardwareCursorRow;
|
|
1339
|
-
let buffer = "";
|
|
1340
|
-
if (rowDelta > 0) {
|
|
1341
|
-
buffer += `\x1b[${rowDelta}B`; // Move down
|
|
1342
|
-
}
|
|
1343
|
-
else if (rowDelta < 0) {
|
|
1344
|
-
buffer += `\x1b[${-rowDelta}A`; // Move up
|
|
1345
|
-
}
|
|
1346
|
-
// Move to absolute column (1-indexed)
|
|
1347
|
-
buffer += `\x1b[${targetCol + 1}G`;
|
|
1348
|
-
if (buffer) {
|
|
1349
|
-
this.terminal.write(buffer);
|
|
1350
|
-
}
|
|
1351
|
-
this.hardwareCursorRow = targetRow;
|
|
1352
|
-
if (this.showHardwareCursor) {
|
|
1353
|
-
this.terminal.showCursor();
|
|
1354
|
-
}
|
|
1355
|
-
else {
|
|
1356
|
-
this.terminal.hideCursor();
|
|
1357
|
-
}
|
|
1358
|
-
}
|
|
1359
884
|
/**
|
|
1360
885
|
* Query the terminal's default background color with OSC 11 (`ESC ] 11 ; ? BEL`).
|
|
1361
886
|
* @param timeoutMs Query timeout in milliseconds.
|