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
import (
"fmt"
"gitea.tyrel.dev/tyrel/itor/models"
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 {
s := fmt.Sprintf(
"Create a card:\n\nFront: %s\nBack: %s\n\n[Tab] switch field.\n[Enter] creates new card.\n[Escape] for back.",
m.createInputs[0].View(),
m.createInputs[1].View(),
)
return template(
`Create a card:
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 {
// TODO: translation I guess?
// Button Text
viewText := "1: View"
correctText := "2: Correct"
incorrectText := "3: Incorrect"
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()
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))
// Button text filling
if m.drawSeen {
correct = fmt.Sprintf("[%s]", hotPinkText.Width(10).Render(correctText))
incorrect = fmt.Sprintf("[%s]", hotPinkText.Width(12).Render(incorrectText))
}
if !m.drawSeen {
skip = fmt.Sprintf("[%s]", hotPinkText.Width(7).Render(skipText))
correctOption = fmt.Sprintf("[%s]", hotPinkText.Width(10).Render(correctText))
incorrectOption = fmt.Sprintf("[%s]", hotPinkText.Width(12).Render(incorrectText))
} else {
skipOption = fmt.Sprintf("[%s]", hotPinkText.Width(7).Render(skipText))
}
s += fmt.Sprintf("%s %s %s %s\n",
view,
correct,
incorrect,
skip)
selectedMap := fmt.Sprintf("%v", m.selected)
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 {

View File

@ -109,34 +109,19 @@ func (m *model) updateSelect(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, cmd
}
func (m *model) viewSelect() string {
s := "Select cards to draw:\n\n"
//// Iterate over our choices
//for i, choice := range m.deck {
//
// // 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()
return template(`Select cards to draw:
{selectList}
// The footer
s += "\nPress [↑/k] move up.\nPress [↓/j] move down.\nPress [enter/space] to toggle selection.\n"
s += "\nPress [c] to Create.\nPress [d] to Draw.\nPress [q] to quit.\n"
// Send the UI for rendering
return s
Press [/k] move up.
Press [/j] move down.
Press [enter/space] to toggle selection.
Press [c] to Create.
Press [d] to Draw.
Press [q] to quit.
`,
"{selectList}", m.selectList.View(),
)
}
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))
}