Top Related Projects
A really basic thread-safe progress bar for Golang applications
Console progress bar for Golang
A go library to render progress bars in terminal applications
Quick Overview
mpb (Multi Progress Bar) is a Go library for rendering multiple progress bars in terminal applications. It provides a flexible and customizable way to display concurrent progress indicators, making it ideal for applications that need to show progress for multiple tasks simultaneously.
Pros
- Supports multiple concurrent progress bars
- Highly customizable with various decorators and styles
- Thread-safe and efficient
- Integrates well with Go's concurrency model
Cons
- Limited to terminal-based applications
- May have a learning curve for complex customizations
- Documentation could be more comprehensive
- Might be overkill for simple progress tracking needs
Code Examples
Basic progress bar:
package main
import (
"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
"time"
)
func main() {
p := mpb.New()
bar := p.New(100,
mpb.BarStyle().Rbound("|"),
mpb.PrependDecorators(
decor.Name("Basic"),
decor.CountersNoUnit("%d / %d"),
),
)
for i := 0; i < 100; i++ {
bar.Increment()
time.Sleep(50 * time.Millisecond)
}
p.Wait()
}
Multiple progress bars:
package main
import (
"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
"math/rand"
"time"
)
func main() {
p := mpb.New()
total, numBars := 100, 3
for i := 0; i < numBars; i++ {
name := fmt.Sprintf("Bar #%d:", i)
bar := p.AddBar(int64(total),
mpb.PrependDecorators(
decor.Name(name),
decor.Percentage(decor.WCSyncSpace),
),
mpb.AppendDecorators(
decor.ETA(decor.ET_STYLE_GO),
),
)
go func() {
for i := 0; i < total; i++ {
bar.Increment()
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
}
}()
}
p.Wait()
}
Getting Started
To use mpb in your Go project, first install it:
go get github.com/vbauerster/mpb/v8
Then, import it in your Go code:
import (
"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
)
Create a progress container and add bars to it:
p := mpb.New()
bar := p.AddBar(100,
mpb.PrependDecorators(decor.Name("Progress")),
mpb.AppendDecorators(decor.Percentage()),
)
// Update progress
for i := 0; i < 100; i++ {
bar.Increment()
time.Sleep(50 * time.Millisecond)
}
// Wait for all bars to complete
p.Wait()
Competitor Comparisons
A really basic thread-safe progress bar for Golang applications
Pros of progressbar
- Simpler API and easier to use for basic progress bar needs
- Includes built-in support for spinners and automatic refresh
- Lightweight with minimal dependencies
Cons of progressbar
- Less customizable and flexible compared to mpb
- Fewer advanced features like multiple bars or custom decorators
- Limited support for concurrent progress tracking
Code Comparison
progressbar:
bar := progressbar.Default(100)
for i := 0; i < 100; i++ {
bar.Add(1)
time.Sleep(10 * time.Millisecond)
}
mpb:
p := mpb.New()
bar := p.AddBar(100)
for i := 0; i < 100; i++ {
bar.Increment()
time.Sleep(10 * time.Millisecond)
}
p.Wait()
Both libraries offer straightforward ways to create and update progress bars. progressbar provides a more concise API for simple use cases, while mpb offers greater flexibility and control over the progress bar's behavior and appearance.
progressbar is ideal for quick implementations and projects with basic progress tracking needs. mpb is better suited for complex applications requiring multiple concurrent progress bars or extensive customization options.
Console progress bar for Golang
Pros of pb
- Simpler API and easier to get started with for basic use cases
- Supports Windows console coloring out of the box
- Has been around longer and is more widely used
Cons of pb
- Less flexible and customizable compared to mpb
- Lacks advanced features like dynamic total updates and multiple bars
Code Comparison
pb:
bar := pb.New(100)
bar.Start()
for i := 0; i < 100; i++ {
bar.Increment()
time.Sleep(time.Millisecond * 10)
}
bar.Finish()
mpb:
p := mpb.New()
bar := p.AddBar(100)
for i := 0; i < 100; i++ {
bar.Increment()
time.Sleep(time.Millisecond * 10)
}
p.Wait()
Summary
pb is a simpler, more established progress bar library that's easier to use for basic scenarios. It has built-in Windows console support but lacks some advanced features.
mpb offers more flexibility and advanced features like dynamic updates and multiple progress bars. It has a slightly more complex API but provides greater customization options.
Choose pb for quick, simple progress bars, and mpb for more complex or customized progress tracking needs in Go applications.
A go library to render progress bars in terminal applications
Pros of uiprogress
- Simpler API, easier to get started for basic use cases
- Supports multiple progress bars out of the box
- Includes a spinner feature for indeterminate progress
Cons of uiprogress
- Less customizable than mpb
- Fewer advanced features (e.g., no dynamic total updates)
- Not actively maintained (last commit in 2019)
Code Comparison
uiprogress:
bar := uiprogress.AddBar(100)
for i := 0; i <= 100; i++ {
bar.Incr()
time.Sleep(time.Millisecond * 20)
}
mpb:
p := mpb.New()
bar := p.AddBar(100)
for i := 0; i < 100; i++ {
bar.Increment()
time.Sleep(time.Millisecond * 20)
}
p.Wait()
Both libraries offer straightforward ways to create and update progress bars. uiprogress has a slightly simpler API, while mpb provides more flexibility and control over the progress bar's behavior and appearance.
mpb offers additional features like dynamic total updates, custom decorators, and better support for concurrent operations, making it more suitable for complex use cases. However, uiprogress might be preferable for simpler projects due to its ease of use and built-in support for multiple progress bars.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Multi Progress Bar
mpb is a Go lib for rendering progress bars in terminal applications.
Features
- Multiple Bars: Multiple progress bars are supported
- Dynamic Total: Set total while bar is running
- Dynamic Add/Remove: Dynamically add or remove bars
- Cancellation: Cancel whole rendering process
- Predefined Decorators: Elapsed time, ewma based ETA, Percentage, Bytes counter
- Decorator's width sync: Synchronized decorator's width among multiple bars
Usage
Rendering single bar
package main
import (
"math/rand"
"time"
"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
)
func main() {
// initialize progress container, with custom width
p := mpb.New(mpb.WithWidth(64))
total := 100
name := "Single Bar:"
// create a single bar, which will inherit container's width
bar := p.New(int64(total),
// BarFillerBuilder with custom style
mpb.BarStyle().Lbound("â¢").Filler("â").Tip("â").Padding("â").Rbound("â"),
mpb.PrependDecorators(
// display our name with one space on the right
decor.Name(name, decor.WC{C: decor.DindentRight | decor.DextraSpace}),
// replace ETA decorator with "done" message, OnComplete event
decor.OnComplete(decor.AverageETA(decor.ET_STYLE_GO), "done"),
),
mpb.AppendDecorators(decor.Percentage()),
)
// simulating some work
max := 100 * time.Millisecond
for i := 0; i < total; i++ {
time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
bar.Increment()
}
// wait for our bar to complete and flush
p.Wait()
}
Rendering multiple bars
var wg sync.WaitGroup
// passed wg will be accounted at p.Wait() call
p := mpb.New(mpb.WithWaitGroup(&wg))
total, numBars := 100, 3
wg.Add(numBars)
for i := 0; i < numBars; i++ {
name := fmt.Sprintf("Bar#%d:", i)
bar := p.AddBar(int64(total),
mpb.PrependDecorators(
// simple name decorator
decor.Name(name),
// decor.DSyncWidth bit enables column width synchronization
decor.Percentage(decor.WCSyncSpace),
),
mpb.AppendDecorators(
// replace ETA decorator with "done" message, OnComplete event
decor.OnComplete(
// ETA decorator with ewma age of 30
decor.EwmaETA(decor.ET_STYLE_GO, 30, decor.WCSyncWidth), "done",
),
),
)
// simulating some work
go func() {
defer wg.Done()
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
max := 100 * time.Millisecond
for i := 0; i < total; i++ {
// start variable is solely for EWMA calculation
// EWMA's unit of measure is an iteration's duration
start := time.Now()
time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
// we need to call EwmaIncrement to fulfill ewma decorator's contract
bar.EwmaIncrement(time.Since(start))
}
}()
}
// wait for passed wg and for all bars to complete and flush
p.Wait()
Dynamic total
Complex example
Bytes counters
Top Related Projects
A really basic thread-safe progress bar for Golang applications
Console progress bar for Golang
A go library to render progress bars in terminal applications
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot