templating

This commit is contained in:
Tyrel Souza 2022-10-10 22:53:02 -04:00
parent 5965b3300a
commit 4c6c12ebe5
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E
4 changed files with 65 additions and 56 deletions

View File

@ -1,7 +1,6 @@
package gui package gui
import ( import (
"fmt"
"gitea.tyrel.dev/tyrel/itor/models" "gitea.tyrel.dev/tyrel/itor/models"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
@ -76,11 +75,16 @@ func (m *model) updateCreate(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
func (m *model) viewCreate() string { func (m *model) viewCreate() string {
s := fmt.Sprintf( return template(
"Create a card:\n\nFront: %s\nBack: %s\n\n[Tab] switch field.\n[Enter] creates new card.\n[Escape] for back.", `Create a card:
m.createInputs[0].View(),
m.createInputs[1].View(),
)
return s Front: {frontInput}
Back: {backInput}
[Tab] switch field.
[Enter] creates new card.
[Escape] for back.`,
"{frontInput}", m.createInputs[0].View(),
"{backInput}", m.createInputs[1].View(),
)
} }

View File

@ -49,40 +49,49 @@ func (m *model) updateDraw(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
func (m *model) viewDraw() string { func (m *model) viewDraw() string {
// TODO: translation I guess? // Button Text
viewText := "1: View" viewText := "1: View"
correctText := "2: Correct" correctText := "2: Correct"
incorrectText := "3: Incorrect" incorrectText := "3: Incorrect"
skipText := "4: Skip" skipText := "4: Skip"
correct := fmt.Sprintf(strings.Repeat(" ", len(correctText)+2))
incorrect := fmt.Sprintf(strings.Repeat(" ", len(incorrectText)+2))
skip := fmt.Sprintf(strings.Repeat(" ", len(skipText)+2))
s := fmt.Sprintf("Draw Mode (%d cards, %v):\n\n", len(m.selected), m.selectedCardIndexes()) // Buttons default
viewOption := fmt.Sprintf("[%s]", hotPinkText.Width(7).Render(viewText))
correctOption := fmt.Sprintf(strings.Repeat(" ", len(correctText)+2))
incorrectOption := fmt.Sprintf(strings.Repeat(" ", len(incorrectText)+2))
skipOption := fmt.Sprintf(strings.Repeat(" ", len(skipText)+2))
s += m.cardListToString() // Button text filling
text := fmt.Sprintf("%s", m.selected)
s += fmt.Sprintf("%s", frontText.Width(len(text)).Render(text))
view := fmt.Sprintf("[%s]", hotPinkText.Width(7).Render(viewText))
if m.drawSeen { if m.drawSeen {
correct = fmt.Sprintf("[%s]", hotPinkText.Width(10).Render(correctText)) correctOption = fmt.Sprintf("[%s]", hotPinkText.Width(10).Render(correctText))
incorrect = fmt.Sprintf("[%s]", hotPinkText.Width(12).Render(incorrectText)) incorrectOption = fmt.Sprintf("[%s]", hotPinkText.Width(12).Render(incorrectText))
} } else {
if !m.drawSeen { skipOption = fmt.Sprintf("[%s]", hotPinkText.Width(7).Render(skipText))
skip = fmt.Sprintf("[%s]", hotPinkText.Width(7).Render(skipText))
} }
s += fmt.Sprintf("%s %s %s %s\n", selectedMap := fmt.Sprintf("%v", m.selected)
view,
correct,
incorrect,
skip)
s += "\nPress [s] to go back to select\nPress [q] to quit." // Return a template
return template(`Draw Mode ({numSelected} cards, {selectedCardIndexes}):
{cardList}
{selectedMap}
{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(),
"{selectedMap}", fmt.Sprintf("%s", frontText.Width(len(selectedMap)).Render(selectedMap)),
"{viewOption}", viewOption,
"{correctOption}", correctOption,
"{incorrectOption}", incorrectOption,
"{skipOption}", skipOption,
)
return s
} }
func (m *model) cardListToString() string { func (m *model) cardListToString() string {

View File

@ -109,34 +109,19 @@ func (m *model) updateSelect(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, cmd return m, cmd
} }
func (m *model) viewSelect() string { func (m *model) viewSelect() string {
s := "Select cards to draw:\n\n"
//// Iterate over our choices return template(`Select cards to draw:
//for i, choice := range m.deck { {selectList}
//
// // Is the selectCardIndex pointing at this choice?
// cursor := " " // no selectCardIndex
// if m.selectCardIndex == i {
// cursor = ">" // selectCardIndex!
// }
//
// // Is this choice selected?
// checked := " " // not selected
// if _, ok := m.selected[i]; ok {
// checked = "✓" // selected!
// }
//
// // Render the row
// s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice.Front)
//}
s += m.selectList.View()
// The footer Press [/k] move up.
s += "\nPress [↑/k] move up.\nPress [↓/j] move down.\nPress [enter/space] to toggle selection.\n" Press [/j] move down.
s += "\nPress [c] to Create.\nPress [d] to Draw.\nPress [q] to quit.\n" Press [enter/space] to toggle selection.
Press [c] to Create.
// Send the UI for rendering Press [d] to Draw.
return s Press [q] to quit.
`,
"{selectList}", m.selectList.View(),
)
} }
func SliceIndex(limit int, predicate func(i int) bool) int { func SliceIndex(limit int, predicate func(i int) bool) int {

11
gui/utils.go Normal file
View File

@ -0,0 +1,11 @@
package gui
import (
"fmt"
"strings"
)
func template(format string, args ...string) string {
r := strings.NewReplacer(args...)
return fmt.Sprint(r.Replace(format))
}