@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
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { deleteKittyImage, isImageLine } from "./terminal-image.js";
|
|
4
|
+
import { TuiBase } from "./tui.js";
|
|
5
|
+
import { visibleWidth } from "./utils.js";
|
|
6
|
+
const KITTY_SEQUENCE_PREFIX = "\x1b_G";
|
|
7
|
+
function parseKittyImageHeader(line) {
|
|
8
|
+
const sequenceStart = line.indexOf(KITTY_SEQUENCE_PREFIX);
|
|
9
|
+
if (sequenceStart === -1)
|
|
10
|
+
return undefined;
|
|
11
|
+
const paramsStart = sequenceStart + KITTY_SEQUENCE_PREFIX.length;
|
|
12
|
+
const paramsEnd = line.indexOf(";", paramsStart);
|
|
13
|
+
if (paramsEnd === -1)
|
|
14
|
+
return undefined;
|
|
15
|
+
const ids = [];
|
|
16
|
+
let rows = 1;
|
|
17
|
+
for (const param of line.slice(paramsStart, paramsEnd).split(",")) {
|
|
18
|
+
const [key, value] = param.split("=", 2);
|
|
19
|
+
if (value === undefined)
|
|
20
|
+
continue;
|
|
21
|
+
const numberValue = Number(value);
|
|
22
|
+
if (!Number.isInteger(numberValue) || numberValue <= 0 || numberValue > 0xffffffff)
|
|
23
|
+
continue;
|
|
24
|
+
if (key === "i")
|
|
25
|
+
ids.push(numberValue);
|
|
26
|
+
else if (key === "r")
|
|
27
|
+
rows = numberValue;
|
|
28
|
+
}
|
|
29
|
+
return { ids, rows };
|
|
30
|
+
}
|
|
31
|
+
function extractKittyImageIds(line) {
|
|
32
|
+
return parseKittyImageHeader(line)?.ids ?? [];
|
|
33
|
+
}
|
|
34
|
+
function extractKittyImageRows(line) {
|
|
35
|
+
return parseKittyImageHeader(line)?.rows ?? 1;
|
|
36
|
+
}
|
|
37
|
+
function isTermuxSession() {
|
|
38
|
+
return Boolean(process.env.TERMUX_VERSION);
|
|
39
|
+
}
|
|
40
|
+
/** TUI implementation that renders into the terminal's main screen and scrollback. */
|
|
41
|
+
export class TuiMainScreen extends TuiBase {
|
|
42
|
+
mode = "regular";
|
|
43
|
+
previousLines = [];
|
|
44
|
+
previousKittyImageIds = new Set();
|
|
45
|
+
previousWidth = 0;
|
|
46
|
+
previousHeight = 0;
|
|
47
|
+
cursorRow = 0;
|
|
48
|
+
hardwareCursorRow = 0;
|
|
49
|
+
maxLinesRendered = 0;
|
|
50
|
+
previousViewportTop = 0;
|
|
51
|
+
captureRenderState() {
|
|
52
|
+
return {
|
|
53
|
+
previousLines: [...this.previousLines],
|
|
54
|
+
previousWidth: this.previousWidth,
|
|
55
|
+
previousHeight: this.previousHeight,
|
|
56
|
+
cursorRow: this.cursorRow,
|
|
57
|
+
hardwareCursorRow: this.hardwareCursorRow,
|
|
58
|
+
maxLinesRendered: this.maxLinesRendered,
|
|
59
|
+
previousViewportTop: this.previousViewportTop,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
restoreRenderState(state) {
|
|
63
|
+
this.previousLines = state.previousLines.map((line) => (isImageLine(line) ? "" : line));
|
|
64
|
+
this.previousKittyImageIds = new Set();
|
|
65
|
+
this.previousWidth = state.previousWidth;
|
|
66
|
+
this.previousHeight = state.previousHeight;
|
|
67
|
+
this.cursorRow = state.cursorRow;
|
|
68
|
+
this.hardwareCursorRow = state.hardwareCursorRow;
|
|
69
|
+
this.maxLinesRendered = state.maxLinesRendered;
|
|
70
|
+
this.previousViewportTop = state.previousViewportTop;
|
|
71
|
+
}
|
|
72
|
+
resetRenderState() {
|
|
73
|
+
this.previousLines = [];
|
|
74
|
+
this.previousWidth = -1;
|
|
75
|
+
this.previousHeight = -1;
|
|
76
|
+
this.cursorRow = 0;
|
|
77
|
+
this.hardwareCursorRow = 0;
|
|
78
|
+
this.maxLinesRendered = 0;
|
|
79
|
+
this.previousViewportTop = 0;
|
|
80
|
+
}
|
|
81
|
+
beforeTerminalStop(options) {
|
|
82
|
+
if (options.preserveScreen || this.previousLines.length === 0)
|
|
83
|
+
return;
|
|
84
|
+
this.terminal.write(" ");
|
|
85
|
+
const targetRow = this.previousLines.length;
|
|
86
|
+
const lineDiff = targetRow - this.hardwareCursorRow;
|
|
87
|
+
if (lineDiff > 0)
|
|
88
|
+
this.terminal.write(`\x1b[${lineDiff}B`);
|
|
89
|
+
else if (lineDiff < 0)
|
|
90
|
+
this.terminal.write(`\x1b[${-lineDiff}A`);
|
|
91
|
+
this.terminal.write("\r\n");
|
|
92
|
+
}
|
|
93
|
+
collectKittyImageIds(lines) {
|
|
94
|
+
const ids = new Set();
|
|
95
|
+
for (const line of lines) {
|
|
96
|
+
for (const id of extractKittyImageIds(line)) {
|
|
97
|
+
ids.add(id);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return ids;
|
|
101
|
+
}
|
|
102
|
+
deleteKittyImages(ids) {
|
|
103
|
+
let buffer = "";
|
|
104
|
+
for (const id of ids) {
|
|
105
|
+
buffer += deleteKittyImage(id);
|
|
106
|
+
}
|
|
107
|
+
return buffer;
|
|
108
|
+
}
|
|
109
|
+
getKittyImageReservedRows(lines, index, maxIndex = lines.length - 1) {
|
|
110
|
+
const rows = extractKittyImageRows(lines[index] ?? "");
|
|
111
|
+
if (rows <= 1)
|
|
112
|
+
return 1;
|
|
113
|
+
const maxRows = Math.min(rows, maxIndex - index + 1, lines.length - index);
|
|
114
|
+
let reservedRows = 1;
|
|
115
|
+
while (reservedRows < maxRows) {
|
|
116
|
+
const line = lines[index + reservedRows] ?? "";
|
|
117
|
+
if (isImageLine(line) || visibleWidth(line) > 0)
|
|
118
|
+
break;
|
|
119
|
+
reservedRows++;
|
|
120
|
+
}
|
|
121
|
+
return reservedRows;
|
|
122
|
+
}
|
|
123
|
+
expandChangedRangeForKittyImages(firstChanged, lastChanged, newLines) {
|
|
124
|
+
let expandedFirstChanged = firstChanged;
|
|
125
|
+
let expandedLastChanged = lastChanged;
|
|
126
|
+
const expandForLines = (lines) => {
|
|
127
|
+
for (let i = 0; i < lines.length; i++) {
|
|
128
|
+
if (extractKittyImageIds(lines[i]).length === 0)
|
|
129
|
+
continue;
|
|
130
|
+
const blockEnd = i + this.getKittyImageReservedRows(lines, i) - 1;
|
|
131
|
+
if (i >= firstChanged || (i <= lastChanged && blockEnd >= firstChanged)) {
|
|
132
|
+
expandedFirstChanged = Math.min(expandedFirstChanged, i);
|
|
133
|
+
expandedLastChanged = Math.max(expandedLastChanged, blockEnd);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
expandForLines(this.previousLines);
|
|
138
|
+
expandForLines(newLines);
|
|
139
|
+
return { firstChanged: expandedFirstChanged, lastChanged: expandedLastChanged };
|
|
140
|
+
}
|
|
141
|
+
deleteChangedKittyImages(firstChanged, lastChanged) {
|
|
142
|
+
if (firstChanged < 0 || lastChanged < firstChanged)
|
|
143
|
+
return "";
|
|
144
|
+
const ids = new Set();
|
|
145
|
+
const maxLine = Math.min(lastChanged, this.previousLines.length - 1);
|
|
146
|
+
for (let i = firstChanged; i <= maxLine; i++) {
|
|
147
|
+
for (const id of extractKittyImageIds(this.previousLines[i] ?? "")) {
|
|
148
|
+
ids.add(id);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return this.deleteKittyImages(ids);
|
|
152
|
+
}
|
|
153
|
+
doRender() {
|
|
154
|
+
if (this.stopped)
|
|
155
|
+
return;
|
|
156
|
+
const width = this.terminal.columns;
|
|
157
|
+
const height = this.terminal.rows;
|
|
158
|
+
const widthChanged = this.previousWidth !== 0 && this.previousWidth !== width;
|
|
159
|
+
const heightChanged = this.previousHeight !== 0 && this.previousHeight !== height;
|
|
160
|
+
const previousBufferLength = this.previousHeight > 0 ? this.previousViewportTop + this.previousHeight : height;
|
|
161
|
+
let prevViewportTop = heightChanged ? Math.max(0, previousBufferLength - height) : this.previousViewportTop;
|
|
162
|
+
let viewportTop = prevViewportTop;
|
|
163
|
+
let hardwareCursorRow = this.hardwareCursorRow;
|
|
164
|
+
const computeLineDiff = (targetRow) => {
|
|
165
|
+
const currentScreenRow = hardwareCursorRow - prevViewportTop;
|
|
166
|
+
const targetScreenRow = targetRow - viewportTop;
|
|
167
|
+
return targetScreenRow - currentScreenRow;
|
|
168
|
+
};
|
|
169
|
+
// Render all components to get new lines
|
|
170
|
+
let newLines = this.render(width);
|
|
171
|
+
// Composite overlays into the rendered lines (before differential compare)
|
|
172
|
+
if (this.hasOverlayEntries) {
|
|
173
|
+
newLines = this.compositeOverlays(newLines, width, height);
|
|
174
|
+
}
|
|
175
|
+
// Extract cursor position before applying line resets (marker must be found first)
|
|
176
|
+
const cursorPos = this.extractCursorPosition(newLines, height);
|
|
177
|
+
newLines = this.applyLineResets(newLines);
|
|
178
|
+
// Helper to clear scrollback and viewport and render all new lines
|
|
179
|
+
const fullRender = (clear) => {
|
|
180
|
+
this.fullRedrawCount += 1;
|
|
181
|
+
let buffer = "\x1b[?2026h"; // Begin synchronized output
|
|
182
|
+
if (clear) {
|
|
183
|
+
buffer += this.deleteKittyImages(this.previousKittyImageIds);
|
|
184
|
+
buffer += "\x1b[2J\x1b[H\x1b[3J"; // Clear screen, home, then clear scrollback
|
|
185
|
+
}
|
|
186
|
+
for (let i = 0; i < newLines.length; i++) {
|
|
187
|
+
if (i > 0)
|
|
188
|
+
buffer += "\r\n";
|
|
189
|
+
const line = newLines[i];
|
|
190
|
+
const isImage = isImageLine(line);
|
|
191
|
+
const imageReservedRows = isImage ? this.getKittyImageReservedRows(newLines, i) : 1;
|
|
192
|
+
if (imageReservedRows > 1 && imageReservedRows <= height) {
|
|
193
|
+
for (let row = 1; row < imageReservedRows; row++) {
|
|
194
|
+
buffer += "\r\n";
|
|
195
|
+
}
|
|
196
|
+
buffer += `\x1b[${imageReservedRows - 1}A`;
|
|
197
|
+
buffer += line;
|
|
198
|
+
buffer += `\x1b[${imageReservedRows - 1}B`;
|
|
199
|
+
i += imageReservedRows - 1;
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
buffer += line;
|
|
203
|
+
}
|
|
204
|
+
buffer += "\x1b[?2026l"; // End synchronized output
|
|
205
|
+
this.terminal.write(buffer);
|
|
206
|
+
this.cursorRow = Math.max(0, newLines.length - 1);
|
|
207
|
+
this.hardwareCursorRow = this.cursorRow;
|
|
208
|
+
// Reset max lines when clearing, otherwise track growth
|
|
209
|
+
if (clear) {
|
|
210
|
+
this.maxLinesRendered = newLines.length;
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);
|
|
214
|
+
}
|
|
215
|
+
const bufferLength = Math.max(height, newLines.length);
|
|
216
|
+
this.previousViewportTop = Math.max(0, bufferLength - height);
|
|
217
|
+
this.positionHardwareCursor(cursorPos, newLines.length);
|
|
218
|
+
this.previousLines = newLines;
|
|
219
|
+
this.previousKittyImageIds = this.collectKittyImageIds(newLines);
|
|
220
|
+
this.previousWidth = width;
|
|
221
|
+
this.previousHeight = height;
|
|
222
|
+
};
|
|
223
|
+
const debugRedraw = process.env.PI_DEBUG_REDRAW === "1";
|
|
224
|
+
const logRedraw = (reason) => {
|
|
225
|
+
if (!debugRedraw)
|
|
226
|
+
return;
|
|
227
|
+
const logPath = path.join(this.logDirectory, "pi-debug.log");
|
|
228
|
+
const msg = `[${new Date().toISOString()}] fullRender: ${reason} (prev=${this.previousLines.length}, new=${newLines.length}, height=${height})\n`;
|
|
229
|
+
fs.mkdirSync(path.dirname(logPath), { recursive: true });
|
|
230
|
+
fs.appendFileSync(logPath, msg);
|
|
231
|
+
};
|
|
232
|
+
// First render - just output everything without clearing (assumes clean screen)
|
|
233
|
+
if (this.previousLines.length === 0 && !widthChanged && !heightChanged) {
|
|
234
|
+
logRedraw("first render");
|
|
235
|
+
fullRender(false);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
// Width changes always need a full re-render because wrapping changes.
|
|
239
|
+
if (widthChanged) {
|
|
240
|
+
logRedraw(`terminal width changed (${this.previousWidth} -> ${width})`);
|
|
241
|
+
fullRender(true);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
// Height changes normally need a full re-render to keep the visible viewport aligned,
|
|
245
|
+
// but Termux changes height when the software keyboard shows or hides.
|
|
246
|
+
// In that environment, a full redraw causes the entire history to replay on every toggle.
|
|
247
|
+
if (heightChanged && !isTermuxSession()) {
|
|
248
|
+
logRedraw(`terminal height changed (${this.previousHeight} -> ${height})`);
|
|
249
|
+
fullRender(true);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
// Content shrunk below the working area and no overlays - re-render to clear empty rows
|
|
253
|
+
// (overlays need the padding, so only do this when no overlays are active)
|
|
254
|
+
// Configurable via setClearOnShrink() or PI_CLEAR_ON_SHRINK=0 env var
|
|
255
|
+
if (this.getClearOnShrink() && newLines.length < this.maxLinesRendered && !this.hasOverlayEntries) {
|
|
256
|
+
logRedraw(`clearOnShrink (maxLinesRendered=${this.maxLinesRendered})`);
|
|
257
|
+
fullRender(true);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
// Find first and last changed lines
|
|
261
|
+
let firstChanged = -1;
|
|
262
|
+
let lastChanged = -1;
|
|
263
|
+
const maxLines = Math.max(newLines.length, this.previousLines.length);
|
|
264
|
+
for (let i = 0; i < maxLines; i++) {
|
|
265
|
+
const oldLine = i < this.previousLines.length ? this.previousLines[i] : "";
|
|
266
|
+
const newLine = i < newLines.length ? newLines[i] : "";
|
|
267
|
+
if (oldLine !== newLine) {
|
|
268
|
+
if (firstChanged === -1) {
|
|
269
|
+
firstChanged = i;
|
|
270
|
+
}
|
|
271
|
+
lastChanged = i;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
const appendedLines = newLines.length > this.previousLines.length;
|
|
275
|
+
if (appendedLines) {
|
|
276
|
+
if (firstChanged === -1) {
|
|
277
|
+
firstChanged = this.previousLines.length;
|
|
278
|
+
}
|
|
279
|
+
lastChanged = newLines.length - 1;
|
|
280
|
+
}
|
|
281
|
+
if (firstChanged !== -1) {
|
|
282
|
+
const expandedRange = this.expandChangedRangeForKittyImages(firstChanged, lastChanged, newLines);
|
|
283
|
+
firstChanged = expandedRange.firstChanged;
|
|
284
|
+
lastChanged = expandedRange.lastChanged;
|
|
285
|
+
}
|
|
286
|
+
const appendStart = appendedLines && firstChanged === this.previousLines.length && firstChanged > 0;
|
|
287
|
+
// No changes - but still need to update hardware cursor position if it moved
|
|
288
|
+
if (firstChanged === -1) {
|
|
289
|
+
this.positionHardwareCursor(cursorPos, newLines.length);
|
|
290
|
+
this.previousViewportTop = prevViewportTop;
|
|
291
|
+
this.previousHeight = height;
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
// All changes are in deleted lines (nothing to render, just clear)
|
|
295
|
+
if (firstChanged >= newLines.length) {
|
|
296
|
+
if (this.previousLines.length > newLines.length) {
|
|
297
|
+
let buffer = "\x1b[?2026h";
|
|
298
|
+
buffer += this.deleteChangedKittyImages(firstChanged, lastChanged);
|
|
299
|
+
// Move to end of new content (clamp to 0 for empty content)
|
|
300
|
+
const targetRow = Math.max(0, newLines.length - 1);
|
|
301
|
+
if (targetRow < prevViewportTop) {
|
|
302
|
+
logRedraw(`deleted lines moved viewport up (${targetRow} < ${prevViewportTop})`);
|
|
303
|
+
fullRender(true);
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
const lineDiff = computeLineDiff(targetRow);
|
|
307
|
+
if (lineDiff > 0)
|
|
308
|
+
buffer += `\x1b[${lineDiff}B`;
|
|
309
|
+
else if (lineDiff < 0)
|
|
310
|
+
buffer += `\x1b[${-lineDiff}A`;
|
|
311
|
+
buffer += "\r";
|
|
312
|
+
// Clear extra lines without scrolling
|
|
313
|
+
const extraLines = this.previousLines.length - newLines.length;
|
|
314
|
+
if (extraLines > height) {
|
|
315
|
+
logRedraw(`extraLines > height (${extraLines} > ${height})`);
|
|
316
|
+
fullRender(true);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const clearStartOffset = newLines.length === 0 ? 0 : 1;
|
|
320
|
+
if (extraLines > 0 && clearStartOffset > 0) {
|
|
321
|
+
buffer += `\x1b[${clearStartOffset}B`;
|
|
322
|
+
}
|
|
323
|
+
for (let i = 0; i < extraLines; i++) {
|
|
324
|
+
buffer += "\r\x1b[2K";
|
|
325
|
+
if (i < extraLines - 1)
|
|
326
|
+
buffer += "\x1b[1B";
|
|
327
|
+
}
|
|
328
|
+
const moveBack = Math.max(0, extraLines - 1 + clearStartOffset);
|
|
329
|
+
if (moveBack > 0) {
|
|
330
|
+
buffer += `\x1b[${moveBack}A`;
|
|
331
|
+
}
|
|
332
|
+
buffer += "\x1b[?2026l";
|
|
333
|
+
this.terminal.write(buffer);
|
|
334
|
+
this.cursorRow = targetRow;
|
|
335
|
+
this.hardwareCursorRow = targetRow;
|
|
336
|
+
}
|
|
337
|
+
this.positionHardwareCursor(cursorPos, newLines.length);
|
|
338
|
+
this.previousLines = newLines;
|
|
339
|
+
this.previousKittyImageIds = this.collectKittyImageIds(newLines);
|
|
340
|
+
this.previousWidth = width;
|
|
341
|
+
this.previousHeight = height;
|
|
342
|
+
this.previousViewportTop = prevViewportTop;
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
// Differential rendering can only touch what was actually visible.
|
|
346
|
+
// If the first changed line is above the previous viewport, we need a full redraw.
|
|
347
|
+
if (firstChanged < prevViewportTop) {
|
|
348
|
+
logRedraw(`firstChanged < viewportTop (${firstChanged} < ${prevViewportTop})`);
|
|
349
|
+
fullRender(true);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
// Render from first changed line to end
|
|
353
|
+
// Build buffer with all updates wrapped in synchronized output
|
|
354
|
+
let buffer = "\x1b[?2026h"; // Begin synchronized output
|
|
355
|
+
buffer += this.deleteChangedKittyImages(firstChanged, lastChanged);
|
|
356
|
+
const prevViewportBottom = prevViewportTop + height - 1;
|
|
357
|
+
const moveTargetRow = appendStart ? firstChanged - 1 : firstChanged;
|
|
358
|
+
if (moveTargetRow > prevViewportBottom) {
|
|
359
|
+
const currentScreenRow = Math.max(0, Math.min(height - 1, hardwareCursorRow - prevViewportTop));
|
|
360
|
+
const moveToBottom = height - 1 - currentScreenRow;
|
|
361
|
+
if (moveToBottom > 0) {
|
|
362
|
+
buffer += `\x1b[${moveToBottom}B`;
|
|
363
|
+
}
|
|
364
|
+
const scroll = moveTargetRow - prevViewportBottom;
|
|
365
|
+
buffer += "\r\n".repeat(scroll);
|
|
366
|
+
prevViewportTop += scroll;
|
|
367
|
+
viewportTop += scroll;
|
|
368
|
+
hardwareCursorRow = moveTargetRow;
|
|
369
|
+
}
|
|
370
|
+
// Move cursor to first changed line (use hardwareCursorRow for actual position)
|
|
371
|
+
const lineDiff = computeLineDiff(moveTargetRow);
|
|
372
|
+
if (lineDiff > 0) {
|
|
373
|
+
buffer += `\x1b[${lineDiff}B`; // Move down
|
|
374
|
+
}
|
|
375
|
+
else if (lineDiff < 0) {
|
|
376
|
+
buffer += `\x1b[${-lineDiff}A`; // Move up
|
|
377
|
+
}
|
|
378
|
+
buffer += appendStart ? "\r\n" : "\r"; // Move to column 0
|
|
379
|
+
// Only render changed lines (firstChanged to lastChanged), not all lines to end
|
|
380
|
+
// This reduces flicker when only a single line changes (e.g., spinner animation)
|
|
381
|
+
const renderEnd = Math.min(lastChanged, newLines.length - 1);
|
|
382
|
+
for (let i = firstChanged; i <= renderEnd; i++) {
|
|
383
|
+
if (i > firstChanged)
|
|
384
|
+
buffer += "\r\n";
|
|
385
|
+
const line = newLines[i];
|
|
386
|
+
const isImage = isImageLine(line);
|
|
387
|
+
const imageReservedRows = isImage ? this.getKittyImageReservedRows(newLines, i, renderEnd) : 1;
|
|
388
|
+
if (imageReservedRows > 1) {
|
|
389
|
+
const imageStartScreenRow = i - viewportTop;
|
|
390
|
+
if (imageStartScreenRow < 0 || imageStartScreenRow + imageReservedRows > height) {
|
|
391
|
+
logRedraw(`kitty image pre-clear would scroll (${imageStartScreenRow} + ${imageReservedRows} > ${height})`);
|
|
392
|
+
fullRender(true);
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
buffer += "\x1b[2K";
|
|
396
|
+
for (let row = 1; row < imageReservedRows; row++) {
|
|
397
|
+
buffer += "\r\n\x1b[2K";
|
|
398
|
+
}
|
|
399
|
+
buffer += `\x1b[${imageReservedRows - 1}A`;
|
|
400
|
+
buffer += line;
|
|
401
|
+
buffer += `\x1b[${imageReservedRows - 1}B`;
|
|
402
|
+
i += imageReservedRows - 1;
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
buffer += "\x1b[2K"; // Clear current line
|
|
406
|
+
if (!isImage && visibleWidth(line) > width) {
|
|
407
|
+
// Log all lines to crash file for debugging
|
|
408
|
+
const crashLogPath = path.join(this.logDirectory, "pi-crash.log");
|
|
409
|
+
const crashData = [
|
|
410
|
+
`Crash at ${new Date().toISOString()}`,
|
|
411
|
+
`Terminal width: ${width}`,
|
|
412
|
+
`Line ${i} visible width: ${visibleWidth(line)}`,
|
|
413
|
+
"",
|
|
414
|
+
"=== All rendered lines ===",
|
|
415
|
+
...newLines.map((l, idx) => `[${idx}] (w=${visibleWidth(l)}) ${l}`),
|
|
416
|
+
"",
|
|
417
|
+
].join("\n");
|
|
418
|
+
fs.mkdirSync(path.dirname(crashLogPath), { recursive: true });
|
|
419
|
+
fs.writeFileSync(crashLogPath, crashData);
|
|
420
|
+
// Clean up terminal state before throwing
|
|
421
|
+
this.stop();
|
|
422
|
+
const errorMsg = [
|
|
423
|
+
`Rendered line ${i} exceeds terminal width (${visibleWidth(line)} > ${width}).`,
|
|
424
|
+
"",
|
|
425
|
+
"This is likely caused by a custom TUI component not truncating its output.",
|
|
426
|
+
"Use visibleWidth() to measure and truncateToWidth() to truncate lines.",
|
|
427
|
+
"",
|
|
428
|
+
`Debug log written to: ${crashLogPath}`,
|
|
429
|
+
].join("\n");
|
|
430
|
+
throw new Error(errorMsg);
|
|
431
|
+
}
|
|
432
|
+
buffer += line;
|
|
433
|
+
}
|
|
434
|
+
// Track where cursor ended up after rendering
|
|
435
|
+
let finalCursorRow = renderEnd;
|
|
436
|
+
// If we had more lines before, clear them and move cursor back
|
|
437
|
+
if (this.previousLines.length > newLines.length) {
|
|
438
|
+
// Move to end of new content first if we stopped before it
|
|
439
|
+
if (renderEnd < newLines.length - 1) {
|
|
440
|
+
const moveDown = newLines.length - 1 - renderEnd;
|
|
441
|
+
buffer += `\x1b[${moveDown}B`;
|
|
442
|
+
finalCursorRow = newLines.length - 1;
|
|
443
|
+
}
|
|
444
|
+
const extraLines = this.previousLines.length - newLines.length;
|
|
445
|
+
for (let i = newLines.length; i < this.previousLines.length; i++) {
|
|
446
|
+
buffer += "\r\n\x1b[2K";
|
|
447
|
+
}
|
|
448
|
+
// Move cursor back to end of new content
|
|
449
|
+
buffer += `\x1b[${extraLines}A`;
|
|
450
|
+
}
|
|
451
|
+
buffer += "\x1b[?2026l"; // End synchronized output
|
|
452
|
+
if (process.env.PI_TUI_DEBUG === "1") {
|
|
453
|
+
const debugDir = "/tmp/tui";
|
|
454
|
+
fs.mkdirSync(debugDir, { recursive: true });
|
|
455
|
+
const debugPath = path.join(debugDir, `render-${Date.now()}-${Math.random().toString(36).slice(2)}.log`);
|
|
456
|
+
const debugData = [
|
|
457
|
+
`firstChanged: ${firstChanged}`,
|
|
458
|
+
`viewportTop: ${viewportTop}`,
|
|
459
|
+
`cursorRow: ${this.cursorRow}`,
|
|
460
|
+
`height: ${height}`,
|
|
461
|
+
`lineDiff: ${lineDiff}`,
|
|
462
|
+
`hardwareCursorRow: ${hardwareCursorRow}`,
|
|
463
|
+
`renderEnd: ${renderEnd}`,
|
|
464
|
+
`finalCursorRow: ${finalCursorRow}`,
|
|
465
|
+
`cursorPos: ${JSON.stringify(cursorPos)}`,
|
|
466
|
+
`newLines.length: ${newLines.length}`,
|
|
467
|
+
`previousLines.length: ${this.previousLines.length}`,
|
|
468
|
+
"",
|
|
469
|
+
"=== newLines ===",
|
|
470
|
+
JSON.stringify(newLines, null, 2),
|
|
471
|
+
"",
|
|
472
|
+
"=== previousLines ===",
|
|
473
|
+
JSON.stringify(this.previousLines, null, 2),
|
|
474
|
+
"",
|
|
475
|
+
"=== buffer ===",
|
|
476
|
+
JSON.stringify(buffer),
|
|
477
|
+
].join("\n");
|
|
478
|
+
fs.writeFileSync(debugPath, debugData);
|
|
479
|
+
}
|
|
480
|
+
// Write entire buffer at once
|
|
481
|
+
this.terminal.write(buffer);
|
|
482
|
+
// Track cursor position for next render
|
|
483
|
+
// cursorRow tracks end of content (for viewport calculation)
|
|
484
|
+
// hardwareCursorRow tracks actual terminal cursor position (for movement)
|
|
485
|
+
this.cursorRow = Math.max(0, newLines.length - 1);
|
|
486
|
+
this.hardwareCursorRow = finalCursorRow;
|
|
487
|
+
// Track terminal's working area (grows but doesn't shrink unless cleared)
|
|
488
|
+
this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);
|
|
489
|
+
this.previousViewportTop = Math.max(prevViewportTop, finalCursorRow - height + 1);
|
|
490
|
+
// Position hardware cursor for IME
|
|
491
|
+
this.positionHardwareCursor(cursorPos, newLines.length);
|
|
492
|
+
this.previousLines = newLines;
|
|
493
|
+
this.previousKittyImageIds = this.collectKittyImageIds(newLines);
|
|
494
|
+
this.previousWidth = width;
|
|
495
|
+
this.previousHeight = height;
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Position the hardware cursor for IME candidate window.
|
|
499
|
+
* @param cursorPos The cursor position extracted from rendered output, or null
|
|
500
|
+
* @param totalLines Total number of rendered lines
|
|
501
|
+
*/
|
|
502
|
+
positionHardwareCursor(cursorPos, totalLines) {
|
|
503
|
+
if (!cursorPos || totalLines <= 0) {
|
|
504
|
+
this.terminal.hideCursor();
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
// Clamp cursor position to valid range
|
|
508
|
+
const targetRow = Math.max(0, Math.min(cursorPos.row, totalLines - 1));
|
|
509
|
+
const targetCol = Math.max(0, cursorPos.col);
|
|
510
|
+
// Move cursor from current position to target
|
|
511
|
+
const rowDelta = targetRow - this.hardwareCursorRow;
|
|
512
|
+
let buffer = "";
|
|
513
|
+
if (rowDelta > 0) {
|
|
514
|
+
buffer += `\x1b[${rowDelta}B`; // Move down
|
|
515
|
+
}
|
|
516
|
+
else if (rowDelta < 0) {
|
|
517
|
+
buffer += `\x1b[${-rowDelta}A`; // Move up
|
|
518
|
+
}
|
|
519
|
+
// Move to absolute column (1-indexed)
|
|
520
|
+
buffer += `\x1b[${targetCol + 1}G`;
|
|
521
|
+
if (buffer) {
|
|
522
|
+
this.terminal.write(buffer);
|
|
523
|
+
}
|
|
524
|
+
this.hardwareCursorRow = targetRow;
|
|
525
|
+
if (this.getShowHardwareCursor()) {
|
|
526
|
+
this.terminal.showCursor();
|
|
527
|
+
}
|
|
528
|
+
else {
|
|
529
|
+
this.terminal.hideCursor();
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
//# sourceMappingURL=tui-main-screen.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tui-main-screen.js","sourceRoot":"","sources":["../src/tui-main-screen.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACpE,OAAO,EAAY,OAAO,EAAuB,MAAM,UAAU,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE1C,MAAM,qBAAqB,GAAG,QAAQ,CAAC;AAOvC,SAAS,qBAAqB,CAAC,IAAY,EAAgC;IAC1E,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,CAAC,qBAAqB,CAAC,CAAC;IAC1D,IAAI,aAAa,KAAK,CAAC,CAAC;QAAE,OAAO,SAAS,CAAC;IAC3C,MAAM,WAAW,GAAG,aAAa,GAAG,qBAAqB,CA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Content":["import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport { deleteKittyImage, isImageLine } from \"./terminal-image.ts\";\nimport { type TUI, TuiBase, type TuiStopOptions } from \"./tui.ts\";\nimport { visibleWidth } from \"./utils.ts\";\n\nconst KITTY_SEQUENCE_PREFIX = \"\\x1b_G\";\n\ninterface KittyImageHeader {\n\tids: number[];\n\trows: number;\n}\n\nfunction parseKittyImageHeader(line: string): KittyImageHeader | undefined {\n\tconst sequenceStart = line.indexOf(KITTY_SEQUENCE_PREFIX);\n\tif (sequenceStart === -1) return undefined;\n\tconst paramsStart = sequenceStart + KITTY_SEQUENCE_PREFIX.length;\n\tconst paramsEnd = line.indexOf(\";\", paramsStart);\n\tif (paramsEnd === -1) return undefined;\n\n\tconst ids: number[] = [];\n\tlet rows = 1;\n\tfor (const param of line.slice(paramsStart, paramsEnd).split(\",\")) {\n\t\tconst [key, value] = param.split(\"=\", 2);\n\t\tif (value === undefined) continue;\n\t\tconst numberValue = Number(value);\n\t\tif (!Number.isInteger(numberValue) || numberValue <= 0 || numberValue > 0xffffffff) continue;\n\t\tif (key === \"i\") ids.push(numberValue);\n\t\telse if (key === \"r\") rows = numberValue;\n\t}\n\treturn { ids, rows };\n}\n\nfunction extractKittyImageIds(line: string): number[] {\n\treturn parseKittyImageHeader(line)?.ids ?? [];\n}\n\nfunction extractKittyImageRows(line: string): number {\n\treturn parseKittyImageHeader(line)?.rows ?? 1;\n}\n\nfunction isTermuxSession(): boolean {\n\treturn Boolean(process.env.TERMUX_VERSION);\n}\n\nexport interface TuiMainScreenRenderState {\n\tpreviousLines: string[];\n\tpreviousWidth: number;\n\tpreviousHeight: number;\n\tcursorRow: number;\n\thardwareCursorRow: number;\n\tmaxLinesRendered: number;\n\tpreviousViewportTop: number;\n}\n\n/** TUI implementation that renders into the terminal's main screen and scrollback. */\nexport class TuiMainScreen extends TuiBase implements TUI {\n\treadonly mode = \"regular\" as const;\n\tprivate previousLines: string[] = [];\n\tprivate previousKittyImageIds = new Set<number>();\n\tprivate previousWidth = 0;\n\tprivate previousHeight = 0;\n\tprivate cursorRow = 0;\n\tprivate hardwareCursorRow = 0;\n\tprivate maxLinesRendered = 0;\n\tprivate previousViewportTop = 0;\n\n\tcaptureRenderState(): TuiMainScreenRenderState {\n\t\treturn {\n\t\t\tpreviousLines: [...this.previousLines],\n\t\t\tpreviousWidth: this.previousWidth,\n\t\t\tpreviousHeight: this.previousHeight,\n\t\t\tcursorRow: this.cursorRow,\n\t\t\thardwareCursorRow: this.hardwareCursorRow,\n\t\t\tmaxLinesRendered: this.maxLinesRendered,\n\t\t\tpreviousViewportTop: this.previousViewportTop,\n\t\t};\n\t}\n\n\trestoreRenderState(state: TuiMainScreenRenderState): void {\n\t\tthis.previousLines = state.previousLines.map((line) => (isImageLine(line) ? \"\" : line));\n\t\tthis.previousKittyImageIds = new Set();\n\t\tthis.previousWidth = state.previousWidth;\n\t\tthis.previousHeight = state.previousHeight;\n\t\tthis.cursorRow = state.cursorRow;\n\t\tthis.hardwareCursorRow = state.hardwareCursorRow;\n\t\tthis.maxLinesRendered = state.maxLinesRendered;\n\t\tthis.previousViewportTop = state.previousViewportTop;\n\t}\n\n\tprotected override resetRenderState(): void {\n\t\tthis.previousLines = [];\n\t\tthis.previousWidth = -1;\n\t\tthis.previousHeight = -1;\n\t\tthis.cursorRow = 0;\n\t\tthis.hardwareCursorRow = 0;\n\t\tthis.maxLinesRendered = 0;\n\t\tthis.previousViewportTop = 0;\n\t}\n\n\tprotected override beforeTerminalStop(options: TuiStopOptions): void {\n\t\tif (options.preserveScreen || this.previousLines.length === 0) return;\n\t\tthis.terminal.write(\" \");\n\t\tconst targetRow = this.previousLines.length;\n\t\tconst lineDiff = targetRow - this.hardwareCursorRow;\n\t\tif (lineDiff > 0) this.terminal.write(`\\x1b[${lineDiff}B`);\n\t\telse if (lineDiff < 0) this.terminal.write(`\\x1b[${-lineDiff}A`);\n\t\tthis.terminal.write(\"\\r\\n\");\n\t}\n\n\tprivate collectKittyImageIds(lines: string[]): Set<number> {\n\t\tconst ids = new Set<number>();\n\t\tfor (const line of lines) {\n\t\t\tfor (const id of extractKittyImageIds(line)) {\n\t\t\t\tids.add(id);\n\t\t\t}\n\t\t}\n\t\treturn ids;\n\t}\n\n\tprivate deleteKittyImages(ids: Iterable<number>): string {\n\t\tlet buffer = \"\";\n\t\tfor (const id of ids) {\n\t\t\tbuffer += deleteKittyImage(id);\n\t\t}\n\t\treturn buffer;\n\t}\n\n\tprivate getKittyImageReservedRows(lines: string[], index: number, maxIndex = lines.length - 1): number {\n\t\tconst rows = extractKittyImageRows(lines[index] ?? \"\");\n\t\tif (rows <= 1) return 1;\n\n\t\tconst maxRows = Math.min(rows, maxIndex - index + 1, lines.length - index);\n\t\tlet reservedRows = 1;\n\t\twhile (reservedRows < maxRows) {\n\t\t\tconst line = lines[index + reservedRows] ?? \"\";\n\t\t\tif (isImageLine(line) || visibleWidth(line) > 0) break;\n\t\t\treservedRows++;\n\t\t}\n\t\treturn reservedRows;\n\t}\n\n\tprivate expandChangedRangeForKittyImages(\n\t\tfirstChanged: number,\n\t\tlastChanged: number,\n\t\tnewLines: string[],\n\t): { firstChanged: number; lastChanged: number } {\n\t\tlet expandedFirstChanged = firstChanged;\n\t\tlet expandedLastChanged = lastChanged;\n\t\tconst expandForLines = (lines: string[]): void => {\n\t\t\tfor (let i = 0; i < lines.length; i++) {\n\t\t\t\tif (extractKittyImageIds(lines[i]).length === 0) continue;\n\t\t\t\tconst blockEnd = i + this.getKittyImageReservedRows(lines, i) - 1;\n\t\t\t\tif (i >= firstChanged || (i <= lastChanged && blockEnd >= firstChanged)) {\n\t\t\t\t\texpandedFirstChanged = Math.min(expandedFirstChanged, i);\n\t\t\t\t\texpandedLastChanged = Math.max(expandedLastChanged, blockEnd);\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\n\t\texpandForLines(this.previousLines);\n\t\texpandForLines(newLines);\n\t\treturn { firstChanged: expandedFirstChanged, lastChanged: expandedLastChanged };\n\t}\n\n\tprivate deleteChangedKittyImages(firstChanged: number, lastChanged: number): string {\n\t\tif (firstChanged < 0 || lastChanged < firstChanged) return \"\";\n\n\t\tconst ids = new Set<number>();\n\t\tconst maxLine = Math.min(lastChanged, this.previousLines.length - 1);\n\t\tfor (let i = firstChanged; i <= maxLine; i++) {\n\t\t\tfor (const id of extractKittyImageIds(this.previousLines[i] ?? \"\")) {\n\t\t\t\tids.add(id);\n\t\t\t}\n\t\t}\n\n\t\treturn this.deleteKittyImages(ids);\n\t}\n\n\tprotected doRender(): void {\n\t\tif (this.stopped) return;\n\t\tconst width = this.terminal.columns;\n\t\tconst height = this.terminal.rows;\n\t\tconst widthChanged = this.previousWidth !== 0 && this.previousWidth !== width;\n\t\tconst heightChanged = this.previousHeight !== 0 && this.previousHeight !== height;\n\t\tconst previousBufferLength = this.previousHeight > 0 ? this.previousViewportTop + this.previousHeight : height;\n\t\tlet prevViewportTop = heightChanged ? Math.max(0, previousBufferLength - height) : this.previousViewportTop;\n\t\tlet viewportTop = prevViewportTop;\n\t\tlet hardwareCursorRow = this.hardwareCursorRow;\n\t\tconst computeLineDiff = (targetRow: number): number => {\n\t\t\tconst currentScreenRow = hardwareCursorRow - prevViewportTop;\n\t\t\tconst targetScreenRow = targetRow - viewportTop;\n\t\t\treturn targetScreenRow - currentScreenRow;\n\t\t};\n\n\t\t// Render all components to get new lines\n\t\tlet newLines = this.render(width);\n\n\t\t// Composite overlays into the rendered lines (before differential compare)\n\t\tif (this.hasOverlayEntries) {\n\t\t\tnewLines = this.compositeOverlays(newLines, width, height);\n\t\t}\n\n\t\t// Extract cursor position before applying line resets (marker must be found first)\n\t\tconst cursorPos = this.extractCursorPosition(newLines, height);\n\n\t\tnewLines = this.applyLineResets(newLines);\n\n\t\t// Helper to clear scrollback and viewport and render all new lines\n\t\tconst fullRender = (clear: boolean): void => {\n\t\t\tthis.fullRedrawCount += 1;\n\t\t\tlet buffer = \"\\x1b[?2026h\"; // Begin synchronized output\n\t\t\tif (clear) {\n\t\t\t\tbuffer += this.deleteKittyImages(this.previousKittyImageIds);\n\t\t\t\tbuffer += \"\\x1b[2J\\x1b[H\\x1b[3J\"; // Clear screen, home, then clear scrollback\n\t\t\t}\n\t\t\tfor (let i = 0; i < newLines.length; i++) {\n\t\t\t\tif (i > 0) buffer += \"\\r\\n\";\n\t\t\t\tconst line = newLines[i];\n\t\t\t\tconst isImage = isImageLine(line);\n\t\t\t\tconst imageReservedRows = isImage ? this.getKittyImageReservedRows(newLines, i) : 1;\n\t\t\t\tif (imageReservedRows > 1 && imageReservedRows <= height) {\n\t\t\t\t\tfor (let row = 1; row < imageReservedRows; row++) {\n\t\t\t\t\t\tbuffer += \"\\r\\n\";\n\t\t\t\t\t}\n\t\t\t\t\tbuffer += `\\x1b[${imageReservedRows - 1}A`;\n\t\t\t\t\tbuffer += line;\n\t\t\t\t\tbuffer += `\\x1b[${imageReservedRows - 1}B`;\n\t\t\t\t\ti += imageReservedRows - 1;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tbuffer += line;\n\t\t\t}\n\t\t\tbuffer += \"\\x1b[?2026l\"; // End synchronized output\n\t\t\tthis.terminal.write(buffer);\n\t\t\tthis.cursorRow = Math.max(0, newLines.length - 1);\n\t\t\tthis.hardwareCursorRow = this.cursorRow;\n\t\t\t// Reset max lines when clearing, otherwise track growth\n\t\t\tif (clear) {\n\t\t\t\tthis.maxLinesRendered = newLines.length;\n\t\t\t} else {\n\t\t\t\tthis.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);\n\t\t\t}\n\t\t\tconst bufferLength = Math.max(height, newLines.length);\n\t\t\tthis.previousViewportTop = Math.max(0, bufferLength - height);\n\t\t\tthis.positionHardwareCursor(cursorPos, newLines.length);\n\t\t\tthis.previousLines = newLines;\n\t\t\tthis.previousKittyImageIds = this.collectKittyImageIds(newLines);\n\t\t\tthis.previousWidth = width;\n\t\t\tthis.previousHeight = height;\n\t\t};\n\n\t\tconst debugRedraw = process.env.PI_DEBUG_REDRAW === \"1\";\n\t\tconst logRedraw = (reason: string): void => {\n\t\t\tif (!debugRedraw) return;\n\t\t\tconst logPath = path.join(this.logDirectory, \"pi-debug.log\");\n\t\t\tconst msg = `[${new Date().toISOString()}] fullRender: ${reason} (prev=${this.previousLines.length}, new=${newLines.length}, height=${height})\\n`;\n\t\t\tfs.mkdirSync(path.dirname(logPath), { recursive: true });\n\t\t\tfs.appendFileSync(logPath, msg);\n\t\t};\n\n\t\t// First render - just output everything without clearing (assumes clean screen)\n\t\tif (this.previousLines.length === 0 && !widthChanged && !heightChanged) {\n\t\t\tlogRedraw(\"first render\");\n\t\t\tfullRender(false);\n\t\t\treturn;\n\t\t}\n\n\t\t// Width changes always need a full re-render because wrapping changes.\n\t\tif (widthChanged) {\n\t\t\tlogRedraw(`terminal width changed (${this.previousWidth} -> ${width})`);\n\t\t\tfullRender(true);\n\t\t\treturn;\n\t\t}\n\n\t\t// Height changes normally need a full re-render to keep the visible viewport aligned,\n\t\t// but Termux changes height when the software keyboard shows or hides.\n\t\t// In that environment, a full redraw causes the entire history to replay on every toggle.\n\t\tif (heightChanged && !isTermuxSession()) {\n\t\t\tlogRedraw(`terminal height changed (${this.previousHeight} -> ${height})`);\n\t\t\tfullRender(true);\n\t\t\treturn;\n\t\t}\n\n\t\t// Content shrunk below the working area and no overlays - re-render to clear empty rows\n\t\t// (overlays need the padding, so only do this when no overlays are active)\n\t\t// Configurable via setClearOnShrink() or PI_CLEAR_ON_SHRINK=0 env var\n\t\tif (this.getClearOnShrink() && newLines.length < this.maxLinesRendered && !this.hasOverlayEntries) {\n\t\t\tlogRedraw(`clearOnShrink (maxLinesRendered=${this.maxLinesRendered})`);\n\t\t\tfullRender(true);\n\t\t\treturn;\n\t\t}\n\n\t\t// Find first and last changed lines\n\t\tlet firstChanged = -1;\n\t\tlet lastChanged = -1;\n\t\tconst maxLines = Math.max(newLines.length, this.previousLines.length);\n\t\tfor (let i = 0; i < maxLines; i++) {\n\t\t\tconst oldLine = i < this.previousLines.length ? this.previousLines[i] : \"\";\n\t\t\tconst newLine = i < newLines.length ? newLines[i] : \"\";\n\n\t\t\tif (oldLine !== newLine) {\n\t\t\t\tif (firstChanged === -1) {\n\t\t\t\t\tfirstChanged = i;\n\t\t\t\t}\n\t\t\t\tlastChanged = i;\n\t\t\t}\n\t\t}\n\t\tconst appendedLines = newLines.length > this.previousLines.length;\n\t\tif (appendedLines) {\n\t\t\tif (firstChanged === -1) {\n\t\t\t\tfirstChanged = this.previousLines.length;\n\t\t\t}\n\t\t\tlastChanged = newLines.length - 1;\n\t\t}\n\t\tif (firstChanged !== -1) {\n\t\t\tconst expandedRange = this.expandChangedRangeForKittyImages(firstChanged, lastChanged, newLines);\n\t\t\tfirstChanged = expandedRange.firstChanged;\n\t\t\tlastChanged = expandedRange.lastChanged;\n\t\t}\n\t\tconst appendStart = appendedLines && firstChanged === this.previousLines.length && firstChanged > 0;\n\n\t\t// No changes - but still need to update hardware cursor position if it moved\n\t\tif (firstChanged === -1) {\n\t\t\tthis.positionHardwareCursor(cursorPos, newLines.length);\n\t\t\tthis.previousViewportTop = prevViewportTop;\n\t\t\tthis.previousHeight = height;\n\t\t\treturn;\n\t\t}\n\n\t\t// All changes are in deleted lines (nothing to render, just clear)\n\t\tif (firstChanged >= newLines.length) {\n\t\t\tif (this.previousLines.length > newLines.length) {\n\t\t\t\tlet buffer = \"\\x1b[?2026h\";\n\t\t\t\tbuffer += this.deleteChangedKittyImages(firstChanged, lastChanged);\n\t\t\t\t// Move to end of new content (clamp to 0 for empty content)\n\t\t\t\tconst targetRow = Math.max(0, newLines.length - 1);\n\t\t\t\tif (targetRow < prevViewportTop) {\n\t\t\t\t\tlogRedraw(`deleted lines moved viewport up (${targetRow} < ${prevViewportTop})`);\n\t\t\t\t\tfullRender(true);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst lineDiff = computeLineDiff(targetRow);\n\t\t\t\tif (lineDiff > 0) buffer += `\\x1b[${lineDiff}B`;\n\t\t\t\telse if (lineDiff < 0) buffer += `\\x1b[${-lineDiff}A`;\n\t\t\t\tbuffer += \"\\r\";\n\t\t\t\t// Clear extra lines without scrolling\n\t\t\t\tconst extraLines = this.previousLines.length - newLines.length;\n\t\t\t\tif (extraLines > height) {\n\t\t\t\t\tlogRedraw(`extraLines > height (${extraLines} > ${height})`);\n\t\t\t\t\tfullRender(true);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst clearStartOffset = newLines.length === 0 ? 0 : 1;\n\t\t\t\tif (extraLines > 0 && clearStartOffset > 0) {\n\t\t\t\t\tbuffer += `\\x1b[${clearStartOffset}B`;\n\t\t\t\t}\n\t\t\t\tfor (let i = 0; i < extraLines; i++) {\n\t\t\t\t\tbuffer += \"\\r\\x1b[2K\";\n\t\t\t\t\tif (i < extraLines - 1) buffer += \"\\x1b[1B\";\n\t\t\t\t}\n\t\t\t\tconst moveBack = Math.max(0, extraLines - 1 + clearStartOffset);\n\t\t\t\tif (moveBack > 0) {\n\t\t\t\t\tbuffer += `\\x1b[${moveBack}A`;\n\t\t\t\t}\n\t\t\t\tbuffer += \"\\x1b[?2026l\";\n\t\t\t\tthis.terminal.write(buffer);\n\t\t\t\tthis.cursorRow = targetRow;\n\t\t\t\tthis.hardwareCursorRow = targetRow;\n\t\t\t}\n\t\t\tthis.positionHardwareCursor(cursorPos, newLines.length);\n\t\t\tthis.previousLines = newLines;\n\t\t\tthis.previousKittyImageIds = this.collectKittyImageIds(newLines);\n\t\t\tthis.previousWidth = width;\n\t\t\tthis.previousHeight = height;\n\t\t\tthis.previousViewportTop = prevViewportTop;\n\t\t\treturn;\n\t\t}\n\n\t\t// Differential rendering can only touch what was actually visible.\n\t\t// If the first changed line is above the previous viewport, we need a full redraw.\n\t\tif (firstChanged < prevViewportTop) {\n\t\t\tlogRedraw(`firstChanged < viewportTop (${firstChanged} < ${prevViewportTop})`);\n\t\t\tfullRender(true);\n\t\t\treturn;\n\t\t}\n\n\t\t// Render from first changed line to end\n\t\t// Build buffer with all updates wrapped in synchronized output\n\t\tlet buffer = \"\\x1b[?2026h\"; // Begin synchronized output\n\t\tbuffer += this.deleteChangedKittyImages(firstChanged, lastChanged);\n\t\tconst prevViewportBottom = prevViewportTop + height - 1;\n\t\tconst moveTargetRow = appendStart ? firstChanged - 1 : firstChanged;\n\t\tif (moveTargetRow > prevViewportBottom) {\n\t\t\tconst currentScreenRow = Math.max(0, Math.min(height - 1, hardwareCursorRow - prevViewportTop));\n\t\t\tconst moveToBottom = height - 1 - currentScreenRow;\n\t\t\tif (moveToBottom > 0) {\n\t\t\t\tbuffer += `\\x1b[${moveToBottom}B`;\n\t\t\t}\n\t\t\tconst scroll = moveTargetRow - prevViewportBottom;\n\t\t\tbuffer += \"\\r\\n\".repeat(scroll);\n\t\t\tprevViewportTop += scroll;\n\t\t\tviewportTop += scroll;\n\t\t\thardwareCursorRow = moveTargetRow;\n\t\t}\n\n\t\t// Move cursor to first changed line (use hardwareCursorRow for actual position)\n\t\tconst lineDiff = computeLineDiff(moveTargetRow);\n\t\tif (lineDiff > 0) {\n\t\t\tbuffer += `\\x1b[${lineDiff}B`; // Move down\n\t\t} else if (lineDiff < 0) {\n\t\t\tbuffer += `\\x1b[${-lineDiff}A`; // Move up\n\t\t}\n\n\t\tbuffer += appendStart ? \"\\r\\n\" : \"\\r\"; // Move to column 0\n\n\t\t// Only render changed lines (firstChanged to lastChanged), not all lines to end\n\t\t// This reduces flicker when only a single line changes (e.g., spinner animation)\n\t\tconst renderEnd = Math.min(lastChanged, newLines.length - 1);\n\t\tfor (let i = firstChanged; i <= renderEnd; i++) {\n\t\t\tif (i > firstChanged) buffer += \"\\r\\n\";\n\t\t\tconst line = newLines[i];\n\t\t\tconst isImage = isImageLine(line);\n\t\t\tconst imageReservedRows = isImage ? this.getKittyImageReservedRows(newLines, i, renderEnd) : 1;\n\t\t\tif (imageReservedRows > 1) {\n\t\t\t\tconst imageStartScreenRow = i - viewportTop;\n\t\t\t\tif (imageStartScreenRow < 0 || imageStartScreenRow + imageReservedRows > height) {\n\t\t\t\t\tlogRedraw(\n\t\t\t\t\t\t`kitty image pre-clear would scroll (${imageStartScreenRow} + ${imageReservedRows} > ${height})`,\n\t\t\t\t\t);\n\t\t\t\t\tfullRender(true);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tbuffer += \"\\x1b[2K\";\n\t\t\t\tfor (let row = 1; row < imageReservedRows; row++) {\n\t\t\t\t\tbuffer += \"\\r\\n\\x1b[2K\";\n\t\t\t\t}\n\t\t\t\tbuffer += `\\x1b[${imageReservedRows - 1}A`;\n\t\t\t\tbuffer += line;\n\t\t\t\tbuffer += `\\x1b[${imageReservedRows - 1}B`;\n\t\t\t\ti += imageReservedRows - 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tbuffer += \"\\x1b[2K\"; // Clear current line\n\t\t\tif (!isImage && visibleWidth(line) > width) {\n\t\t\t\t// Log all lines to crash file for debugging\n\t\t\t\tconst crashLogPath = path.join(this.logDirectory, \"pi-crash.log\");\n\t\t\t\tconst crashData = [\n\t\t\t\t\t`Crash at ${new Date().toISOString()}`,\n\t\t\t\t\t`Terminal width: ${width}`,\n\t\t\t\t\t`Line ${i} visible width: ${visibleWidth(line)}`,\n\t\t\t\t\t\"\",\n\t\t\t\t\t\"=== All rendered lines ===\",\n\t\t\t\t\t...newLines.map((l, idx) => `[${idx}] (w=${visibleWidth(l)}) ${l}`),\n\t\t\t\t\t\"\",\n\t\t\t\t].join(\"\\n\");\n\t\t\t\tfs.mkdirSync(path.dirname(crashLogPath), { recursive: true });\n\t\t\t\tfs.writeFileSync(crashLogPath, crashData);\n\n\t\t\t\t// Clean up terminal state before throwing\n\t\t\t\tthis.stop();\n\n\t\t\t\tconst errorMsg = [\n\t\t\t\t\t`Rendered line ${i} exceeds terminal width (${visibleWidth(line)} > ${width}).`,\n\t\t\t\t\t\"\",\n\t\t\t\t\t\"This is likely caused by a custom TUI component not truncating its output.\",\n\t\t\t\t\t\"Use visibleWidth() to measure and truncateToWidth() to truncate lines.\",\n\t\t\t\t\t\"\",\n\t\t\t\t\t`Debug log written to: ${crashLogPath}`,\n\t\t\t\t].join(\"\\n\");\n\t\t\t\tthrow new Error(errorMsg);\n\t\t\t}\n\t\t\tbuffer += line;\n\t\t}\n\n\t\t// Track where cursor ended up after rendering\n\t\tlet finalCursorRow = renderEnd;\n\n\t\t// If we had more lines before, clear them and move cursor back\n\t\tif (this.previousLines.length > newLines.length) {\n\t\t\t// Move to end of new content first if we stopped before it\n\t\t\tif (renderEnd < newLines.length - 1) {\n\t\t\t\tconst moveDown = newLines.length - 1 - renderEnd;\n\t\t\t\tbuffer += `\\x1b[${moveDown}B`;\n\t\t\t\tfinalCursorRow = newLines.length - 1;\n\t\t\t}\n\t\t\tconst extraLines = this.previousLines.length - newLines.length;\n\t\t\tfor (let i = newLines.length; i < this.previousLines.length; i++) {\n\t\t\t\tbuffer += \"\\r\\n\\x1b[2K\";\n\t\t\t}\n\t\t\t// Move cursor back to end of new content\n\t\t\tbuffer += `\\x1b[${extraLines}A`;\n\t\t}\n\n\t\tbuffer += \"\\x1b[?2026l\"; // End synchronized output\n\n\t\tif (process.env.PI_TUI_DEBUG === \"1\") {\n\t\t\tconst debugDir = \"/tmp/tui\";\n\t\t\tfs.mkdirSync(debugDir, { recursive: true });\n\t\t\tconst debugPath = path.join(debugDir, `render-${Date.now()}-${Math.random().toString(36).slice(2)}.log`);\n\t\t\tconst debugData = [\n\t\t\t\t`firstChanged: ${firstChanged}`,\n\t\t\t\t`viewportTop: ${viewportTop}`,\n\t\t\t\t`cursorRow: ${this.cursorRow}`,\n\t\t\t\t`height: ${height}`,\n\t\t\t\t`lineDiff: ${lineDiff}`,\n\t\t\t\t`hardwareCursorRow: ${hardwareCursorRow}`,\n\t\t\t\t`renderEnd: ${renderEnd}`,\n\t\t\t\t`finalCursorRow: ${finalCursorRow}`,\n\t\t\t\t`cursorPos: ${JSON.stringify(cursorPos)}`,\n\t\t\t\t`newLines.length: ${newLines.length}`,\n\t\t\t\t`previousLines.length: ${this.previousLines.length}`,\n\t\t\t\t\"\",\n\t\t\t\t\"=== newLines ===\",\n\t\t\t\tJSON.stringify(newLines, null, 2),\n\t\t\t\t\"\",\n\t\t\t\t\"=== previousLines ===\",\n\t\t\t\tJSON.stringify(this.previousLines, null, 2),\n\t\t\t\t\"\",\n\t\t\t\t\"=== buffer ===\",\n\t\t\t\tJSON.stringify(buffer),\n\t\t\t].join(\"\\n\");\n\t\t\tfs.writeFileSync(debugPath, debugData);\n\t\t}\n\n\t\t// Write entire buffer at once\n\t\tthis.terminal.write(buffer);\n\n\t\t// Track cursor position for next render\n\t\t// cursorRow tracks end of content (for viewport calculation)\n\t\t// hardwareCursorRow tracks actual terminal cursor position (for movement)\n\t\tthis.cursorRow = Math.max(0, newLines.length - 1);\n\t\tthis.hardwareCursorRow = finalCursorRow;\n\t\t// Track terminal's working area (grows but doesn't shrink unless cleared)\n\t\tthis.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);\n\t\tthis.previousViewportTop = Math.max(prevViewportTop, finalCursorRow - height + 1);\n\n\t\t// Position hardware cursor for IME\n\t\tthis.positionHardwareCursor(cursorPos, newLines.length);\n\n\t\tthis.previousLines = newLines;\n\t\tthis.previousKittyImageIds = this.collectKittyImageIds(newLines);\n\t\tthis.previousWidth = width;\n\t\tthis.previousHeight = height;\n\t}\n\n\t/**\n\t * Position the hardware cursor for IME candidate window.\n\t * @param cursorPos The cursor position extracted from rendered output, or null\n\t * @param totalLines Total number of rendered lines\n\t */\n\tprivate positionHardwareCursor(cursorPos: { row: number; col: number } | null, totalLines: number): void {\n\t\tif (!cursorPos || totalLines <= 0) {\n\t\t\tthis.terminal.hideCursor();\n\t\t\treturn;\n\t\t}\n\n\t\t// Clamp cursor position to valid range\n\t\tconst targetRow = Math.max(0, Math.min(cursorPos.row, totalLines - 1));\n\t\tconst targetCol = Math.max(0, cursorPos.col);\n\n\t\t// Move cursor from current position to target\n\t\tconst rowDelta = targetRow - this.hardwareCursorRow;\n\t\tlet buffer = \"\";\n\t\tif (rowDelta > 0) {\n\t\t\tbuffer += `\\x1b[${rowDelta}B`; // Move down\n\t\t} else if (rowDelta < 0) {\n\t\t\tbuffer += `\\x1b[${-rowDelta}A`; // Move up\n\t\t}\n\t\t// Move to absolute column (1-indexed)\n\t\tbuffer += `\\x1b[${targetCol + 1}G`;\n\n\t\tif (buffer) {\n\t\t\tthis.terminal.write(buffer);\n\t\t}\n\n\t\tthis.hardwareCursorRow = targetRow;\n\t\tif (this.getShowHardwareCursor()) {\n\t\t\tthis.terminal.showCursor();\n\t\t} else {\n\t\t\tthis.terminal.hideCursor();\n\t\t}\n\t}\n}\n"]}
|