Skip to content
Myr

Add progressbar example

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christian Rocha · committed by Christian Rocha · parent 9f04c93 · Browse files · 2 files+88 added−0 removed
This commit

State

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

2 files+88 added−0 removed

added:examples/progress/main.go+84 added
1package main23import (4 "fmt"5 "os"6 "strings"7 "time"89 "github.com/charmbracelet/bubbles/progress"10 tea "github.com/charmbracelet/bubbletea"11)1213const (14 fps = 6015 stepSize float64 = 1.0 / (float64(fps) * 2.0)16 padding = 217 maxWidth = 8018)1920func 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 }2627 if err = tea.NewProgram(example{progress: prog}).Start(); err != nil {28 fmt.Println("Oh no!", err)29 os.Exit(1)30 }31}3233type tickMsg time.Time3435type example struct {36 percent float6437 progress *progress.Model38}3940func (e example) Init() tea.Cmd {41 return tickCmd()42}4344func (e example) Update(msg tea.Msg) (tea.Model, tea.Cmd) {45 switch msg := msg.(type) {4647 case tea.KeyMsg:48 switch msg.String() {49 case "q", "ctrl+c":50 return e, tea.Quit51 default:52 return e, nil53 }5455 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, nil6162 case tickMsg:63 e.percent += stepSize64 if e.percent > 1.0 {65 e.percent = 1.066 return e, tea.Quit67 }68 return e, tickCmd()6970 default:71 return e, nil72 }73}7475func (e example) View() string {76 pad := strings.Repeat(" ", padding)77 return "\n" + pad + e.progress.View(e.percent) + "\n\n"78}7980func 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"
278 unmodified lines