diff --git a/internal/browser/layout.go b/internal/browser/layout.go index fd75472..c1fedbf 100644 --- a/internal/browser/layout.go +++ b/internal/browser/layout.go @@ -188,7 +188,7 @@ func recomputeSearchResults(s *BrowserState) { // Jump to first result if any matches found if len(results) > 0 { - s.ScrollOffset = float64(results[0]) * s.EntryHeight + s.ScrollOffset = 0 // Search results start at the top } else { s.ScrollOffset = 0 } @@ -246,14 +246,20 @@ func computeVisibleEntries(state *BrowserState) []ui.ListItem { } // computeSearchResults builds the list of ui.ListItem entries for search results. -// Only entries whose indices are in SearchResults are shown. +// Only entries whose indices are in SearchResults are shown, based on the current scroll offset. func computeSearchResults(state *BrowserState) []ui.ListItem { + startIndex := state.GetScrollIndex() + // show one past the "visible count" for a partial view of the next item + endIndex := startIndex + state.VisibleCount + 1 + if endIndex > len(state.SearchResults) { + endIndex = len(state.SearchResults) + } + var items []ui.ListItem - for _, idx := range state.SearchResults { - if idx < 0 || idx >= state.TotalEntries { - continue - } - entry, ok := getEntryByIndex(state, idx) + for idx := startIndex; idx < endIndex; idx++ { + rawIdx := state.SearchResults[idx] + + entry, ok := getEntryByIndex(state, rawIdx) if !ok { // Page not loaded yet; add placeholder items = append(items, ui.ListItem{