Getting started
progress-go draws a progress bar for a known amount of work. It has one type, Bar, and three calls:
- Start starts the clock and, on a terminal, draws the bar and keeps redrawing it.
- Advance adds to the work done, from any goroutine.
- Stop draws the bar one last time, ends its line, and returns any error from writing it.
Install
Section titled “Install”progress-go needs Go 1.26 or newer.
go get github.com/vardius/progress-goYour first bar
Section titled “Your first bar”This program does ten steps of a tenth of a second each, and advances a verbose bar after every step:
// Command count counts to ten, a tenth of a second at a time, under a verbose bar.//// go run ./examples/countpackage main
import ( "io" "log" "os" "time"
"github.com/vardius/progress-go")
// count does n steps of work, each taking the given time, and advances the bar after each.func count(out io.Writer, n int, each time.Duration) error { bar := progress.New(int64(n), progress.Options{Output: out, Verbose: true}) bar.Start() for range n { time.Sleep(each) // a step of real work goes here bar.Advance(1) } return bar.Stop()}
func main() { log.SetFlags(0) if err := count(os.Stdout, 10, 100*time.Millisecond); err != nil { log.Fatal(err) }}Run it
Section titled “Run it”-
From a clone of the repository, run it in a terminal:
Terminal window go run ./examples/countThe bar fills over a second, redrawn in place. Once it stops, the line reads:
10/10 [██████████████████████████████████████████████████] 100% 176 KB 1s/1s 0sThe heap size,
176 KBhere, depends on your program. -
Pipe it into another command. Its output is no longer a terminal, so the bar writes no escape codes and no frames along the way: only the last line, once.
Terminal window go run ./examples/count | cat
What just happened
Section titled “What just happened”progress.Newmade a bar for 10 units of work.Options{Verbose: true}added the heap size and the times. With noOutput, the bar draws toos.Stdout.Startstarted the clock. Standard output was a terminal, so it drew the bar and started a timer that redraws it every 100 ms.- Each
Advance(1)added one to the count and returned at once. The next redraw showed it. Stopstopped the timer, drew the bar a last time, and ended the line. It returns the first error from writing the bar, if there was one.
Next steps
Section titled “Next steps”Counting workItems, bytes, resuming part way, and work that overshoots.
OutputTerminals and logs, the glyph, verbose fields, write errors.
GoroutinesWorkers that share one bar, and what Advance costs.
BenchmarksWhat each call costs, and how v1 compares with the untagged version.