Categorygithub.com/schollz/progressbar/v3
modulepackage
3.18.0
Repository: https://github.com/schollz/progressbar.git
Documentation: pkg.go.dev

# README

progressbar

CI go report card coverage godocs

A very simple thread-safe progress bar which should work on every OS without problems. I needed a progressbar for croc and everything I tried had problems, so I made another one. In order to be OS agnostic I do not plan to support multi-line outputs.

Install

go get -u github.com/schollz/progressbar/v3

Usage

Basic usage

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

which looks like:

Example of basic bar

I/O operations

The progressbar implements an io.Writer so it can automatically detect the number of bytes written to a stream, so you can use it as a progressbar for an io.Reader.

req, _ := http.NewRequest("GET", "https://dl.google.com/go/go1.14.2.src.tar.gz", nil)
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()

f, _ := os.OpenFile("go1.14.2.src.tar.gz", os.O_CREATE|os.O_WRONLY, 0644)
defer f.Close()

bar := progressbar.DefaultBytes(
    resp.ContentLength,
    "downloading",
)
io.Copy(io.MultiWriter(f, bar), resp.Body)

which looks like:

Example of download bar

Progress bar with unknown length

A progressbar with unknown length is a spinner. Any bar with -1 length will automatically convert it to a spinner with a customizable spinner type. For example, the above code can be run and set the resp.ContentLength to -1.

which looks like:

Example of download bar with unknown length

Customization

There is a lot of customization that you can do - change the writer, the color, the width, description, theme, etc. See all the options.

bar := progressbar.NewOptions(1000,
    progressbar.OptionSetWriter(ansi.NewAnsiStdout()), //you should install "github.com/k0kubun/go-ansi"
    progressbar.OptionEnableColorCodes(true),
    progressbar.OptionShowBytes(true),
    progressbar.OptionSetWidth(15),
    progressbar.OptionSetDescription("[cyan][1/3][reset] Writing moshable file..."),
    progressbar.OptionSetTheme(progressbar.Theme{
        Saucer:        "[green]=[reset]",
        SaucerHead:    "[green]>[reset]",
        SaucerPadding: " ",
        BarStart:      "[",
        BarEnd:        "]",
    }))
for i := 0; i < 1000; i++ {
    bar.Add(1)
    time.Sleep(5 * time.Millisecond)
}

which looks like:

Example of customized bar

Contributing

Pull requests are welcome. Feel free to...

  • Revise documentation
  • Add new features
  • Fix bugs
  • Suggest improvements

Thanks

Thanks @Dynom for massive improvements in version 2.0!

Thanks @CrushedPixel for adding descriptions and color code support!

Thanks @MrMe42 for adding some minor features!

Thanks @tehstun for some great PRs!

Thanks @Benzammour and @haseth for helping create v3!

Thanks @briandowns for compiling the list of spinners.

License

MIT

# Packages

# Functions

Default provides a progressbar with recommended defaults.
DefaultBytes provides a progressbar to measure byte throughput with recommended defaults.
DefaultBytesSilent is the same as DefaultBytes, but does not output anywhere.
DefaultSilent is the same as Default, but does not output anywhere.
New returns a new ProgressBar with the specified maximum.
New64 returns a new ProgressBar with the specified maximum.
NewOptions constructs a new instance of ProgressBar, with any options you specify.
NewOptions64 constructs a new instance of ProgressBar, with any options you specify.
NewReader return a new Reader with a given progress bar.
OptionClearOnFinish will clear the bar once its finished.
OptionEnableColorCodes enables or disables support for color codes using mitchellh/colorstring.
OptionFullWidth sets the bar to be full width.
OptionOnCompletion will invoke cmpl function once its finished.
OptionSetDescription sets the description of the bar to render in front of it.
OptionSetElapsedTime will enable elapsed time.
OptionSetItsString sets what's displayed for iterations a second.
OptionSetMaxDetailRow sets the max row of details the row count should be less than the terminal height, otherwise it will not give you the output you want.
OptionSetPredictTime will also attempt to predict the time remaining.
OptionSetRenderBlankState sets whether or not to render a 0% bar on construction.
OptionSetSpinnerChangeInterval sets the spinner change interval the spinner will change according to this value.
OptionSetTheme sets the elements the bar is constructed with.
OptionSetVisibility sets the visibility.
OptionSetWidth sets the width of the bar.
OptionSetWriter sets the output writer (defaults to os.StdOut).
OptionShowBytes will update the progress bar configuration settings to display/hide kBytes/Sec.
OptionShowCount will also print current count out of total.
OptionShowDescriptionAtLineEnd defines whether description should be written at line end instead of line start.
OptionShowElapsedTimeOnFinish will keep the display of elapsed time on finish.
OptionShowIts will also print the iterations/second.
OptionShowTotalBytes will keep the display of total bytes.
OptionSpinnerCustom sets the spinner used for indeterminate bars to the passed slice of string.
OptionSpinnerType sets the type of spinner used for indeterminate bars.
OptionThrottle will wait the specified duration before updating again.
OptionUseANSICodes will use more optimized terminal i/o.
OptionUseIECUnits will enable IEC units (e.g.

# Variables

ThemeASCII is a predefined Theme that uses ASCII symbols.
ThemeDefault is given by default (if not changed with OptionSetTheme), and it looks like "|████ |".
ThemeUnicode is a predefined Theme that uses Unicode characters, displaying a graphic bar.

# Structs

ProgressBar is a thread-safe, simple progress bar.
Reader is the progressbar io.Reader struct.
State is the basic properties of the bar.
Theme defines the elements of the bar.

# Type aliases

Option is the type all options need to adhere to.