From 45bc7a691b0da12018178df8d20cbcdaa925bf86 Mon Sep 17 00:00:00 2001 From: Louis Garman Date: Sun, 29 Dec 2024 15:13:55 +0000 Subject: [PATCH] fix: always try to put cursor on first module --- internal/tui/explorer/tracker.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/internal/tui/explorer/tracker.go b/internal/tui/explorer/tracker.go index 2b5ffc9..51030af 100644 --- a/internal/tui/explorer/tracker.go +++ b/internal/tui/explorer/tracker.go @@ -41,23 +41,32 @@ func (t *tracker) reindex(tree *tree, height int) { t.doReindex(tree) t.selector.reindex(t.nodes) - // When pug first starts up, for the user's convenience we want the cursor - // to be on the first module. Because modules are added asynchronously, a - // semaphore detects whether the cursor has been set to the first module, to - // ensure this is only done once. - if !t.initialized { + placeCursorOnFirstModule := func() bool { for i, n := range t.nodes { if _, ok := n.(moduleNode); ok { t.cursorNode = t.nodes[i] t.cursorIndex = i - t.initialized = true - break + return true } } + // If no modules found then set cursor on first node + if t.cursorIndex < 0 { + t.cursorNode = t.nodes[0] + t.cursorIndex = 0 + } + return false } - if t.cursorIndex < 0 { - t.cursorNode = t.nodes[0] - t.cursorIndex = 0 + + // When pug first starts up, for the user's convenience we want the cursor + // to be on the first module. Because modules are added asynchronously, a + // semaphore detects whether the cursor has been set to the first module, to + // ensure this is only done once. + if !t.initialized { + if placeCursorOnFirstModule() { + t.initialized = true + } + } else if t.cursorIndex < 0 { + placeCursorOnFirstModule() } t.setStart(height) }