Skip to content
TypeScript

perf(shared): count terminal resume lines without allocations (#28539)

Hieu Le6ef66b6cd1d1
diff --git a/packages/shared/src/terminalOutput.ts b/packages/shared/src/terminalOutput.ts
index f17c7c68d6d..b930282f777 100644
--- a/packages/shared/src/terminalOutput.ts
+++ b/packages/shared/src/terminalOutput.ts
@@ -631,8 +631,14 @@ export function terminalOutputResumeText(state: TerminalOutputState): string {
const { screen } = state;
const lastRow = screen.lines.length - 1;
// Moves are relative to the last line, and a viewer only holds the lines in `text`.
- const shownLines = state.text.split("\n").length;
- const reachable = Math.min(LIVE_LINES, shownLines) - 1;
+ let reachable = 0;
+ let cursor = 0;
+ while (reachable < LIVE_LINES - 1) {
+ const newline = state.text.indexOf("\n", cursor);
+ if (newline === -1) break;
+ reachable += 1;
+ cursor = newline + 1;
+ }
const up = (row: number) => Math.min(lastRow - row, reachable);
const goTo = (row: number, col: number) =>
(up(row) > 0 ? `${ESC}[${up(row)}A` : "") + `${ESC}[${col + 1}G`;