From c65729468a78662b9366f1dd0b39fc132f3da72f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 25 May 2025 10:12:23 +0530 Subject: [PATCH] Handle up/down arrow keys to change current result --- kittens/choose_files/main.go | 6 ++++-- kittens/choose_files/results.go | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/kittens/choose_files/main.go b/kittens/choose_files/main.go index b3641b6be..de297680c 100644 --- a/kittens/choose_files/main.go +++ b/kittens/choose_files/main.go @@ -32,7 +32,9 @@ type State struct { exclude_patterns []*regexp.Regexp score_patterns []ScorePattern search_text string - current_idx int + + current_idx int + num_of_matches_at_last_render int } func (s State) BaseDir() string { return utils.IfElse(s.base_dir == "", default_cwd, s.base_dir) } @@ -119,7 +121,7 @@ func (h *Handler) OnInitialize() (ans string, err error) { func (h *Handler) OnKeyEvent(ev *loop.KeyEvent) (err error) { switch { - case h.handle_edit_keys(ev): + case h.handle_edit_keys(ev), h.handle_result_list_keys(ev): h.draw_screen() case ev.MatchesPressOrRepeat("esc") || ev.MatchesPressOrRepeat("ctrl+c"): h.lp.Quit(1) diff --git a/kittens/choose_files/results.go b/kittens/choose_files/results.go index a7e740a7d..315bcac14 100644 --- a/kittens/choose_files/results.go +++ b/kittens/choose_files/results.go @@ -8,6 +8,7 @@ import ( "unicode/utf8" "github.com/kovidgoyal/kitty/tools/icons" + "github.com/kovidgoyal/kitty/tools/tui/loop" "github.com/kovidgoyal/kitty/tools/utils" "github.com/kovidgoyal/kitty/tools/utils/style" "github.com/kovidgoyal/kitty/tools/wcswidth" @@ -215,5 +216,28 @@ func (h *Handler) draw_results(y, bottom_margin int, matches []*ResultItem, in_p default: h.draw_list_of_results(matches, y, height-2) } + h.state.num_of_matches_at_last_render = len(matches) return } + +func (h *Handler) next_result(amt int) { + if h.state.num_of_matches_at_last_render > 0 { + idx := h.state.CurrentIndex() + idx += amt + h.state.num_of_matches_at_last_render + idx %= h.state.num_of_matches_at_last_render + h.state.SetCurrentIndex(idx) + } +} + +func (h *Handler) handle_result_list_keys(ev *loop.KeyEvent) bool { + switch { + case ev.MatchesPressOrRepeat("down"): + h.next_result(1) + return true + case ev.MatchesPressOrRepeat("up"): + h.next_result(-1) + return true + default: + return false + } +}