Socket
Socket
Sign inDemoInstall

gopkg.in/vbauerster/mpb.v1

Package Overview
Dependencies
0
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    gopkg.in/vbauerster/mpb.v1

Package mpb is a library for rendering progress bars in terminal applications.


Version published

Readme

Source

Multi Progress Bar GoDoc Build Status

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.

Features

  • 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
  • Predefined Decoratros: Elapsed time, Ewmaest based ETA, Percentage, Bytes counter

Usage

Following is the simplest use case:

	// Star mpb's rendering goroutine.
	// If you don't plan to cancel, feed with nil
	// otherwise provide context.Context, see cancel example
	p := mpb.New(nil)
	// Set custom width for every bar, which mpb will contain
	// The default one in 70
	p.SetWidth(80)
	// 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).AppendPercentage()

	for i := 0; i < 100; i++ {
		bar.Incr(1) // increment progress bar
		time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
	}

	// Don't forget to stop mpb's rendering goroutine
	p.Stop()

	// You cannot add bars after p.Stop() has been called
	// p.AddBar(100) // will panic

Running this, will produce:

gif

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(nil)
	for i := 0; i < 3; i++ {
		wg.Add(1) // add wg delta
		name := fmt.Sprintf("Bar#%d:", i)
		bar := p.AddBar(100).PrependName(name, len(name)).AppendPercentage()
		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++ {
				bar.Incr(1)
				time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
			}
		}()
	}
	wg.Wait() // Wait for goroutines to finish
	p.Stop()  // Stop mpb's rendering goroutine
	// p.AddBar(1) // panic: you cannot reuse p, create new one!
	fmt.Println("finish")

simple.gif

The source code: example/simple/main.go

Cancel

cancel.gif

The source code: example/cancel/main.go

Removing bar

remove.gif

The source code: example/remove/main.go

Sorting bars by progress

sort.gif

The source code: example/sort/main.go

Resizing bars on terminal width change

resize.gif

The source code: example/prependETA/main.go

Multiple io

io-multiple.gif

The source code: example/io/multiple/main.go

Custom Decorators

Here is example from getparty src code.

Installation

$ go get -u github.com/vbauerster/mpb

License

MIT

The typeface used in screen shots: Iosevka

FAQs

Last updated on 11 Mar 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc