From fb693fbc903b48569eadf35e5327f62faac5c2cc Mon Sep 17 00:00:00 2001 From: mom Date: Wed, 28 Jan 2026 01:40:57 +0000 Subject: [PATCH] perf(tui): scan only bottom terminal height lines in extractCursorPosition --- packages/tui/src/tui.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/tui/src/tui.ts b/packages/tui/src/tui.ts index 569bc920..f872cb05 100644 --- a/packages/tui/src/tui.ts +++ b/packages/tui/src/tui.ts @@ -769,17 +769,15 @@ export class TUI extends Container { /** * Find and extract cursor position from rendered lines. * Searches for CURSOR_MARKER, calculates its position, and strips it from the output. + * Only scans the bottom terminal height lines (visible viewport). * @param lines - Rendered lines to search - * @param height - Terminal height (to calculate viewport) + * @param height - Terminal height (visible viewport size) * @returns Cursor position { row, col } or null if no marker found */ private extractCursorPosition(lines: string[], height: number): { row: number; col: number } | null { + // Only scan the bottom `height` lines (visible viewport) const viewportTop = Math.max(0, lines.length - height); - for (let row = lines.length - 1; row >= 0; row--) { - // Early out if above visible viewport - if (row < viewportTop) { - return null; - } + for (let row = lines.length - 1; row >= viewportTop; row--) { const line = lines[row]; const markerIndex = line.indexOf(CURSOR_MARKER); if (markerIndex !== -1) {