From 4c6c12ebe5f0b23a36e832a82b98b86b57e7ec17 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Mon, 10 Oct 2022 22:53:02 -0400 Subject: [PATCH] templating --- gui/create.go | 18 ++++++++++------- gui/draw.go | 55 ++++++++++++++++++++++++++++++--------------------- gui/select.go | 37 +++++++++++----------------------- gui/utils.go | 11 +++++++++++ 4 files changed, 65 insertions(+), 56 deletions(-) create mode 100644 gui/utils.go diff --git a/gui/create.go b/gui/create.go index 5ed1e32..22fe247 100644 --- a/gui/create.go +++ b/gui/create.go @@ -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(), + ) } diff --git a/gui/draw.go b/gui/draw.go index 44c5720..cca63a1 100644 --- a/gui/draw.go +++ b/gui/draw.go @@ -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 { diff --git a/gui/select.go b/gui/select.go index e431682..84d19ea 100644 --- a/gui/select.go +++ b/gui/select.go @@ -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 { diff --git a/gui/utils.go b/gui/utils.go new file mode 100644 index 0000000..6c0e73d --- /dev/null +++ b/gui/utils.go @@ -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)) +}