Skip to content

Commit c7419ca

Browse files
committed
Fix: 'rows' variable initialization error in mouse event handler
Issue: When clicking on any row in the TUI, getting 'Cannot access rows before initialization' error causing immediate exit with code 1. Root cause: rows was declared as a local variable inside redraw(), but hitTestClick tried to access it in the event loop where it didn't exist. Fix: Declare rows as a persistent variable outside redraw() and update it on each redraw() call. This makes rows available throughout the event loop. Also fixed normalizeScrollOffset calls for wheel scroll to use correct signature: normalizeScrollOffset(scrollOffset, rows, groups, viewportHeight). Closes #173 (partial - fixes the crash, functionality preserved)
1 parent 9607b27 commit c7419ca

1 file changed

Lines changed: 6 additions & 17 deletions

File tree

src/tui.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ export async function runInteractive(
214214
focusedIndex: number;
215215
} = { active: false, repoIndex: -1, candidates: [], focusedIndex: 0 };
216216

217+
// Persistent rows reference — updated on every redraw so it's available in the event loop
218+
let rows: Row[] = [];
219+
217220
/** Schedule a debounced stats recompute (while typing in filter bar). */
218221
const scheduleStatsUpdate = () => {
219222
if (statsDebounceTimer !== null) clearTimeout(statsDebounceTimer);
@@ -227,7 +230,7 @@ export async function runInteractive(
227230

228231
const redraw = () => {
229232
const activeFilter = filterMode ? filterInput : filterPath;
230-
const rows = buildRows(groups, activeFilter, filterTarget, filterRegex);
233+
rows = buildRows(groups, activeFilter, filterTarget, filterRegex);
231234
// Normalise scrollOffset downward so the viewport is packed to the bottom.
232235
// After a fold/unfold, filter change, or navigation near the end of the
233236
// list, the rows visible from scrollOffset onwards can be fewer than
@@ -296,27 +299,13 @@ export async function runInteractive(
296299
if (mouseEvent.button === 64) {
297300
// Wheel up — scroll up by a small step (3 rows)
298301
scrollOffset = Math.max(0, scrollOffset - 3);
299-
scrollOffset = normalizeScrollOffset(
300-
groups,
301-
rows,
302-
scrollOffset,
303-
termHeight,
304-
filterBarLines,
305-
stickyRepoLine !== null ? 1 : 0,
306-
);
302+
scrollOffset = normalizeScrollOffset(scrollOffset, rows, groups, getViewportHeight(rows));
307303
redraw();
308304
continue;
309305
} else if (mouseEvent.button === 65) {
310306
// Wheel down — scroll down by a small step (3 rows)
311307
scrollOffset = Math.min(Math.max(0, rows.length - 1), scrollOffset + 3);
312-
scrollOffset = normalizeScrollOffset(
313-
groups,
314-
rows,
315-
scrollOffset,
316-
termHeight,
317-
filterBarLines,
318-
stickyRepoLine !== null ? 1 : 0,
319-
);
308+
scrollOffset = normalizeScrollOffset(scrollOffset, rows, groups, getViewportHeight(rows));
320309
redraw();
321310
continue;
322311
}

0 commit comments

Comments
 (0)