Back to godoc.org
github.com/schollz/progressbar/v3

Package progressbar

v3.5.1
Latest Go to latest

The latest major version is v3.

Published: Sep 16, 2020 | License: MIT | Module: github.com/schollz/progressbar/v3

Overview

Example (XOutOfY)

Code:

bar := NewOptions(100, OptionSetPredictTime(true))

for i := 0; i < 100; i++ {
	bar.Add(1)
	time.Sleep(1 * time.Millisecond)
}

Index

Examples

type Option

type Option func(p *ProgressBar)

Option is the type all options need to adhere to

func OptionClearOnFinish

func OptionClearOnFinish() Option

OptionClearOnFinish will clear the bar once its finished

Example

Code:

bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false), OptionClearOnFinish())
bar.Reset()
bar.Finish()
fmt.Println("Finished")
Finished

func OptionEnableColorCodes

func OptionEnableColorCodes(colorCodes bool) Option

OptionEnableColorCodes enables or disables support for color codes using mitchellh/colorstring

func OptionFullWidth

func OptionFullWidth() Option

OptionFullWidth sets the bar to be full width

func OptionOnCompletion

func OptionOnCompletion(cmpl func()) Option

OptionOnCompletion will invoke cmpl function once its finished

func OptionSetDescription

func OptionSetDescription(description string) Option

OptionSetDescription sets the description of the bar to render in front of it

func OptionSetItsString

func OptionSetItsString(iterationString string) Option

OptionSetItsString sets what's displayed for interations a second. The default is "it" which would display: "it/s"

func OptionSetPredictTime

func OptionSetPredictTime(predictTime bool) Option

OptionSetPredictTime will also attempt to predict the time remaining.

Example

Code:

bar := NewOptions(100, OptionSetWidth(10), OptionSetPredictTime(false))
_ = bar.Add(10)
10% |█         |

func OptionSetRenderBlankState

func OptionSetRenderBlankState(r bool) Option

OptionSetRenderBlankState sets whether or not to render a 0% bar on construction

Example

Code:

NewOptions(10, OptionSetWidth(10), OptionSetRenderBlankState(true))
0% |          |  [0s:0s]

func OptionSetTheme

func OptionSetTheme(t Theme) Option

OptionSetTheme sets the elements the bar is constructed of

func OptionSetVisibility

func OptionSetVisibility(visibility bool) Option

OptionSetVisibility sets the visibility

func OptionSetWidth

func OptionSetWidth(s int) Option

OptionSetWidth sets the width of the bar

func OptionSetWriter

func OptionSetWriter(w io.Writer) Option

OptionSetWriter sets the output writer (defaults to os.StdOut)

func OptionShowBytes

func OptionShowBytes(val bool) Option

OptionShowBytes will update the progress bar configuration settings to display/hide kBytes/Sec

func OptionShowCount

func OptionShowCount() Option

OptionShowCount will also print current count out of total

func OptionShowIts

func OptionShowIts() Option

OptionShowIts will also print the iterations/second

Example

Code:

bar := NewOptions(100, OptionSetWidth(10), OptionShowIts(), OptionSetPredictTime(false))
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(10)
10% |█         | (10 it/s)
Example (Count)

Code:

bar := NewOptions(100, OptionSetWidth(10), OptionShowIts(), OptionShowCount())
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(10)
10% |█         | (10/100, 10 it/s) [1s:9s]

func OptionSpinnerType

func OptionSpinnerType(spinnerType int) Option

OptionSpinnerType sets the type of spinner used for indeterminate bars

func OptionThrottle

func OptionThrottle(duration time.Duration) Option

OptionThrottle will wait the specified duration before updating again. The default duration is 0 seconds.

Example

Code:

bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false), OptionThrottle(100*time.Millisecond))
bar.Reset()
bar.Add(5)
time.Sleep(150 * time.Millisecond)
bar.Add(5)
bar.Add(10)
10% |█         |  [0s:1s]

type ProgressBar

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

ProgressBar is a thread-safe, simple progress bar

Example

Code:

bar := New(100)
bar.Add(10)
10% |████                                    |  [0s:0s]
Example (Basic)

Code:

bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false))
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(10)
10% |█         |  [1s:9s]
Example (Invisible)

Code:

bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false), OptionSetVisibility(false))
bar.Reset()
bar.RenderBlank()
fmt.Println("hello, world")
time.Sleep(1 * time.Second)
bar.Add(10)
hello, world

func Default

func Default(max int64, description ...string) *ProgressBar

Default provides a progressbar with recommended defaults. Set max to -1 to use as a spinner.

Example

Code:

bar := Default(100)
for i := 0; i < 50; i++ {
	bar.Add(1)
	time.Sleep(10 * time.Millisecond)
}

func DefaultBytes

func DefaultBytes(maxBytes int64, description ...string) *ProgressBar

DefaultBytes provides a progressbar to measure byte throughput with recommended defaults. Set maxBytes to -1 to use as a spinner.

func New

func New(max int) *ProgressBar

New returns a new ProgressBar with the specified maximum

func New64

func New64(max int64) *ProgressBar

New64 returns a new ProgressBar with the specified maximum

func NewOptions

func NewOptions(max int, options ...Option) *ProgressBar

NewOptions constructs a new instance of ProgressBar, with any options you specify

func NewOptions64

func NewOptions64(max int64, options ...Option) *ProgressBar

NewOptions64 constructs a new instance of ProgressBar, with any options you specify

func (*ProgressBar) Add

func (p *ProgressBar) Add(num int) error

Add will add the specified amount to the progressbar

func (*ProgressBar) Add64

func (p *ProgressBar) Add64(num int64) error

Add64 will add the specified amount to the progressbar

func (*ProgressBar) ChangeMax

func (p *ProgressBar) ChangeMax(newMax int)

ChangeMax takes in a int and changes the max value of the progress bar

func (*ProgressBar) ChangeMax64

func (p *ProgressBar) ChangeMax64(newMax int64)

ChangeMax64 is basically the same as ChangeMax, but takes in a int64 to avoid casting

func (*ProgressBar) Clear

func (p *ProgressBar) Clear() error

Clear erases the progress bar from the current line

func (*ProgressBar) Describe

func (p *ProgressBar) Describe(description string)

Describe will change the description shown before the progress, which can be changed on the fly (as for a slow running process).

Example

Code:

bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false))
bar.Reset()
time.Sleep(1 * time.Second)
bar.Describe("performing axial adjustements")
bar.Add(10)
performing axial adjustements  10% |█         |  [1s:9s]

func (*ProgressBar) Finish

func (p *ProgressBar) Finish() error

Finish will fill the bar to full

Example

Code:

bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false))
bar.Finish()
100% |██████████|  [0s:0s]

func (*ProgressBar) GetMax

func (p *ProgressBar) GetMax() int

GetMax returns the max of a bar

func (*ProgressBar) GetMax64

func (p *ProgressBar) GetMax64() int64

GetMax64 returns the current max

func (*ProgressBar) Read

func (p *ProgressBar) Read(b []byte) (n int, err error)

Read implement io.Reader

func (*ProgressBar) RenderBlank

func (p *ProgressBar) RenderBlank() error

RenderBlank renders the current bar state, you can use this to render a 0% state

func (*ProgressBar) Reset

func (p *ProgressBar) Reset()

Reset will reset the clock that is used to calculate current time and the time left.

func (*ProgressBar) Set

func (p *ProgressBar) Set(num int) error

Set wil set the bar to a current number

Example

Code:

bar := New(100)
bar.Set(10)
10% |████                                    |  [0s:0s]

func (*ProgressBar) Set64

func (p *ProgressBar) Set64(num int64) error

Set64 wil set the bar to a current number

Example

Code:

bar := New(100)
bar.Set64(10)
10% |████                                    |  [0s:0s]

func (*ProgressBar) State

func (p *ProgressBar) State() State

State returns the current state

func (*ProgressBar) Write

func (p *ProgressBar) Write(b []byte) (n int, err error)

Write implement io.Writer

type Reader

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

Reader is the progressbar io.Reader struct

func (*Reader) Close

func (r *Reader) Close() (err error)

Close the reader when it implements io.Closer

func (*Reader) Read

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

Read will read the data and add the number of bytes to the progressbar

type State

type State struct {
	CurrentPercent float64
	CurrentBytes   float64
	SecondsSince   float64
	SecondsLeft    float64
	KBsPerSecond   float64
}

State is the basic properties of the bar

type Theme

type Theme struct {
	Saucer        string
	SaucerHead    string
	SaucerPadding string
	BarStart      string
	BarEnd        string
}

Theme defines the elements of the bar

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier