Skip to content

Commit

Permalink
feat(choose,filter): esc exit 1, ctrl+c exit 130
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Dec 11, 2024
1 parent 05614c8 commit 13d7035
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
12 changes: 11 additions & 1 deletion choose/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -77,6 +81,7 @@ type keymap struct {
ToggleAll,
Toggle,
Abort,
Quit,
Submit key.Binding
}

Expand Down Expand Up @@ -105,6 +110,7 @@ type model struct {
header string
items []item
quitting bool
submitted bool
index int
limit int
numSelected int
Expand Down Expand Up @@ -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
Expand All @@ -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
}
}
Expand Down
3 changes: 3 additions & 0 deletions choose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions filter/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -65,6 +69,7 @@ type keymap struct {
ToggleAndPrevious,
Toggle,
Abort,
Quit,
Submit key.Binding
}

Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 13d7035

Please sign in to comment.