Skip to content

Commit 8b7edbf

Browse files
committed
fix(shared): retain cleared row geometry in bounded resumes
1 parent d265b3e commit 8b7edbf

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

‎packages/shared/src/terminalOutput.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ describe("appendTerminalOutput edge cases from review", () => {
8888
expect(continued.text).toBe("Y\n X");
8989
const resumed = appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, terminalOutputResumeText(state));
9090
expect(appendTerminalOutput(resumed, "X\u001b[1A\rY").text).toBe(continued.text);
91+
for (const [input, maxChars, next] of [
92+
["\n".repeat(10) + "\u001b[2J", 3, "\u001b[8AX"],
93+
["\n".repeat(10) + "\u001b[8A\u001b[2Jx", 3, "X"],
94+
["\n".repeat(100) + "\u001b[2J", 64, "X"],
95+
] as const) {
96+
const bounded = feed([input], maxChars);
97+
const boundedResume = appendTerminalOutput(
98+
EMPTY_TERMINAL_OUTPUT,
99+
terminalOutputResumeText(bounded),
100+
maxChars,
101+
);
102+
expect(appendTerminalOutput(boundedResume, next, maxChars).text).toBe(
103+
appendTerminalOutput(bounded, next, maxChars).text,
104+
);
105+
}
91106
});
92107

93108
it("clears only scrollback on ESC[3J", () => {
@@ -158,6 +173,8 @@ describe("terminalOutputResumeText with a saved cursor or trimmed output", () =>
158173
120,
159174
);
160175
expect(viewer.text).toBe(appendTerminalOutput(source, " edited", 120).text);
176+
const styled = feed([Array(64).fill("\u001b[31mx\u001b[0my".repeat(32)).join("\n")], 4096);
177+
expect(terminalOutputResumeText(styled).length).toBeLessThanOrEqual(styled.text.length + 63);
161178
});
162179
});
163180

‎packages/shared/src/terminalOutput.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,13 @@ export function appendTerminalOutput(
595595
export function terminalOutputResumeText(state: TerminalOutputState): string {
596596
const { screen } = state;
597597
const lastRow = screen.lines.length - 1;
598-
// Moves are relative to the last line, and a viewer only holds the lines in `text`.
599-
const shownLines = state.text.split("\n").length;
600-
const reachable = Math.min(LIVE_LINES, shownLines) - 1;
598+
// Cleared rows still carry cursor geometry even when the bounded tail omits them.
599+
const text = screen.lines.every((line) => lineLength(line) === 0)
600+
? screen.settled + "\n".repeat(lastRow)
601+
: state.text;
602+
const shownLines = text.split("\n").length;
603+
const omittedRows = Math.max(0, screen.lines.length - shownLines);
604+
const reachable = Math.min(LIVE_LINES, shownLines + omittedRows) - 1;
601605
const up = (row: number) => Math.min(lastRow - row, reachable);
602606
const goTo = (row: number, col: number) =>
603607
(up(row) > 0 ? `${ESC}[${up(row)}A` : "") + `${ESC}[${col + 1}G`;
@@ -614,7 +618,7 @@ export function terminalOutputResumeText(state: TerminalOutputState): string {
614618
suffix += goTo(screen.row, screen.col);
615619
}
616620
if (screen.pen !== "") suffix += sgr(screen.pen);
617-
return state.text + suffix + state.pending;
621+
return "\n".repeat(omittedRows) + text + suffix + state.pending;
618622
}
619623

620624
export function normalizeTerminalOutput(

0 commit comments

Comments
 (0)