Skip to content

Commit 6791f6b

Browse files
committed
perf(shared): count terminal resume lines without allocations
1 parent 7195b69 commit 6791f6b

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

‎packages/shared/src/terminalOutput.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,14 @@ export function terminalOutputResumeText(state: TerminalOutputState): string {
598598
const { screen } = state;
599599
const lastRow = screen.lines.length - 1;
600600
// Moves are relative to the last line, and a viewer only holds the lines in `text`.
601-
const shownLines = state.text.split("\n").length;
602-
const reachable = Math.min(LIVE_LINES, shownLines) - 1;
601+
let reachable = 0;
602+
let cursor = 0;
603+
while (reachable < LIVE_LINES - 1) {
604+
const newline = state.text.indexOf("\n", cursor);
605+
if (newline === -1) break;
606+
reachable += 1;
607+
cursor = newline + 1;
608+
}
603609
const up = (row: number) => Math.min(lastRow - row, reachable);
604610
const goTo = (row: number, col: number) =>
605611
(up(row) > 0 ? `${ESC}[${up(row)}A` : "") + `${ESC}[${col + 1}G`;

0 commit comments

Comments
 (0)