diff --git a/internal/browser/layout.go b/internal/browser/layout.go index c1fedbf..baafc91 100644 --- a/internal/browser/layout.go +++ b/internal/browser/layout.go @@ -152,7 +152,7 @@ func HandleSortModeChange(s *BrowserState) { // It checks each entry in the SortIndex against the query and updates the indices // to reflect the new sort order. func recomputeSearchResults(s *BrowserState) { - if s.Query == "" || s.SortIndex == nil { + if s.Query == "" { s.SearchResults = nil return } @@ -160,28 +160,32 @@ func recomputeSearchResults(s *BrowserState) { queryLower := strings.ToLower(s.Query) var results []int - // Get the position map for the NEW sort mode - positionMap := s.getSortedIndices() - if positionMap == nil { - s.SearchResults = nil - return - } - - // Iterate over the sorted entries and check if they match the query - for sortedIdx, rawIdx := range positionMap { - if rawIdx < 0 || rawIdx >= len(s.SortIndex.Entries) { - continue + if s.SortIndex != nil && s.SortIndex.SortOrders != nil { + // Use SortIndex for searching (production path) + positionMap := s.getSortedIndices() + if positionMap != nil { + for sortedIdx, rawIdx := range positionMap { + if rawIdx < 0 || rawIdx >= len(s.SortIndex.Entries) { + continue + } + entry := s.SortIndex.Entries[rawIdx] + if strings.Contains(strings.ToLower(entry.Name), queryLower) { + results = append(results, sortedIdx) + } + } } - - entry := s.SortIndex.Entries[rawIdx] - if strings.Contains(strings.ToLower(entry.Name), queryLower) { - results = append(results, sortedIdx) + } else { + // Fallback: search through loaded Pages (used in tests without SortIndex) + for _, page := range s.Pages { + for i, entry := range page.Entries { + if strings.Contains(strings.ToLower(entry.Name), queryLower) { + results = append(results, page.Index*PageSize+i) + } + } } } - // Sort the result indices - // (they might be out of order because we iterated over raw matches) - // This is important for scrolling + // Sort the result indices for consistent ordering sort.Ints(results) s.SearchResults = results