From 414bb721a642670b73ee269ecfc50ffced4aa1f3 Mon Sep 17 00:00:00 2001 From: David Schlachter Date: Mon, 12 Jan 2026 00:21:47 -0500 Subject: Nicer progress bar for first run --- ui.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'ui.go') 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 { -- cgit v1.2.3