The progressModel struct wraps a github.com/charmbracelet/bubbles/progress model to integrate it into a Bubble Tea application. It handles window resizing to maintain a maximum width and manages periodic updates via a tickMsg.
Key behaviors:
- Resizing: Responds to
tea.WindowSizeMsg by adjusting the progress bar width, capped at a maxWidth of 80 characters. - Updating: Uses
m.progress.IncrPercent(float64) to increment progress. You can also use m.progress.SetPercent(float64) to set an explicit value. - Animation: Must handle
progress.FrameMsg in the Update loop to allow the progress bar to animate its internal state. - Termination: The model automatically quits when
m.progress.Percent() == 1.0 or when a key is pressed.
func (m progressModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
return m, tea.Quit
case tea.WindowSizeMsg:
m.progress.Width = msg.Width - padding*2 - 4
if m.progress.Width > maxWidth {
m.progress.Width = maxWidth
}
return m, nil
case tickMsg:
if m.progress.Percent() == 1.0 {
return m, tea.Quit
}
cmd := m.progress.IncrPercent(0.25)
return m, tea.Batch(tickCmd(), cmd)
case progress.FrameMsg:
progressModel, cmd := m.progress.Update(msg)
m.progress = progressModel.(progress.Model)
return m, cmd
default:
return m, nil
}
}