bubbletea/examples/progress-static/main.go
2025-11-14 14:46:54 -05:00

97 lines
2.1 KiB
Go

package main
// A simple example that shows how to render a progress bar in a "pure"
// fashion. In this example we bump the progress by 25% every second,
// maintaining the progress state on our top level model using the progress bar
// model's ViewAs method only for rendering.
//
// The signature for ViewAs is:
//
// func (m Model) ViewAs(percent float64) string
//
// So it takes a float between 0 and 1, and renders the progress bar
// accordingly. When using the progress bar in this "pure" fashion and there's
// no need to call an Update method.
//
// The progress bar is also able to animate itself, however. For details see
// the progress-animated example.
import (
"fmt"
"os"
"strings"
"time"
"charm.land/bubbles/v2/progress"
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
)
const (
padding = 2
maxWidth = 80
)
var (
helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#626262")).Render
yellow = lipgloss.Color("#FDFF8C")
pink = lipgloss.Color("#FF7CCB")
)
func main() {
prog := progress.New(progress.WithScaled(true), progress.WithColors(pink, yellow))
if _, err := tea.NewProgram(model{progress: prog}).Run(); err != nil {
fmt.Println("Oh no!", err)
os.Exit(1)
}
}
type tickMsg time.Time
type model struct {
percent float64
progress progress.Model
}
func (m model) Init() tea.Cmd {
return tickCmd()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyPressMsg:
return m, tea.Quit
case tea.WindowSizeMsg:
m.progress.SetWidth(msg.Width - padding*2 - 4)
if m.progress.Width() > maxWidth {
m.progress.SetWidth(maxWidth)
}
return m, nil
case tickMsg:
m.percent += 0.25
if m.percent > 1.0 {
m.percent = 1.0
return m, tea.Quit
}
return m, tickCmd()
default:
return m, nil
}
}
func (m model) View() tea.View {
pad := strings.Repeat(" ", padding)
return tea.NewView("\n" +
pad + m.progress.ViewAs(m.percent) + "\n\n" +
pad + helpStyle("Press any key to quit"))
}
func tickCmd() tea.Cmd {
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}