itor/gui/draw.go

137 lines
3.4 KiB
Go
Raw Normal View History

2022-10-09 03:18:46 +00:00
package gui
2022-10-09 05:21:00 +00:00
import (
"fmt"
2022-10-11 03:32:01 +00:00
"math/rand"
2022-10-09 05:55:21 +00:00
"strings"
2022-10-11 03:32:01 +00:00
"time"
2022-10-09 05:21:00 +00:00
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
2022-10-11 03:32:01 +00:00
var hotPinkTextStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FF06B7"))
var frontTextStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#268bd2"))
var backTextStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#6c71c4"))
2022-10-09 03:18:46 +00:00
func (m *model) updateDraw(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
2022-10-09 05:55:21 +00:00
// string value of keypresses
2022-10-09 03:18:46 +00:00
switch msg.String() {
2022-10-09 05:55:21 +00:00
case "1": // View
m.drawSeen = true
case "2": // Correct
if m.drawSeen {
2022-10-11 03:32:01 +00:00
m.drawSeen = false
m.drawRandomCard()
2022-10-09 05:55:21 +00:00
}
case "3": // Incorrect
if m.drawSeen {
2022-10-11 03:32:01 +00:00
m.drawSeen = false
m.drawRandomCard()
2022-10-09 05:55:21 +00:00
}
case "4": // Skip
if !m.drawSeen {
2022-10-11 03:32:01 +00:00
m.drawSeen = false
m.drawRandomCard()
2022-10-09 05:55:21 +00:00
}
2022-10-09 03:18:46 +00:00
case "ctrl+c", "q":
return m, tea.Quit
case "s":
m.switchMode(Select)
}
}
2022-10-11 03:32:01 +00:00
return m, tea.Tick(time.Second, func(_ time.Time) tea.Msg {
return nil
})
2022-10-09 03:18:46 +00:00
}
2022-10-09 05:21:00 +00:00
2022-10-09 03:18:46 +00:00
func (m *model) viewDraw() string {
2022-10-11 03:32:01 +00:00
card := m.selectedCard()
2022-10-11 02:53:02 +00:00
// Button Text
2022-10-09 05:55:21 +00:00
viewText := "1: View"
correctText := "2: Correct"
incorrectText := "3: Incorrect"
skipText := "4: Skip"
2022-10-11 02:53:02 +00:00
// Buttons default
2022-10-11 03:32:01 +00:00
viewOption := fmt.Sprintf("[%s]", hotPinkTextStyle.Width(7).Render(viewText))
2022-10-11 02:53:02 +00:00
correctOption := fmt.Sprintf(strings.Repeat(" ", len(correctText)+2))
incorrectOption := fmt.Sprintf(strings.Repeat(" ", len(incorrectText)+2))
skipOption := fmt.Sprintf(strings.Repeat(" ", len(skipText)+2))
2022-10-09 05:55:21 +00:00
2022-10-11 02:53:02 +00:00
// Button text filling
2022-10-11 03:32:01 +00:00
frontText := frontTextStyle.Width(len(card.Front)).Render(card.Front)
backText := ""
2022-10-09 05:55:21 +00:00
if m.drawSeen {
2022-10-11 03:32:01 +00:00
backText = backTextStyle.Width(len(card.Back)).Render(card.Back)
correctOption = fmt.Sprintf("[%s]", hotPinkTextStyle.Width(10).Render(correctText))
incorrectOption = fmt.Sprintf("[%s]", hotPinkTextStyle.Width(12).Render(incorrectText))
2022-10-11 02:53:02 +00:00
} else {
2022-10-11 03:32:01 +00:00
skipOption = fmt.Sprintf("[%s]", hotPinkTextStyle.Width(7).Render(skipText))
2022-10-09 05:55:21 +00:00
}
2022-10-09 03:18:46 +00:00
2022-10-11 02:53:02 +00:00
selectedMap := fmt.Sprintf("%v", m.selected)
2022-10-09 03:18:46 +00:00
2022-10-11 02:53:02 +00:00
// Return a template
return template(`Draw Mode ({numSelected} cards, {selectedCardIndexes}):
2022-10-11 03:32:01 +00:00
{frontTextStyle}
{backTextStyle}
2022-10-11 02:53:02 +00:00
{viewOption} {correctOption} {incorrectOption} {skipOption}
Press [s] to go back to select
Press [q] to quit.
`,
"{numSelected}", fmt.Sprintf("%d", len(m.selected)),
"{selectedCardIndexes}", fmt.Sprintf("%v", m.selectedCardIndexes()),
"{cardList}", m.cardListToString(),
2022-10-11 03:32:01 +00:00
"{selectedMap}", fmt.Sprintf("%s", frontTextStyle.Width(len(selectedMap)).Render(selectedMap)),
2022-10-11 02:53:02 +00:00
"{viewOption}", viewOption,
"{correctOption}", correctOption,
"{incorrectOption}", incorrectOption,
"{skipOption}", skipOption,
2022-10-11 03:32:01 +00:00
"{frontTextStyle}", frontText,
"{backTextStyle}", backText,
2022-10-11 02:53:02 +00:00
)
2022-10-09 03:46:15 +00:00
2022-10-09 03:18:46 +00:00
}
2022-10-09 05:21:00 +00:00
func (m *model) cardListToString() string {
s := ""
for i, card := range m.deck {
if _, ok := m.selected[i]; ok {
2022-10-09 05:55:21 +00:00
s += fmt.Sprintf("%d %s [%s]\n", i, card.Front, card.Back)
2022-10-09 05:21:00 +00:00
}
}
return s
}
2022-10-11 03:32:01 +00:00
func (m *model) drawRandomCard() {
if len(m.selectedCardIndexes()) == 1 {
// Can't draw a next card, just return same card.
return
}
rand.Seed(time.Now().Unix()) // initialize global pseudo random generator
indexes := m.selectedCardIndexes()
nextId := m.drawCardId
// Randomize until the next card is not the same card
for {
randomIndex := indexes[rand.Intn(len(indexes))]
nextId = m.deck[randomIndex].Id
if m.drawCardId != nextId {
break
}
}
m.drawCardId = nextId
}