Skip to content

Commit

Permalink
feat: use keymap / bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Oct 24, 2023
1 parent bc9520d commit 9df37e6
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 35 deletions.
14 changes: 5 additions & 9 deletions field_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (c *Confirm) KeyMap(k *KeyMap) Field {

// KeyBinds returns the help message for the confirm field.
func (c *Confirm) KeyBinds() []key.Binding {
return []key.Binding{c.keymap.Next, c.keymap.Prev}
return []key.Binding{c.keymap.Toggle, c.keymap.Next, c.keymap.Prev}
}

// Init initializes the confirm field.
Expand All @@ -116,16 +116,12 @@ func (c *Confirm) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

c.err = nil

switch msg.String() {
case "y", "Y":
*c.value = true
case "n", "N":
*c.value = false
case "h", "l", "left", "right":
switch {
case key.Matches(msg, c.keymap.Toggle):
*c.value = !*c.value
case "shift+tab":
case key.Matches(msg, c.keymap.Prev):
cmds = append(cmds, prevField)
case "enter", "tab":
case key.Matches(msg, c.keymap.Next):
cmds = append(cmds, nextField)
}
}
Expand Down
6 changes: 3 additions & 3 deletions field_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ func (i *Input) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
i.err = nil

switch msg.String() {
case "shift+tab":
switch {
case key.Matches(msg, i.keymap.Prev):
cmds = append(cmds, prevField)
case "enter", "tab":
case key.Matches(msg, i.keymap.Next):
cmds = append(cmds, nextField)
}
}
Expand Down
12 changes: 6 additions & 6 deletions field_multiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,20 @@ func (m *MultiSelect[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

m.err = nil

switch msg.String() {
case "up", "k", "ctrl+p":
switch {
case key.Matches(msg, m.keymap.Up):
m.cursor = max(m.cursor-1, 0)
case "down", "j", "ctrl+n":
case key.Matches(msg, m.keymap.Down):
m.cursor = min(m.cursor+1, len(m.options)-1)
case " ", "x":
case key.Matches(msg, m.keymap.Toggle):
if !m.selected[m.cursor] && m.numSelected() >= m.limit {
break
}
m.selected[m.cursor] = !m.selected[m.cursor]
case "shift+tab":
case key.Matches(msg, m.keymap.Prev):
m.finalize()
return m, prevField
case "tab", "enter":
case key.Matches(msg, m.keymap.Next):
m.finalize()
return m, nextField
}
Expand Down
6 changes: 4 additions & 2 deletions field_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ func (n *Note) KeyBinds() []key.Binding {
func (n *Note) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "shift+tab":
switch {
case key.Matches(msg, n.keymap.Prev):
return n, prevField
case key.Matches(msg, n.keymap.Next):
return n, nextField
}
return n, nextField
}
Expand Down
10 changes: 5 additions & 5 deletions field_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ func (s *Select[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
s.err = nil
switch msg.String() {
case "up", "k", "ctrl+p":
switch {
case key.Matches(msg, s.keymap.Up):
s.selected = max(s.selected-1, 0)
case "down", "j", "ctrl+n":
case key.Matches(msg, s.keymap.Down):
s.selected = min(s.selected+1, len(s.options)-1)
case "shift+tab":
case key.Matches(msg, s.keymap.Prev):
return s, prevField
case "tab", "enter":
case key.Matches(msg, s.keymap.Next):
value := s.options[s.selected].Value
s.err = s.validate(value)
if s.err != nil {
Expand Down
6 changes: 3 additions & 3 deletions field_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ func (t *Text) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
t.err = nil

switch msg.String() {
case "tab", "ctrl+d":
switch {
case key.Matches(msg, t.keymap.Next):
cmds = append(cmds, nextField)
case "shift+tab":
case key.Matches(msg, t.keymap.Prev):
cmds = append(cmds, prevField)
}
}
Expand Down
4 changes: 2 additions & 2 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func (f *Form) Init() tea.Cmd {
func (f *Form) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
switch {
case key.Matches(msg, f.keymap.Quit):
f.quitting = true
return f, tea.Quit
}
Expand Down
15 changes: 10 additions & 5 deletions keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import "github.com/charmbracelet/bubbles/key"

// KeyMap is the keybindings to navigate the form.
type KeyMap struct {
Quit key.Binding

Input InputKeyMap
Text TextKeyMap
Select SelectKeyMap
Expand Down Expand Up @@ -50,13 +52,15 @@ type NoteKeyMap struct {

// ConfirmKeyMap is the keybindings for confirm fields.
type ConfirmKeyMap struct {
Next key.Binding
Prev key.Binding
Next key.Binding
Prev key.Binding
Toggle key.Binding
}

// NewDefaultKeyMap returns a new default keymap.
func NewDefaultKeyMap() *KeyMap {
return &KeyMap{
Quit: key.NewBinding(key.WithKeys("ctrl+c")),
Input: InputKeyMap{
Next: key.NewBinding(key.WithKeys("enter", "tab"), key.WithHelp("enter", "submit")),
Prev: key.NewBinding(key.WithKeys("shift+tab"), key.WithHelp("shift+tab", "back")),
Expand All @@ -80,12 +84,13 @@ func NewDefaultKeyMap() *KeyMap {
Down: key.NewBinding(key.WithKeys("down", "j", "ctrl+n"), key.WithHelp("↓", "down")),
},
Note: NoteKeyMap{
Next: key.NewBinding(key.WithKeys("enter", "tab"), key.WithHelp("enter", "submit")),
Next: key.NewBinding(key.WithKeys("enter", "tab"), key.WithHelp("enter", "next")),
Prev: key.NewBinding(key.WithKeys("shift+tab")),
},
Confirm: ConfirmKeyMap{
Next: key.NewBinding(key.WithKeys("enter", "tab"), key.WithHelp("enter", "submit")),
Prev: key.NewBinding(key.WithKeys("shift+tab"), key.WithHelp("shift+tab", "back")),
Next: key.NewBinding(key.WithKeys("enter", "tab"), key.WithHelp("enter", "submit")),
Prev: key.NewBinding(key.WithKeys("shift+tab"), key.WithHelp("shift+tab", "back")),
Toggle: key.NewBinding(key.WithKeys("h", "j", "right", "left"), key.WithHelp("←/→", "toggle")),
},
}
}

0 comments on commit 9df37e6

Please sign in to comment.