mpb

package module
v3.0.4+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2019 License: BSD-3-Clause Imports: 13 Imported by: 0

README

Multi Progress Bar

This is a fork from github.com/vbauerster/mpb. There are a few differences, but they are mostly cosmetic. The major points of design are:

  • Simple: It should be very easy to create a progressbar.
  • Informative: The progressbar should provide the main point of data well (when will X finish).
  • Pretty: The progressbar should look pretty.

GoDoc Build Status Go Report Card codecov

mpb is a Go lib for rendering progress bars in terminal applications.

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 github.com/james-antill/mpb

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 a single bar, add as many as you need
	bar := p.AddBar(int64(total),
		// Prepending decorators
		mpb.PrependDecorators(
			// StaticName decorator with minWidth and no extra config
			// If you need to change name while rendering, use DynamicName
			decor.StaticName(name, len(name), 0),
			// ETA decorator with minWidth and no extra config
			decor.ETA(4, 0),
		),
		// Appending decorators
		mpb.AppendDecorators(
			// Percentage decorator with minWidth and no extra config
			decor.Percentage(5, 0),
		),
	)

	for i := 0; i < total; i++ {
		time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100)
		bar.Increment()
	}

	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:

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

	for i := 0; i < numBars; i++ {
		name := fmt.Sprintf("Bar#%d:", i)
		bar := p.AddBar(int64(total),
			mpb.PrependDecorators(
				decor.StaticName(name, 0, 0),
				// DSyncSpace is shortcut for DwidthSync|DextraSpace
				// means sync the width of respective decorator's column
				// and prepend one extra space.
				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(10)+1) * time.Second / 100)
				bar.Increment()
			}
		}()
	}
	// Wait for incr loop goroutines to finish,
	// and shutdown mpb's rendering goroutine
	p.Stop()

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/james-antill/mpb"
	"github.com/james-antill/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 a single bar, add as many as you need
	bar := p.AddBar(int64(total),
		// Prepending decorators
		mpb.PrependDecorators(
			// StaticName decorator with minWidth and no extra config
			// If you need to change name while rendering, use DynamicName
			decor.StaticName(name, len(name), 0),
			// ETA decorator with minWidth and no extra config
			decor.ETA(4, 0),
		),
		// Appending decorators
		mpb.AppendDecorators(
			// Percentage decorator with minWidth and no extra config
			decor.Percentage(5, 0),
		),
	)

	for i := 0; i < total; i++ {
		time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100)
		bar.Increment()
	}

	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 (
	"math/rand"
	"time"

	"github.com/james-antill/mpb"
	"github.com/james-antill/mpb/decor"
)

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

	for bar.InProgress() {
		time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100)
		bar.Increment()
	}
}
Output:

func (*Bar) Incr

func (b *Bar) Incr(n int)

Incr increments progress bar

func (*Bar) Increment

func (b *Bar) Increment()

Increment shorthand for b.Incr(1)

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

func (*Bar) Update

func (b *Bar) Update()

Update updates the startTime/timeElapsed for ETA/Nsec

type BarOption

type BarOption func(*state)

BarOption is a function option which changes the default behavior of a bar, if passed to p.AddBar(int64, ...BarOption)

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) AddBarDef

func (p *Progress) AddBarDef(total int64, name string, unit decor.Units,
	options ...BarOption) *Bar

AddBarDef creates a new progress bar with sane default options.

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 is a way to gracefully shutdown mpb's rendering goroutine. It is NOT for cancelation (use mpb.WithContext for cancelation purposes). If *sync.WaitGroup has been provided via mpb.WithWaitGroup(), its Wait() method will be called first.

type ProgressOption

type ProgressOption func(*pConf)

ProgressOption is a function option which changes the default behavior of progress pool, if passed to mpb.New(...ProgressOption)

func Output

func Output(w io.Writer) ProgressOption

Output overrides default output os.Stdout

func OutputInterceptors

func OutputInterceptors(interseptors ...func(io.Writer)) ProgressOption

OutputInterceptors provides a way to write to the underlying progress pool's writer. Could be useful if you want to output something below the bars, while they're rendering.

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 WithWaitGroup

func WithWaitGroup(wg *sync.WaitGroup) ProgressOption

WithWaitGroup provides means to have a single joint point. If *sync.WaitGroup is provided, you can safely call just p.Stop() without calling Wait() on provided *sync.WaitGroup. Makes sense when there are more than one bar to render.

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