Skip to content

Commit

Permalink
feat: assign keys to confirmation dialog options
Browse files Browse the repository at this point in the history
  • Loading branch information
idursun committed Feb 24, 2025
1 parent bd245e5 commit 16b8fca
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
32 changes: 22 additions & 10 deletions internal/ui/confirmation/confirmation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package confirmation

import (
"github.com/charmbracelet/bubbles/key"
"strings"

"github.com/charmbracelet/lipgloss"
Expand All @@ -10,11 +11,19 @@ import (
tea "github.com/charmbracelet/bubbletea"
)

var (
right = key.NewBinding(key.WithKeys("right", "l"))
left = key.NewBinding(key.WithKeys("left", "h"))
enter = key.NewBinding(key.WithKeys("enter"))
cancel = key.NewBinding(key.WithKeys("esc"))
)

type CloseMsg struct{}

type option struct {
label string
cmd tea.Cmd
label string
cmd tea.Cmd
keyBinding key.Binding
}

type Model struct {
Expand Down Expand Up @@ -44,21 +53,24 @@ func (m *Model) Init() tea.Cmd {
func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyLeft:
switch {
case key.Matches(msg, left):
if m.selected > 0 {
m.selected--
}
case tea.KeyRight:
case key.Matches(msg, right):
if m.selected < len(m.options) {
m.selected++
}
case tea.KeyEnter:
case key.Matches(msg, enter):
selectedOption := m.options[m.selected]
return m, selectedOption.cmd
case tea.KeyEscape:
return m, Close
default:
for _, option := range m.options {
if key.Matches(msg, option.keyBinding) {
return m, option.cmd
}
}
}
}
return m, nil
Expand All @@ -78,8 +90,8 @@ func (m *Model) View() string {
return lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).Padding(0, 1, 0, 1).Render(w.String())
}

func (m *Model) AddOption(label string, cmd tea.Cmd) {
m.options = append(m.options, option{label, cmd})
func (m *Model) AddOption(label string, cmd tea.Cmd, keyBinding key.Binding) {
m.options = append(m.options, option{label, cmd, keyBinding})
}

func New(message string) Model {
Expand Down
5 changes: 3 additions & 2 deletions internal/ui/operations/abandon/abandon.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package abandon

import (
"github.com/charmbracelet/bubbles/key"
"github.com/idursun/jjui/internal/ui/common"
"github.com/idursun/jjui/internal/ui/confirmation"

Expand Down Expand Up @@ -28,8 +29,8 @@ func (m Model) View() string {

func New(commands common.UICommands, revision string) tea.Model {
model := confirmation.New("Are you sure you want to abandon this revision?")
model.AddOption("Yes", tea.Batch(commands.Abandon(revision), common.Close))
model.AddOption("No", common.Close)
model.AddOption("Yes", tea.Batch(commands.Abandon(revision), common.Close), key.NewBinding(key.WithKeys("y")))
model.AddOption("No", common.Close, key.NewBinding(key.WithKeys("n", "esc")))

return Model{
confirmation: &model,
Expand Down
8 changes: 4 additions & 4 deletions internal/ui/operations/details/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
unselectedHint: "moves to the new revision",
})
model := confirmation.New("Are you sure you want to split the selected files?")
model.AddOption("Yes", tea.Batch(m.UICommands.Split(m.revision, selectedFiles), common.Close))
model.AddOption("No", confirmation.Close)
model.AddOption("Yes", tea.Batch(m.UICommands.Split(m.revision, selectedFiles), common.Close), key.NewBinding(key.WithKeys("y")))
model.AddOption("No", confirmation.Close, key.NewBinding(key.WithKeys("n", "esc")))
m.confirmation = &model
return m, m.confirmation.Init()
case key.Matches(msg, restore):
Expand All @@ -171,8 +171,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
unselectedHint: "stays as is",
})
model := confirmation.New("Are you sure you want to restore the selected files?")
model.AddOption("Yes", tea.Batch(m.UICommands.Restore(m.revision, selectedFiles), common.Close))
model.AddOption("No", confirmation.Close)
model.AddOption("Yes", tea.Batch(m.UICommands.Restore(m.revision, selectedFiles), common.Close), key.NewBinding(key.WithKeys("y")))
model.AddOption("No", confirmation.Close, key.NewBinding(key.WithKeys("n", "esc")))
m.confirmation = &model
return m, m.confirmation.Init()
case key.Matches(msg, mark):
Expand Down
5 changes: 3 additions & 2 deletions internal/ui/operations/undo/undo_operation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package undo

import (
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/idursun/jjui/internal/ui/common"
"github.com/idursun/jjui/internal/ui/confirmation"
Expand All @@ -27,7 +28,7 @@ func (o Operation) Render() string {

func NewOperation(commands common.UICommands) (operations.Operation, tea.Cmd) {
model := confirmation.New("Are you sure you want to undo last change?")
model.AddOption("Yes", tea.Batch(commands.Undo(), common.Close))
model.AddOption("No", common.Close)
model.AddOption("Yes", tea.Batch(commands.Undo(), common.Close), key.NewBinding(key.WithKeys("y")))
model.AddOption("No", common.Close, key.NewBinding(key.WithKeys("n", "esc")))
return Operation{Overlay: &model}, model.Init()
}

0 comments on commit 16b8fca

Please sign in to comment.