From df0da429545895356259fecf23ea35fb9c938b61 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Tue, 12 Jan 2021 18:13:29 -0500 Subject: [PATCH] Add progressbar example --- examples/progress/main.go | 84 +++++++++++++++++++++++++++++++++++++++ examples/views/main.go | 4 ++ 2 files changed, 88 insertions(+) create mode 100644 examples/progress/main.go diff --git a/examples/progress/main.go b/examples/progress/main.go new file mode 100644 index 0000000..bda12f7 --- /dev/null +++ b/examples/progress/main.go @@ -0,0 +1,84 @@ +package main + +import ( + "fmt" + "os" + "strings" + "time" + + "github.com/charmbracelet/bubbles/progress" + tea "github.com/charmbracelet/bubbletea" +) + +const ( + fps = 60 + stepSize float64 = 1.0 / (float64(fps) * 2.0) + padding = 2 + maxWidth = 80 +) + +func main() { + prog, err := progress.NewModel(progress.WithScaledGradient("#FF7CCB", "#FDFF8C")) + if err != nil { + fmt.Println("Could not initialize progress model:", err) + os.Exit(1) + } + + if err = tea.NewProgram(example{progress: prog}).Start(); err != nil { + fmt.Println("Oh no!", err) + os.Exit(1) + } +} + +type tickMsg time.Time + +type example struct { + percent float64 + progress *progress.Model +} + +func (e example) Init() tea.Cmd { + return tickCmd() +} + +func (e example) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + + case tea.KeyMsg: + switch msg.String() { + case "q", "ctrl+c": + return e, tea.Quit + default: + return e, nil + } + + case tea.WindowSizeMsg: + e.progress.Width = msg.Width - padding*2 - 4 + if e.progress.Width > maxWidth { + e.progress.Width = maxWidth + } + return e, nil + + case tickMsg: + e.percent += stepSize + if e.percent > 1.0 { + e.percent = 1.0 + return e, tea.Quit + } + return e, tickCmd() + + default: + return e, nil + } +} + +func (e example) View() string { + pad := strings.Repeat(" ", padding) + return "\n" + pad + e.progress.View(e.percent) + "\n\n" +} + +func tickCmd() tea.Cmd { + return tea.Tick(time.Second/fps, func(t time.Time) tea.Msg { + return tickMsg(t) + }) +} diff --git a/examples/views/main.go b/examples/views/main.go index c4b85aa..6bc1143 100644 --- a/examples/views/main.go +++ b/examples/views/main.go @@ -1,6 +1,10 @@ package main // An example demonstrating an application with multiple views. +// +// Note that this example was produced before the Bubbles progress component +// was available (github.com/charmbracelet/bubbles/progress) and thus, we're +// implementing a progress bar from scratch here. import ( "fmt"