itor/gui/model.go

107 lines
3.1 KiB
Go
Raw Permalink Normal View History

2022-10-09 03:46:15 +00:00
package gui
import (
2022-10-09 05:21:00 +00:00
"gitea.tyrel.dev/tyrel/itor/models"
"github.com/charmbracelet/bubbles/list"
2022-10-09 03:46:15 +00:00
"github.com/charmbracelet/bubbles/textinput"
2022-10-09 05:55:21 +00:00
"reflect"
2022-10-11 03:32:01 +00:00
"sort"
2022-10-09 03:46:15 +00:00
)
type model struct {
2022-10-09 05:21:00 +00:00
deck []models.Card // items on the to-do list
selectCardIndex int // which to-do list item our selectCardIndex is pointing at
selected map[int]struct{} // which to-do items are selected
selectList list.Model
createInputs []textinput.Model // Inputs for front and back, is an array because we change position by an index
createdInputIndex int // holds an index for which input is active editing in create mode
2022-10-11 03:32:01 +00:00
drawSeen bool
drawCardId string
2022-10-09 05:55:21 +00:00
2022-10-09 05:21:00 +00:00
mode Mode
err error
2022-10-09 03:46:15 +00:00
}
2022-10-11 03:32:01 +00:00
/// returns an int slice of the selected card indexes, sorted
2022-10-09 05:55:21 +00:00
func (m *model) selectedCardIndexes() []int {
var keys []int
for _, key := range reflect.ValueOf(m.selected).MapKeys() {
keys = append(keys, int(key.Int()))
}
2022-10-11 03:32:01 +00:00
sort.Ints(keys)
2022-10-09 05:55:21 +00:00
return keys
}
2022-10-11 03:32:01 +00:00
/// Returns a card with matching drawCardId
func (m *model) selectedCard() models.Card {
for _, card := range m.deck {
if card.Id == m.drawCardId {
return card
}
}
return models.Card{
Front: "ERROR",
Back: "ERROR",
Id: "-1",
}
}
2022-10-09 03:46:15 +00:00
func initialModel() model {
front := textinput.New()
front.Placeholder = "Front..."
front.Focus()
front.CharLimit = 256
front.Width = 80
back := textinput.New()
back.Placeholder = "Back..."
back.CharLimit = 256
back.Width = 80
2022-10-09 05:21:00 +00:00
deck := []models.Card{
2022-10-11 03:32:01 +00:00
models.NewCard("Oui, je parle français.", "Yes, I speak French."),
models.NewCard("Non, je ne parle pas français.", "No, I dont speak French."),
models.NewCard("Merci beaucoup.", "Thank you very much."),
models.NewCard("Je mange le pain.", "I eat the bread."),
models.NewCard("Tu manges la salade.", "You seat the salad."),
models.NewCard("Vous mangez la pizza.", "You eat the pizza."),
models.NewCard("Le garçon chante.", "The boy sings."),
models.NewCard("La fille nage.", "The girl swims."),
models.NewCard("Les enfants chantent.", "The children sing."),
models.NewCard("Un garçon écrit.", "A boy writes."),
models.NewCard("Une fille dort.", "A girl sleeps."),
models.NewCard("Des enfant étudient.", "Some kids study."),
models.NewCard("Je le/la mange.", "I eat it."),
models.NewCard("Je les mange.", "I eat them."),
models.NewCard("Marc et Sylvie.", "Marc and Sylvie."),
models.NewCard("Il aime Sylvie mais il est trop timide.", "He likes Sylvie but hes ),too shy."),
2022-10-09 05:21:00 +00:00
}
var items []list.Item
for _, card := range deck {
items = append(items, card)
}
selectedList := list.New(items, itemDelegate{}, 80, 8)
selectedList.Title = "Card selection"
selectedList.SetShowStatusBar(false)
selectedList.SetFilteringEnabled(false)
selectedList.SetShowHelp(false)
selectedList.SetFilteringEnabled(false)
selectedList.Styles.Title = titleStyle
selectedList.Styles.PaginationStyle = paginationStyle
selectedList.Styles.HelpStyle = helpStyle
2022-10-09 03:46:15 +00:00
return model{
2022-10-09 05:21:00 +00:00
deck: deck,
selectList: selectedList,
2022-10-09 03:46:15 +00:00
createInputs: []textinput.Model{front, back},
selected: make(map[int]struct{}),
mode: Select,
}
}