Handle up/down arrow keys to change current result

This commit is contained in:
Kovid Goyal
2025-05-25 10:12:23 +05:30
parent 5dc29563e8
commit c65729468a
2 changed files with 28 additions and 2 deletions

View File

@@ -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)

View File

@@ -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
}
}