itor/gui/model.go

77 lines
1.9 KiB
Go
Raw 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-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-09 05:55:21 +00:00
drawSeen bool
drawCardIndex int
2022-10-09 05:21:00 +00:00
mode Mode
err error
2022-10-09 03:46:15 +00:00
}
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()))
}
return keys
}
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{
models.NewCard("Hello (JP)", "Konnichi wa"),
models.NewCard("Hello (SP)", "Hola"),
models.NewCard("Hello (DE)", "Guten Tag"),
}
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,
}
}