Skip to content
Source
Sign inSign up
pingdotgg/t3code

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

Mergedhieu-lee(GitHub profile, opens in a new tab)merged 1 commit intomain fromrefs/pull/28539/headmerged by maria-rcks View on GitHub

Showing 1 changed file with 8 additions and 2 deletions.

x
0 of 1 viewed, 1 left
8 additions and 2 deletionspackages/shared/src/terminalOutput.tsModified
View file
@@ -598,8 +598,14 @@
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`;