itor/gui/select.go

78 lines
1.7 KiB
Go
Raw Normal View History

2022-10-09 03:10:26 +00:00
package gui
2022-10-09 03:18:46 +00:00
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
)
2022-10-09 03:10:26 +00:00
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
2022-10-09 03:46:15 +00:00
// The "up" and "k" keys move the selectCardIndex up
2022-10-09 03:10:26 +00:00
case "up", "k":
2022-10-09 03:46:15 +00:00
if m.selectCardIndex > 0 {
m.selectCardIndex--
2022-10-09 03:10:26 +00:00
}
2022-10-09 03:46:15 +00:00
// The "down" and "j" keys move the selectCardIndex down
2022-10-09 03:10:26 +00:00
case "down", "j":
2022-10-09 03:46:15 +00:00
if m.selectCardIndex < len(m.cards)-1 {
m.selectCardIndex++
2022-10-09 03:10:26 +00:00
}
2022-10-09 03:18:46 +00:00
// MODES
2022-10-09 03:10:26 +00:00
case "d":
m.switchMode(Draw)
2022-10-09 03:18:46 +00:00
case "c":
m.switchMode(Create)
2022-10-09 03:10:26 +00:00
// The "enter" key and the spacebar (a literal space) toggle
2022-10-09 03:46:15 +00:00
// the selected state for the item that the selectCardIndex is pointing at.
2022-10-09 03:10:26 +00:00
case "enter", " ":
2022-10-09 03:46:15 +00:00
_, ok := m.selected[m.selectCardIndex]
2022-10-09 03:10:26 +00:00
if ok {
2022-10-09 03:46:15 +00:00
delete(m.selected, m.selectCardIndex)
2022-10-09 03:10:26 +00:00
} else {
2022-10-09 03:46:15 +00:00
m.selected[m.selectCardIndex] = struct{}{}
2022-10-09 03:10:26 +00:00
}
}
}
return m, nil
}
2022-10-09 03:18:46 +00:00
func (m *model) viewSelect() string {
s := "Select cards to query:\n\n"
// Iterate over our choices
for i, choice := range m.cards {
2022-10-09 03:46:15 +00:00
// Is the selectCardIndex pointing at this choice?
cursor := " " // no selectCardIndex
if m.selectCardIndex == i {
cursor = ">" // selectCardIndex!
2022-10-09 03:18:46 +00:00
}
// 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
2022-10-09 03:46:15 +00:00
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"
2022-10-09 03:18:46 +00:00
// Send the UI for rendering
return s
}