Handle backspace and esc keys

This commit is contained in:
Kovid Goyal 2025-05-20 14:34:52 +05:30
parent 97b6212513
commit 56b26838ce
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 30 additions and 1 deletions

View file

@ -77,6 +77,21 @@ func (h *Handler) OnInitialize() (ans string, err error) {
return
}
func (h *Handler) OnKeyEvent(ev *loop.KeyEvent) (err error) {
switch {
case h.handle_edit_keys(ev):
h.draw_screen()
case ev.MatchesPressOrRepeat("esc"):
h.lp.Quit(1)
}
return
}
func (h *Handler) OnText(text string, from_key_event, in_bracketed_paste bool) (err error) {
h.state.search_text += text
return h.draw_screen()
}
var default_cwd string
func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
@ -101,6 +116,8 @@ func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
handler.init_sizes(new_size)
return handler.draw_screen()
}
lp.OnKeyEvent = handler.OnKeyEvent
lp.OnText = handler.OnText
err = lp.Run()
if err != nil {
return 1, err

View file

@ -35,7 +35,6 @@ func (h *Handler) draw_frame(width, height int) {
func (h *Handler) draw_search_text(available_width int) {
text := h.state.SearchText()
text = "abcdefghijklmnopqrstuvwxyz1234"
available_width /= 2
if wcswidth.Stringwidth(text) > available_width {
g := wcswidth.SplitIntoGraphemes(text)
@ -58,3 +57,16 @@ func (h *Handler) draw_search_bar(y int) (height int, err error) {
return
}
func (h *Handler) handle_edit_keys(ev *loop.KeyEvent) bool {
switch {
case ev.MatchesPressOrRepeat("backspace"):
if h.state.SearchText() == "" {
h.lp.Beep()
} else {
h.state.search_text = h.state.search_text[:len(h.state.search_text)-1]
return true
}
}
return false
}