itor/gui/update.go

73 lines
1.4 KiB
Go

package gui
import tea "github.com/charmbracelet/bubbletea"
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.mode == Draw {
return m.updateDraw(msg)
}
// Select is default mode
return m.updateSelect(msg)
}
func (m *model) updateDraw(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
// Is it a key press?
case tea.KeyMsg:
switch msg.String() {
case "s":
m.switchMode(Select)
// These keys should exit the program.
case "ctrl+c", "q":
return m, tea.Quit
}
}
return m, nil
}
func (m *model) updateSelect(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
// Is it a key press?
case tea.KeyMsg:
switch msg.String() {
// These keys should exit the program.
case "ctrl+c", "q":
return m, tea.Quit
// The "up" and "k" keys move the cursor up
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
// The "down" and "j" keys move the cursor down
case "down", "j":
if m.cursor < len(m.cards)-1 {
m.cursor++
}
case "d":
m.switchMode(Draw)
// The "enter" key and the spacebar (a literal space) toggle
// the selected state for the item that the cursor is pointing at.
case "enter", " ":
_, ok := m.selected[m.cursor]
if ok {
delete(m.selected, m.cursor)
} else {
m.selected[m.cursor] = struct{}{}
}
}
}
// Return the updated model to the Bubble Tea runtime for processing.
// Note that we're not returning a command.
return m, nil
}