itor/gui/select.go

78 lines
1.7 KiB
Go

package gui
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
)
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() {
case "ctrl+c", "q":
return m, tea.Quit
// The "up" and "k" keys move the selectCardIndex up
case "up", "k":
if m.selectCardIndex > 0 {
m.selectCardIndex--
}
// The "down" and "j" keys move the selectCardIndex down
case "down", "j":
if m.selectCardIndex < len(m.deck)-1 {
m.selectCardIndex++
}
// MODES
case "d":
m.switchMode(Draw)
case "c":
m.switchMode(Create)
// The "enter" key and the spacebar (a literal space) toggle
// the selected state for the item that the selectCardIndex is pointing at.
case "enter", " ":
_, ok := m.selected[m.selectCardIndex]
if ok {
delete(m.selected, m.selectCardIndex)
} else {
m.selected[m.selectCardIndex] = struct{}{}
}
}
}
return m, nil
}
func (m *model) viewSelect() string {
s := "Select deck to query:\n\n"
// Iterate over our choices
for i, choice := range m.deck {
// Is the selectCardIndex pointing at this choice?
cursor := " " // no selectCardIndex
if m.selectCardIndex == i {
cursor = ">" // selectCardIndex!
}
// Is this choice selected?
checked := " " // not selected
if _, ok := m.selected[i]; ok {
checked = "✓" // selected!
}
// Render the row
s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice.Front)
}
// The footer
s += "\nPress [up/down/j/k] to Move up and down.\nPress [enter/space] to toggle selection.\n"
s += "\nPress [c] to Create.\nPress [d] to Draw.\nPress [q] to quit.\n"
// Send the UI for rendering
return s
}