mpb

package module
v3.0.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2017 License: BSD-3-Clause Imports: 9 Imported by: 0

README

Multi Progress Bar

GoDoc Build Status Go Report Card cover.run go

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
  • Dynamic Decorator's Width Sync: Sync width among decorator group (available since v2)
  • Predefined Decoratros: Elapsed time, Ewmaest based ETA, Percentage, Bytes counter

Installation

To get the package, execute:

go get gopkg.in/vbauerster/mpb.v3

Usage

Following is the simplest use case:

	p := mpb.New(
		// override default (80) width
		mpb.WithWidth(100),
		// override default "[=>-]" format
		mpb.WithFormat("╢▌▌░╟"),
		// override default 100ms refresh rate
		mpb.WithRefreshRate(120*time.Millisecond),
	)

	total := 100
	name := "Single Bar:"
	// Add a bar. You're not limited to just one bar, add many if you need.
	bar := p.AddBar(int64(total),
		// Prepending decorators
		mpb.PrependDecorators(
			// Name decorator with minWidth and no width sync options
			decor.Name(name, len(name), 0),
			// ETA decorator with minWidth and width sync options DwidthSync|DextraSpace
			decor.ETA(4, decor.DSyncSpace),
		),
		// Appending decorators
		mpb.AppendDecorators(
			// Percentage decorator with minWidth and no width sync options
			decor.Percentage(5, 0),
		),
	)

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

	p.Stop()

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:

	p := mpb.New()
	total := 100
	numBars := 3
	var wg sync.WaitGroup
	wg.Add(numBars)

	for i := 0; i < numBars; i++ {
		name := fmt.Sprintf("Bar#%d:", i)
		bar := p.AddBar(int64(total),
			mpb.PrependDecorators(
				decor.Name(name, len(name), 0),
				decor.Percentage(3, decor.DSyncSpace),
			),
			mpb.AppendDecorators(
				decor.ETA(2, 0),
			),
		)
		go func() {
			defer wg.Done()
			for i := 0; i < total; 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

simple.gif

The source code: examples/simple/main.go

Cancel

cancel.gif

The source code: examples/cancel/main.go

Removing bar

remove.gif

The source code: examples/remove/main.go

Sorting bars by progress

sort.gif

The source code: examples/sort/main.go

Resizing bars on terminal width change

resize.gif

The source code: examples/prependETA/main.go

Multiple io

io-multiple.gif

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

License

BSD 3-Clause

The typeface used in screen shots: Iosevka

Documentation

Overview

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

Example
package main

import (
	"math/rand"
	"time"

	"github.com/vbauerster/mpb"
	"github.com/vbauerster/mpb/decor"
)

func main() {
	p := mpb.New(
		// override default (80) width
		mpb.WithWidth(100),
		// override default "[=>-]" format
		mpb.WithFormat("╢▌▌░╟"),
		// override default 100ms refresh rate
		mpb.WithRefreshRate(120*time.Millisecond),
	)

	total := 100
	name := "Single Bar:"
	// Add a bar. You're not limited to just one bar, add many if you need.
	bar := p.AddBar(int64(total),
		// Prepending decorators
		mpb.PrependDecorators(
			// Name decorator with minWidth and no width sync options
			decor.Name(name, len(name), 0),
			// ETA decorator with minWidth and width sync options DwidthSync|DextraSpace
			decor.ETA(4, decor.DSyncSpace),
		),
		// Appending decorators
		mpb.AppendDecorators(
			// Percentage decorator with minWidth and no width sync options
			decor.Percentage(5, 0),
		),
	)

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

	p.Stop()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bar

type Bar struct {
	// contains filtered or unexported fields
}

Bar represents a progress Bar

func (*Bar) Complete

func (b *Bar) Complete()

Complete signals to the bar, that process has been completed. You should call this method when total is unknown and you've reached the point of process completion. If you don't call this method, it will be called implicitly, upon p.Stop() call.

func (*Bar) Current

func (b *Bar) Current() int64

func (*Bar) ID

func (b *Bar) ID() int

ID returs id of the bar

func (*Bar) InProgress

func (b *Bar) InProgress() bool

InProgress returns true, while progress is running. Can be used as condition in for loop

Example
package main

import (
	"time"

	"github.com/vbauerster/mpb"
	"github.com/vbauerster/mpb/decor"
)

func main() {
	p := mpb.New()
	bar := p.AddBar(100, mpb.AppendDecorators(decor.Percentage(5, 0)))

	for bar.InProgress() {
		time.Sleep(time.Millisecond * 20)
		bar.Incr(1)
	}
}
Output:

func (*Bar) Incr

func (b *Bar) Incr(n int)

Incr increments progress bar

func (*Bar) NumOfAppenders

func (b *Bar) NumOfAppenders() int

func (*Bar) NumOfPrependers

func (b *Bar) NumOfPrependers() int

func (*Bar) ProxyReader

func (b *Bar) ProxyReader(r io.Reader) *Reader

ProxyReader wrapper for io operations, like io.Copy

func (*Bar) RemoveAllAppenders

func (b *Bar) RemoveAllAppenders()

RemoveAllAppenders removes all append functions

func (*Bar) RemoveAllPrependers

func (b *Bar) RemoveAllPrependers()

RemoveAllPrependers removes all prepend functions

func (*Bar) ResumeFill

func (b *Bar) ResumeFill(r rune, till int64)

ResumeFill fills bar with different r rune, from 0 to till amount of progress.

func (*Bar) Total

func (b *Bar) Total() int64

type BarOption

type BarOption func(*state)

func AppendDecorators

func AppendDecorators(appenders ...decor.DecoratorFunc) BarOption

func BarEtaAlpha

func BarEtaAlpha(a float64) BarOption

BarEtaAlpha option is a way to adjust ETA behavior. You can play with it, if you're not satisfied with default behavior. Default value is 0.25.

func BarID

func BarID(id int) BarOption

func BarTrim

func BarTrim() BarOption

func BarTrimLeft

func BarTrimLeft() BarOption

func BarTrimRight

func BarTrimRight() BarOption

func PrependDecorators

func PrependDecorators(prependers ...decor.DecoratorFunc) BarOption

type BeforeRender

type BeforeRender func([]*Bar)

BeforeRender is a func, which gets called before render process

type Progress

type Progress struct {
	// contains filtered or unexported fields
}

Progress represents the container that renders Progress bars

func New

func New(options ...ProgressOption) *Progress

New creates new Progress instance, which orchestrates bars rendering process. Accepts mpb.ProgressOption funcs for customization.

func (*Progress) AddBar

func (p *Progress) AddBar(total int64, options ...BarOption) *Bar

AddBar creates a new progress bar and adds to the container.

func (*Progress) BarCount

func (p *Progress) BarCount() int

BarCount returns bars count

func (*Progress) RemoveBar

func (p *Progress) RemoveBar(b *Bar) bool

RemoveBar removes bar at any time.

func (*Progress) Stop

func (p *Progress) Stop()

Stop shutdowns Progress' goroutine. Should be called only after each bar's work done, i.e. bar has reached its 100 %. It is NOT for cancelation. Use WithContext or WithCancel for cancelation purposes.

type ProgressOption

type ProgressOption func(*pConf)

func Output

func Output(w io.Writer) ProgressOption

Output overrides default output os.Stdout

func WithBeforeRenderFunc

func WithBeforeRenderFunc(f BeforeRender) ProgressOption

WithBeforeRenderFunc provided BeforeRender func, will be called before each render cycle.

func WithCancel

func WithCancel(ch <-chan struct{}) ProgressOption

WithCancel provide your cancel channel, which you plan to close at some point.

func WithContext

func WithContext(ctx context.Context) ProgressOption

func WithFormat

func WithFormat(format string) ProgressOption

WithFormat overrides default bar format "[=>-]"

func WithRefreshRate

func WithRefreshRate(d time.Duration) ProgressOption

WithRefreshRate overrides default 100ms refresh rate

func WithShutdownNotifier

func WithShutdownNotifier(ch chan struct{}) ProgressOption

WithShutdownNotifier provided chanel will be closed, inside p.Stop() call

func WithWidth

func WithWidth(w int) ProgressOption

WithWidth overrides default width 80

type Reader

type Reader struct {
	io.Reader
	// contains filtered or unexported fields
}

Reader is io.Reader wrapper, for proxy read bytes

func (*Reader) Close

func (r *Reader) Close() error

Close the reader when it implements io.Closer

func (*Reader) Read

func (r *Reader) Read(p []byte) (int, error)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL