Skip to content

Commit c657294

Browse files
committed
Handle up/down arrow keys to change current result
1 parent 5dc2956 commit c657294

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

kittens/choose_files/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ type State struct {
3232
exclude_patterns []*regexp.Regexp
3333
score_patterns []ScorePattern
3434
search_text string
35-
current_idx int
35+
36+
current_idx int
37+
num_of_matches_at_last_render int
3638
}
3739

3840
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) {
119121

120122
func (h *Handler) OnKeyEvent(ev *loop.KeyEvent) (err error) {
121123
switch {
122-
case h.handle_edit_keys(ev):
124+
case h.handle_edit_keys(ev), h.handle_result_list_keys(ev):
123125
h.draw_screen()
124126
case ev.MatchesPressOrRepeat("esc") || ev.MatchesPressOrRepeat("ctrl+c"):
125127
h.lp.Quit(1)

kittens/choose_files/results.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"unicode/utf8"
99

1010
"github.com/kovidgoyal/kitty/tools/icons"
11+
"github.com/kovidgoyal/kitty/tools/tui/loop"
1112
"github.com/kovidgoyal/kitty/tools/utils"
1213
"github.com/kovidgoyal/kitty/tools/utils/style"
1314
"github.com/kovidgoyal/kitty/tools/wcswidth"
@@ -215,5 +216,28 @@ func (h *Handler) draw_results(y, bottom_margin int, matches []*ResultItem, in_p
215216
default:
216217
h.draw_list_of_results(matches, y, height-2)
217218
}
219+
h.state.num_of_matches_at_last_render = len(matches)
218220
return
219221
}
222+
223+
func (h *Handler) next_result(amt int) {
224+
if h.state.num_of_matches_at_last_render > 0 {
225+
idx := h.state.CurrentIndex()
226+
idx += amt + h.state.num_of_matches_at_last_render
227+
idx %= h.state.num_of_matches_at_last_render
228+
h.state.SetCurrentIndex(idx)
229+
}
230+
}
231+
232+
func (h *Handler) handle_result_list_keys(ev *loop.KeyEvent) bool {
233+
switch {
234+
case ev.MatchesPressOrRepeat("down"):
235+
h.next_result(1)
236+
return true
237+
case ev.MatchesPressOrRepeat("up"):
238+
h.next_result(-1)
239+
return true
240+
default:
241+
return false
242+
}
243+
}

0 commit comments

Comments
 (0)