mpb is a Go lib for rendering progress bars in terminal applications.
It is inspired by uiprogress library, but unlike the last one, implementation is mutex free, following Go's idiom:
Don't communicate by sharing memory, share memory by communicating.
- Multiple Bars: mpb can render multiple progress bars that can be tracked concurrently
- Cancellable: cancel rendering goroutine at any time
- Dynamic Addition: Add additional progress bar at any time
- Dynamic Removal: Remove rendering progress bar at any time
- Dynamic Sorting: Sort bars as you wish
- Dynamic Resize: Resize bars on terminal width change
- Custom Decorator Functions: Add custom functions around the bar along with helper functions
- Dynamic Decorator's Width Sync: Sync width among decorator group (available since v2)
- Predefined Decoratros: Elapsed time, Ewmaest based ETA, Percentage, Bytes counter
To get the package, execute:
go get gopkg.in/vbauerster/mpb.v2
Following is the simplest use case:
// Star mpb's rendering goroutine.
p := mpb.New()
// Set custom width for every bar, which mpb will render
// The default one is 80
p.SetWidth(100)
// Set custom format for every bar, the default one is "[=>-]"
p.Format("╢▌▌░╟")
// Set custom refresh rate, the default one is 100 ms
p.RefreshRate(120 * time.Millisecond)
// Add a bar. You're not limited to just one bar, add many if you need.
bar := p.AddBar(100).PrependName("Single Bar:", 0, 0).AppendPercentage(5, 0)
for i := 0; i < 100; i++ {
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
bar.Incr(1) // increment progress bar
}
// Don't forget to stop mpb's rendering goroutine
p.Stop()
Running this, will produce:
However mpb was designed with concurrency in mind. Each new bar renders in its own goroutine, therefore adding multiple bars is easy and safe:
var wg sync.WaitGroup
p := mpb.New()
wg.Add(3) // add wg delta
for i := 0; i < 3; i++ {
name := fmt.Sprintf("Bar#%d:", i)
bar := p.AddBar(100).
PrependName(name, len(name), 0).
// Prepend Percentage decorator and sync width
PrependPercentage(3, mpb.DwidthSync|mpb.DextraSpace).
// Append ETA and don't sync width
AppendETA(2, 0)
go func() {
defer wg.Done()
// you can p.AddBar() here, but ordering will be non deterministic
// if you still need p.AddBar() here and maintain ordering, use
// (*mpb.Progress).BeforeRenderFunc(f mpb.BeforeRender)
for i := 0; i < 100; i++ {
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
bar.Incr(1)
}
}()
}
wg.Wait() // Wait for goroutines to finish
p.Stop() // Stop mpb's rendering goroutine
The source code: example/simple/main.go
To cancel use either WithCancel or WithContext method. The last one requires Go 1.7
The source code: example/cancel/main.go
The source code: example/remove/main.go
The source code: example/sort/main.go
The source code: example/prependETA/main.go
The source code: example/io/multiple/main.go
Refer to godoc example.
The typeface used in screen shots: Iosevka