From d8410dff24675398db061ec3d4cad1db914e2dbe Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 6 Jun 2025 19:50:01 +0530 Subject: [PATCH] Ensure positions are always sorted when rendering --- kittens/choose_files/results.go | 2 +- kittens/choose_files/scan.go | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/kittens/choose_files/results.go b/kittens/choose_files/results.go index eac396e28..80011abdc 100644 --- a/kittens/choose_files/results.go +++ b/kittens/choose_files/results.go @@ -134,7 +134,7 @@ func (h *Handler) draw_column_of_matches(matches []*ResultItem, current_idx int, } else { h.lp.QueueWriteString(icon + " ") } - h.render_match_with_positions(text, add_ellipsis, m.positions, is_current) + h.render_match_with_positions(text, add_ellipsis, m.sorted_positions(), is_current) h.lp.MoveCursorVertically(1) } } diff --git a/kittens/choose_files/scan.go b/kittens/choose_files/scan.go index 2613c11a3..f3dc2f0f0 100644 --- a/kittens/choose_files/scan.go +++ b/kittens/choose_files/scan.go @@ -19,16 +19,27 @@ import ( var _ = fmt.Print type ResultItem struct { - text, abspath string - dir_entry os.DirEntry - positions []int // may be nil - score float64 + text, abspath string + dir_entry os.DirEntry + positions []int // may be nil + score float64 + positions_sorted bool } func (r ResultItem) String() string { return fmt.Sprintf("{text: %#v, abspath: %#v, is_dir: %v, positions: %#v}", r.text, r.abspath, r.dir_entry.IsDir(), r.positions) } +func (r *ResultItem) sorted_positions() []int { + if !r.positions_sorted { + r.positions_sorted = true + if len(r.positions) > 1 { + sort.Ints(r.positions) + } + } + return r.positions +} + type dir_cache map[string][]os.DirEntry type ScanCache struct {