itor/gui/model.go

107 lines
3.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package gui
import (
"gitea.tyrel.dev/tyrel/itor/models"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
"reflect"
"sort"
)
type model struct {
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
drawSeen bool
drawCardId string
mode Mode
err error
}
/// returns an int slice of the selected card indexes, sorted
func (m *model) selectedCardIndexes() []int {
var keys []int
for _, key := range reflect.ValueOf(m.selected).MapKeys() {
keys = append(keys, int(key.Int()))
}
sort.Ints(keys)
return keys
}
/// 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",
}
}
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
deck := []models.Card{
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."),
}
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
return model{
deck: deck,
selectList: selectedList,
createInputs: []textinput.Model{front, back},
selected: make(map[int]struct{}),
mode: Select,
}
}