summaryrefslogtreecommitdiff
path: root/ui.go
diff options
context:
space:
mode:
authorDavid Schlachter <t480-debian-git@schlachter.ca>2026-01-12 00:21:47 -0500
committerDavid Schlachter <t480-debian-git@schlachter.ca>2026-01-12 00:21:47 -0500
commit414bb721a642670b73ee269ecfc50ffced4aa1f3 (patch)
tree9c763c750bd8741b9d199c06dc226ea141c89115 /ui.go
parentc7e2672b0083eb81ed4d494e1f90b30e0a86c7d9 (diff)
Nicer progress bar for first run
Diffstat (limited to 'ui.go')
-rw-r--r--ui.go22
1 files changed, 17 insertions, 5 deletions
diff --git a/ui.go b/ui.go
index 9bac321..05c7e7d 100644
--- a/ui.go
+++ b/ui.go
@@ -8,6 +8,7 @@ import (
"strings"
"github.com/charmbracelet/bubbles/key"
+ "github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
@@ -23,6 +24,7 @@ type model struct {
wordInput textinput.Model
definitionViewport viewport.Model
+ importProgress progress.Model
ankiDeck string
ankiModel string
@@ -34,6 +36,7 @@ type model struct {
currentWord string
currentDefinition string
statusString string
+ importFraction float64
err error
}
@@ -82,6 +85,7 @@ func initialModel(c *http.Client, db *sql.DB, apiURL, ankiDeck, ankiModel, rawDi
wordInput: input,
definitionViewport: textbox,
+ importProgress: progress.New(progress.WithDefaultGradient()),
ankiDeck: ankiDeck,
ankiModel: ankiModel,
@@ -115,7 +119,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}
case populatingDictionaryMsg:
- m.statusString = fmt.Sprintf("Currently on line %d...", msg.currentLine)
+ m.importFraction = float64(msg.currentLine) / float64(msg.totalLines)
return m, populateDictionary((*dictionaryPopulator)(msg))
case definitionMsg:
m.currentDefinition = string(msg)
@@ -130,12 +134,17 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.definitionViewport.SetContent("")
m.err = nil
case tea.WindowSizeMsg:
- // headerHeight is the height of everything above the definition window.
- headerHeight := 11
+ m.importProgress.Width = msg.Width
m.wordInput.Width = msg.Width
m.definitionViewport.Width = msg.Width
+
+ // headerHeight is the height of everything above the definition window.
+ headerHeight := 11
+
m.definitionViewport.Height = msg.Height - headerHeight
- m.definitionViewport.SetContent(formatDefinitionForDisplay(m.p, m.currentDefinition, m.definitionViewport.Width))
+ m.definitionViewport.SetContent(
+ formatDefinitionForDisplay(m.p, m.currentDefinition, m.definitionViewport.Width),
+ )
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC, tea.KeyCtrlD:
@@ -182,7 +191,10 @@ func (m model) View() string {
if m.err != nil {
return fmt.Sprintf("Failed to load dictionary! Error:\n\n%s\n\nExit with Control-C.\n", m.err)
}
- return fmt.Sprintf("Preparing dictionary...\n\n%s\n\n", m.statusString)
+ if m.importFraction == 0 {
+ return "\nWe'll start preparing the dictionary in a few seconds...\n\n\n\n"
+ }
+ return fmt.Sprintf("\nPreparing dictionary...\n\n%s\n\n", m.importProgress.ViewAs(m.importFraction))
}
func formatDefinitionForDisplay(policy bluemonday.Policy, definition string, maxWidth int) string {