diff --git a/field_confirm.go b/field_confirm.go index 874fa9c3..708033fc 100644 --- a/field_confirm.go +++ b/field_confirm.go @@ -150,7 +150,7 @@ func (c *Confirm) Blur() tea.Cmd { // KeyBinds returns the help message for the confirm field. func (c *Confirm) KeyBinds() []key.Binding { - return []key.Binding{c.keymap.Toggle, c.keymap.Prev, c.keymap.Submit, c.keymap.Next} + return []key.Binding{c.keymap.Toggle, c.keymap.Prev, c.keymap.Submit, c.keymap.Next, c.keymap.Accept, c.keymap.Reject} } // Init initializes the confirm field. @@ -205,6 +205,12 @@ func (c *Confirm) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmds = append(cmds, PrevField) case key.Matches(msg, c.keymap.Next, c.keymap.Submit): cmds = append(cmds, NextField) + case key.Matches(msg, c.keymap.Accept): + c.accessor.Set(true) + cmds = append(cmds, NextField) + case key.Matches(msg, c.keymap.Reject): + c.accessor.Set(false) + cmds = append(cmds, NextField) } } @@ -254,11 +260,27 @@ func (c *Confirm) View() string { affirmative = styles.BlurredButton.Render(c.affirmative) negative = styles.FocusedButton.Render(c.negative) } + c.keymap.Reject.SetHelp("n", c.negative) } else { affirmative = styles.FocusedButton.Render(c.affirmative) + c.keymap.Reject.SetEnabled(false) + } + + c.keymap.Accept.SetHelp("y", c.affirmative) + + buttonsRow := lipgloss.JoinHorizontal(lipgloss.Center, affirmative, negative) + + promptWidth := lipgloss.Width(sb.String()) + buttonsWidth := lipgloss.Width(buttonsRow) + + renderWidth := promptWidth + if buttonsWidth > renderWidth { + renderWidth = buttonsWidth } - sb.WriteString(lipgloss.JoinHorizontal(lipgloss.Center, affirmative, negative)) + style := lipgloss.NewStyle().Width(renderWidth).Align(lipgloss.Center) + + sb.WriteString(style.Render(buttonsRow)) return styles.Base.Render(sb.String()) } diff --git a/keymap.go b/keymap.go index 31f74099..8cca6d45 100644 --- a/keymap.go +++ b/keymap.go @@ -97,6 +97,8 @@ type ConfirmKeyMap struct { Prev key.Binding Toggle key.Binding Submit key.Binding + Accept key.Binding + Reject key.Binding } // NewDefaultKeyMap returns a new default keymap. @@ -172,7 +174,9 @@ func NewDefaultKeyMap() *KeyMap { Prev: key.NewBinding(key.WithKeys("shift+tab"), key.WithHelp("shift+tab", "back")), Next: key.NewBinding(key.WithKeys("enter", "tab"), key.WithHelp("enter", "next")), Submit: key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "submit")), - Toggle: key.NewBinding(key.WithKeys("h", "l", "right", "left"), key.WithHelp("←/→", "toggle")), + Toggle: key.NewBinding(key.WithKeys("h", "l", "right", "left", "tab", "shift+tab"), key.WithHelp("←/→", "toggle")), + Accept: key.NewBinding(key.WithKeys("y", "Y"), key.WithHelp("y", "Yes")), + Reject: key.NewBinding(key.WithKeys("n", "N"), key.WithHelp("n", "No")), }, } }