98 lines
1.8 KiB
Go
98 lines
1.8 KiB
Go
package gui
|
|
|
|
import (
|
|
"fmt"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
type Mode int64
|
|
|
|
const (
|
|
Select Mode = iota
|
|
Create
|
|
Draw
|
|
)
|
|
|
|
type Card struct {
|
|
Front string
|
|
Back string
|
|
LastCorrect time.Time
|
|
}
|
|
|
|
type model struct {
|
|
cards []Card // items on the to-do list
|
|
cursor int // which to-do list item our cursor is pointing at
|
|
selected map[int]struct{} // which to-do items are selected
|
|
mode Mode
|
|
}
|
|
|
|
func initialModel() model {
|
|
return model{
|
|
cards: []Card{
|
|
{Front: "Hello (JP)", Back: "Konnichi wa"},
|
|
{Front: "Hello (SP)", Back: "Hola"},
|
|
{Front: "Hello (DE)", Back: "Guten Tag"},
|
|
},
|
|
|
|
selected: make(map[int]struct{}),
|
|
mode: Select,
|
|
}
|
|
}
|
|
|
|
func (m *model) Init() tea.Cmd {
|
|
// Just return `nil`, which means "no I/O right now, please."
|
|
return nil
|
|
}
|
|
|
|
func (m *model) switchMode(mode Mode) {
|
|
// TODO: initialization stuff
|
|
switch mode {
|
|
case Select:
|
|
m.mode = Select
|
|
case Draw:
|
|
m.mode = Draw
|
|
case Create:
|
|
m.mode = Create
|
|
}
|
|
}
|
|
|
|
func (m *model) cardListToString() string {
|
|
s := ""
|
|
for i, card := range m.cards {
|
|
if _, ok := m.selected[i]; ok {
|
|
s += fmt.Sprintf("%s [%s]\n", card.Front, card.Back)
|
|
}
|
|
}
|
|
return s
|
|
}
|
|
|
|
func (m *model) View() string {
|
|
if m.mode == Draw {
|
|
return m.viewDraw()
|
|
} else if m.mode == Create {
|
|
return m.viewCreate()
|
|
}
|
|
// Select is default mode
|
|
return m.viewSelect()
|
|
}
|
|
|
|
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
if m.mode == Draw {
|
|
return m.updateDraw(msg)
|
|
} else if m.mode == Create {
|
|
return m.updateCreate(msg)
|
|
}
|
|
// Select is default mode
|
|
return m.updateSelect(msg)
|
|
}
|
|
func Run() {
|
|
initial := initialModel()
|
|
p := tea.NewProgram(&initial)
|
|
if err := p.Start(); err != nil {
|
|
fmt.Printf("Alas, there's been an error: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|