Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+88 added−0 removed
other:+88 added−0 removed
Provenance
Commit
df0da42
Tree
b50b55b
Author
Christian Rocha <christian@rocha.is>
Committer
Christian Rocha <christian@rocha.is>
Parent
9f04c93
2 files changed+88 added−0 removed
added:examples/progress/main.go+84 added
1+package main2+3+import (4+ "fmt"5+ "os"6+ "strings"7+ "time"8+9+ "github.com/charmbracelet/bubbles/progress"10+ tea "github.com/charmbracelet/bubbletea"11+)12+13+const (14+ fps = 6015+ stepSize float64 = 1.0 / (float64(fps) * 2.0)16+ padding = 217+ maxWidth = 8018+)19+20+func main() {21+ prog, err := progress.NewModel(progress.WithScaledGradient("#FF7CCB", "#FDFF8C"))22+ if err != nil {23+ fmt.Println("Could not initialize progress model:", err)24+ os.Exit(1)25+ }26+27+ if err = tea.NewProgram(example{progress: prog}).Start(); err != nil {28+ fmt.Println("Oh no!", err)29+ os.Exit(1)30+ }31+}32+33+type tickMsg time.Time34+35+type example struct {36+ percent float6437+ progress *progress.Model38+}39+40+func (e example) Init() tea.Cmd {41+ return tickCmd()42+}43+44+func (e example) Update(msg tea.Msg) (tea.Model, tea.Cmd) {45+ switch msg := msg.(type) {46+47+ case tea.KeyMsg:48+ switch msg.String() {49+ case "q", "ctrl+c":50+ return e, tea.Quit51+ default:52+ return e, nil53+ }54+55+ case tea.WindowSizeMsg:56+ e.progress.Width = msg.Width - padding*2 - 457+ if e.progress.Width > maxWidth {58+ e.progress.Width = maxWidth59+ }60+ return e, nil61+62+ case tickMsg:63+ e.percent += stepSize64+ if e.percent > 1.0 {65+ e.percent = 1.066+ return e, tea.Quit67+ }68+ return e, tickCmd()69+70+ default:71+ return e, nil72+ }73+}74+75+func (e example) View() string {76+ pad := strings.Repeat(" ", padding)77+ return "\n" + pad + e.progress.View(e.percent) + "\n\n"78+}79+80+func tickCmd() tea.Cmd {81+ return tea.Tick(time.Second/fps, func(t time.Time) tea.Msg {82+ return tickMsg(t)83+ })84+}
modified:examples/views/main.go+4 added
1package main23// An example demonstrating an application with multiple views.4+//5+// Note that this example was produced before the Bubbles progress component6+// was available (github.com/charmbracelet/bubbles/progress) and thus, we're7+// implementing a progress bar from scratch here.89import (10 "fmt"11 "math"