Files
Netcatty/components/terminal/clearTerminalViewport.ts
T
陈大猫 a0f7be4f2f [codex] Respect scrollback clear setting for terminal clears (#1684)
* Respect scrollback clear setting for terminal clears

* Fix scrollback clear ordering
2026-06-25 19:13:17 +08:00

197 lines
5.0 KiB
TypeScript

import type { Terminal as XTerm } from "@xterm/xterm";
type CsiParam = number | number[];
type InternalTerminal = XTerm & {
_core?: {
scroll?: (eraseAttr: unknown, isWrapped?: boolean) => void;
_inputHandler?: {
_eraseAttrData?: () => unknown;
};
};
};
type ClearTerminalViewportOptions = {
wipeScrollback?: boolean;
};
type AppendEraseScrollbackOptions = {
wipeScrollback: boolean;
normalScreen: boolean;
};
const getVisibleContentRowCount = (term: XTerm): number => {
const buffer = term.buffer.active;
if (buffer.type !== "normal") {
return 0;
}
const baseY = buffer.baseY;
for (let row = term.rows - 1; row >= 0; row--) {
const line = buffer.getLine(baseY + row);
if (!line) {
continue;
}
if (line.translateToString(true).length > 0) {
return row + 1;
}
}
return 0;
};
export const preserveTerminalViewportInScrollback = (term: XTerm): void => {
const rowsToPreserve = getVisibleContentRowCount(term);
if (rowsToPreserve <= 0) {
return;
}
const internal = term as InternalTerminal;
const scroll = internal._core?.scroll;
const eraseAttr = internal._core?._inputHandler?._eraseAttrData?.();
if (typeof scroll !== "function" || eraseAttr === undefined) {
return;
}
for (let row = 0; row < rowsToPreserve; row++) {
scroll.call(internal._core, eraseAttr, false);
}
};
export const clearTerminalViewport = (
term: XTerm,
options: ClearTerminalViewportOptions = {},
): void => {
const buffer = term.buffer.active;
if (buffer.type !== "normal") return;
const cursorY = buffer.cursorY;
const cursorX = buffer.cursorX;
if (cursorY === 0 && buffer.baseY === 0) return;
const internal = term as InternalTerminal;
const scroll = internal._core?.scroll;
const eraseAttr = internal._core?._inputHandler?._eraseAttrData?.();
if (typeof scroll !== "function" || eraseAttr === undefined) return;
// Push lines above cursor into scrollback so they are preserved.
// After cursorY scrolls the prompt line shifts to active-screen row 0.
for (let i = 0; i < cursorY; i++) {
scroll.call(internal._core, eraseAttr, false);
}
// Clear everything below the prompt and reposition the cursor on it.
// CSI coordinates are 1-indexed.
const col = cursorX + 1;
const eraseScrollback = options.wipeScrollback ? "\x1b[3J" : "";
term.write(`\x1b[2;1H\x1b[J${eraseScrollback}\x1b[1;${col}H`, () => {
term.scrollToBottom();
});
};
export const isEraseScrollbackSequence = (params: CsiParam[]): boolean =>
params.length > 0 && params[0] === 3;
export const isEraseViewportSequence = (params: CsiParam[]): boolean =>
params.length > 0 && params[0] === 2;
/**
* Netcatty preserves visible rows in scrollback before CSI 2 J so shell `clear`
* does not discard history. TUIs inside DEC 2026 sync blocks or the alternate
* screen expect an in-place erase instead.
*/
export const shouldPreserveViewportBeforeFullErase = (
term: XTerm,
inDec2026SyncBlock: boolean,
clearWipesScrollback = false,
): boolean => {
if (inDec2026SyncBlock) {
return false;
}
if (clearWipesScrollback) {
return false;
}
return term.buffer.active.type === "normal";
};
export const shouldWipeScrollbackAfterFullErase = (
term: XTerm,
inDec2026SyncBlock: boolean,
clearWipesScrollback: boolean,
): boolean => {
if (!clearWipesScrollback || inDec2026SyncBlock) {
return false;
}
return term.buffer.active.type === "normal";
};
export const appendEraseScrollbackAfterFullErases = (
data: string,
{ wipeScrollback, normalScreen }: AppendEraseScrollbackOptions,
): string => {
if (!wipeScrollback || !normalScreen || data.length === 0) {
return data;
}
let result = "";
let index = 0;
let inDec2026SyncBlock = false;
let inAlternateScreen = false;
while (index < data.length) {
if (data.startsWith("\x1b[?2026h", index)) {
inDec2026SyncBlock = true;
result += "\x1b[?2026h";
index += "\x1b[?2026h".length;
continue;
}
if (data.startsWith("\x1b[?2026l", index)) {
inDec2026SyncBlock = false;
result += "\x1b[?2026l";
index += "\x1b[?2026l".length;
continue;
}
const altEnter = ["\x1b[?47h", "\x1b[?1047h", "\x1b[?1049h"].find((sequence) =>
data.startsWith(sequence, index)
);
if (altEnter) {
inAlternateScreen = true;
result += altEnter;
index += altEnter.length;
continue;
}
const altLeave = ["\x1b[?47l", "\x1b[?1047l", "\x1b[?1049l"].find((sequence) =>
data.startsWith(sequence, index)
);
if (altLeave) {
inAlternateScreen = false;
result += altLeave;
index += altLeave.length;
continue;
}
if (data.startsWith("\x1b[2J", index)) {
result += "\x1b[2J";
index += "\x1b[2J".length;
if (
!inDec2026SyncBlock
&& !inAlternateScreen
&& !data.startsWith("\x1b[3J", index)
) {
result += "\x1b[3J";
}
continue;
}
result += data[index];
index += 1;
}
return result;
};