Skip to content

Commit

Permalink
feat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+…
Browse files Browse the repository at this point in the history
…c exit 130, help arrow order (#771)

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 authored Dec 12, 2024
1 parent 05614c8 commit 774667a
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 20 deletions.
19 changes: 12 additions & 7 deletions choose/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,18 @@ func defaultKeymap() keymap {
return keymap{
Down: key.NewBinding(
key.WithKeys("down", "j", "ctrl+j", "ctrl+n"),
key.WithHelp("↓", "down"),
),
Up: key.NewBinding(
key.WithKeys("up", "k", "ctrl+k", "ctrl+p"),
key.WithHelp("↑", "up"),
),
Right: key.NewBinding(
key.WithKeys("right", "l", "ctrl+f"),
key.WithHelp("→", "right"),
),
Left: key.NewBinding(
key.WithKeys("left", "h", "ctrl+b"),
key.WithHelp("←", "left"),
),
Home: key.NewBinding(
key.WithKeys("g", "home"),
key.WithHelp("g", "home"),
),
End: key.NewBinding(
key.WithKeys("G", "end"),
Expand All @@ -57,9 +52,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 +76,7 @@ type keymap struct {
ToggleAll,
Toggle,
Abort,
Quit,
Submit key.Binding
}

Expand All @@ -89,7 +89,7 @@ func (k keymap) ShortHelp() []key.Binding {
k.Toggle,
key.NewBinding(
key.WithKeys("up", "down", "right", "left"),
key.WithHelp("↑↓←→", "navigate"),
key.WithHelp("←↓↑→", "navigate"),
),
k.Submit,
k.ToggleAll,
Expand All @@ -105,6 +105,7 @@ type model struct {
header string
items []item
quitting bool
submitted bool
index int
limit int
numSelected int
Expand Down Expand Up @@ -177,6 +178,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 +203,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
2 changes: 1 addition & 1 deletion confirm/confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func defaultKeymap(affirmative, negative string) keymap {
"ctrl+p",
"tab",
),
key.WithHelp("←/→", "toggle"),
key.WithHelp("←→", "toggle"),
),
Submit: key.NewBinding(
key.WithKeys("enter"),
Expand Down
8 changes: 4 additions & 4 deletions file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ var keyAbort = key.NewBinding(

func defaultKeymap() keymap {
km := filepicker.DefaultKeyMap()
km.Down.SetHelp("↓", "down")
km.Up.SetHelp("↑", "up")
return keymap(km)
}

Expand All @@ -45,8 +43,10 @@ func (k keymap) FullHelp() [][]key.Binding { return nil }
// ShortHelp implements help.KeyMap.
func (k keymap) ShortHelp() []key.Binding {
return []key.Binding{
k.Up,
k.Down,
key.NewBinding(
key.WithKeys("up", "down"),
key.WithHelp("↓↑", "navigate"),
),
keyQuit,
k.Select,
}
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
16 changes: 12 additions & 4 deletions filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ func defaultKeymap() keymap {
return keymap{
Down: key.NewBinding(
key.WithKeys("down", "ctrl+j", "ctrl+n"),
key.WithHelp("↓", "down"),
),
Up: key.NewBinding(
key.WithKeys("up", "ctrl+k", "ctrl+p"),
key.WithHelp("↑", "up"),
),
ToggleAndNext: key.NewBinding(
key.WithKeys("tab"),
Expand All @@ -47,8 +45,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 +67,7 @@ type keymap struct {
ToggleAndPrevious,
Toggle,
Abort,
Quit,
Submit key.Binding
}

Expand All @@ -77,7 +80,7 @@ func (k keymap) ShortHelp() []key.Binding {
k.ToggleAndNext,
key.NewBinding(
key.WithKeys("up", "down"),
key.WithHelp("↑↓", "navigate"),
key.WithHelp("↓↑", "navigate"),
),
k.Submit,
}
Expand Down Expand Up @@ -112,6 +115,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 +255,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
2 changes: 1 addition & 1 deletion gum.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ type Gum struct {
// │ 7 │ │
// │ 8 │ │
// ╰────────────────────────────────────────────────╯
// ↑/↓: Navigate • q: Quit
// ↓↑: navigate • q: quit
//
Pager pager.Options `cmd:"" help:"Scroll through a file"`

Expand Down
4 changes: 4 additions & 0 deletions input/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package input

import (
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -67,6 +68,9 @@ func (o Options) Run() error {
}

m = tm.(model)
if !m.submitted {
return errors.New("not submitted")
}
fmt.Println(m.textinput.Value())
return nil
}
7 changes: 6 additions & 1 deletion input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type model struct {
headerStyle lipgloss.Style
textinput textinput.Model
quitting bool
submitted bool
showHelp bool
help help.Model
keymap keymap
Expand Down Expand Up @@ -79,9 +80,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "ctrl+c":
m.quitting = true
return m, tea.Interrupt
case "esc", "enter":
case "esc":
m.quitting = true
return m, tea.Quit
case "enter":
m.quitting = true
m.submitted = true
return m, tea.Quit
}
}

Expand Down
2 changes: 1 addition & 1 deletion pager/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (k keymap) ShortHelp() []key.Binding {
return []key.Binding{
key.NewBinding(
key.WithKeys("up", "down"),
key.WithHelp("↑/↓", "navigate"),
key.WithHelp("↓↑", "navigate"),
),
k.Quit,
k.Search,
Expand Down
2 changes: 1 addition & 1 deletion table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func defaultKeymap() keymap {
return keymap{
Navigate: key.NewBinding(
key.WithKeys("up", "down"),
key.WithHelp("↑↓", "navigate"),
key.WithHelp("↓↑", "navigate"),
),
Select: key.NewBinding(
key.WithKeys("enter"),
Expand Down
4 changes: 4 additions & 0 deletions write/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package write

import (
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -73,6 +74,9 @@ func (o Options) Run() error {
return fmt.Errorf("failed to run write: %w", err)
}
m = tm.(model)
if !m.submitted {
return errors.New("not submitted")
}
fmt.Println(m.textarea.Value())
return nil
}
10 changes: 10 additions & 0 deletions write/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
type keymap struct {
textarea.KeyMap
Submit key.Binding
Quit key.Binding
Abort key.Binding
OpenInEditor key.Binding
}
Expand All @@ -47,6 +48,10 @@ func defaultKeymap() keymap {
)
return keymap{
KeyMap: km,
Quit: key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "quit"),
),
Abort: key.NewBinding(
key.WithKeys("ctrl+c"),
key.WithHelp("ctrl+c", "cancel"),
Expand All @@ -67,6 +72,7 @@ type model struct {
header string
headerStyle lipgloss.Style
quitting bool
submitted bool
textarea textarea.Model
showHelp bool
help help.Model
Expand Down Expand Up @@ -117,8 +123,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, km.Abort):
m.quitting = true
return m, tea.Interrupt
case key.Matches(msg, km.Quit):
m.quitting = true
return m, tea.Quit
case key.Matches(msg, km.Submit):
m.quitting = true
m.submitted = true
return m, tea.Quit
case key.Matches(msg, km.OpenInEditor):
//nolint: gosec
Expand Down

0 comments on commit 774667a

Please sign in to comment.