From 13d703538fb18c1a8794c3db11f6ba5e4c0ca89d Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 11 Dec 2024 15:02:44 -0300 Subject: [PATCH] feat(choose,filter): esc exit 1, ctrl+c exit 130 Signed-off-by: Carlos Alexandro Becker --- choose/choose.go | 12 +++++++++++- choose/command.go | 3 +++ filter/command.go | 3 +++ filter/filter.go | 12 +++++++++++- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/choose/choose.go b/choose/choose.go index 2c2e1173e..afb6079ef 100644 --- a/choose/choose.go +++ b/choose/choose.go @@ -57,9 +57,13 @@ func defaultKeymap() keymap { key.WithDisabled(), ), Abort: key.NewBinding( - key.WithKeys("ctrl+c", "esc"), + key.WithKeys("ctrl+c"), key.WithHelp("ctrl+c", "abort"), ), + Quit: key.NewBinding( + key.WithKeys("esc"), + key.WithHelp("esc", "quit"), + ), Submit: key.NewBinding( key.WithKeys("enter", "ctrl+q"), key.WithHelp("enter", "submit"), @@ -77,6 +81,7 @@ type keymap struct { ToggleAll, Toggle, Abort, + Quit, Submit key.Binding } @@ -105,6 +110,7 @@ type model struct { header string items []item quitting bool + submitted bool index int limit int numSelected int @@ -177,6 +183,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } else { m = m.deselectAll() } + case key.Matches(msg, km.Quit): + m.quitting = true + return m, tea.Quit case key.Matches(msg, km.Abort): m.quitting = true return m, tea.Interrupt @@ -199,6 +208,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if m.limit <= 1 && m.numSelected < 1 { m.items[m.index].selected = true } + m.submitted = true return m, tea.Quit } } diff --git a/choose/command.go b/choose/command.go index fd07d8404..39c2bcc41 100644 --- a/choose/command.go +++ b/choose/command.go @@ -136,6 +136,9 @@ func (o Options) Run() error { return fmt.Errorf("unable to pick selection: %w", err) } m = tm.(model) + if !m.submitted { + return errors.New("nothing selected") + } if o.Ordered && o.Limit > 1 { sort.Slice(m.items, func(i, j int) bool { return m.items[i].order < m.items[j].order diff --git a/filter/command.go b/filter/command.go index 4edbdc334..1c5a6ca5b 100644 --- a/filter/command.go +++ b/filter/command.go @@ -119,6 +119,9 @@ func (o Options) Run() error { } m := tm.(model) + if !m.submitted { + return errors.New("nothing selected") + } isTTY := term.IsTerminal(os.Stdout.Fd()) // allSelections contains values only if limit is greater diff --git a/filter/filter.go b/filter/filter.go index 33ed114e6..0b0d877a2 100644 --- a/filter/filter.go +++ b/filter/filter.go @@ -47,8 +47,12 @@ func defaultKeymap() keymap { key.WithHelp("ctrl+@", "toggle"), key.WithDisabled(), ), + Quit: key.NewBinding( + key.WithKeys("esc"), + key.WithHelp("esc", "quit"), + ), Abort: key.NewBinding( - key.WithKeys("ctrl+c", "esc"), + key.WithKeys("ctrl+c"), key.WithHelp("ctrl+c", "abort"), ), Submit: key.NewBinding( @@ -65,6 +69,7 @@ type keymap struct { ToggleAndPrevious, Toggle, Abort, + Quit, Submit key.Binding } @@ -112,6 +117,7 @@ type model struct { keymap keymap help help.Model strict bool + submitted bool } func (m model) Init() tea.Cmd { return textinput.Blink } @@ -251,11 +257,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyMsg: km := m.keymap switch { + case key.Matches(msg, km.Quit): + m.quitting = true + return m, tea.Quit case key.Matches(msg, km.Abort): m.quitting = true return m, tea.Interrupt case key.Matches(msg, km.Submit): m.quitting = true + m.submitted = true return m, tea.Quit case key.Matches(msg, km.Down): m.CursorDown()