progressbar

package module
v2.5.1 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2018 License: MIT Imports: 9 Imported by: 66

README

progressbar

travis 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.

Example of progress bar

Install

go get -u github.com/schollz/progressbar

Usage

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

which looks like:

 100% |████████████████████████████████████████| [1s:0s]

The times at the end show the elapsed time and the remaining time, respectively.

Long running processes

For long running processes, you might want to render from a 0% state.

// Renders the bar right on construction
bar := progress.NewOptions(100, OptionSetRenderBlankState(true))

Alternatively, when you want to delay rendering, but still want to render a 0% state

bar := progress.NewOptions(100)

// Render the current state, which is 0% in this case
bar.RenderBlank()

// Emulate work
for i := 0; i < 10; i++ {
    time.Sleep(10 * time.Minute)
    bar.Add(10)
}
Use a custom writer

The default writer is standard output (os.Stdout), but you can set it to whatever satisfies io.Writer.

bar := NewOptions(
    10,
    OptionSetTheme(Theme{Saucer: "#", SaucerPadding: "-", BarStart: ">", BarEnd: "<"}),
    OptionSetWidth(10),
    OptionSetWriter(&buf),
)

bar.Add(5)
result := strings.TrimSpace(buf.String())

// Result equals:
// 50% >#####-----< [0s:0s]

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!

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(p *ProgressBar)

Option is the type all options need to adhere to

func OptionEnableColorCodes added in v2.3.0

func OptionEnableColorCodes(colorCodes bool) Option

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

func OptionSetBytes added in v2.4.0

func OptionSetBytes(maxBytes int) Option

OptionSetBytes will also print the bytes/second

func OptionSetDescription added in v2.2.0

func OptionSetDescription(description string) Option

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

func OptionSetRenderBlankState

func OptionSetRenderBlankState(r bool) Option

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

func OptionSetTheme

func OptionSetTheme(t Theme) Option

OptionSetTheme sets the elements the bar is constructed of

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 OptionShowIts added in v2.5.0

func OptionShowIts() Option

OptionShowIts will also print the iterations/second

type ProgressBar

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

ProgressBar is a thread-safe, simple progress bar

Example
bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false))
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(10)
Output:

10% |█         |  [1s:9s]

func New

func New(max int) *ProgressBar

New 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 (*ProgressBar) Add

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

Add with increase the current count on the progress bar

func (*ProgressBar) Clear added in v2.3.0

func (p *ProgressBar) Clear() error

Clear erases the progress bar from the current line

func (*ProgressBar) Finish added in v2.4.0

func (p *ProgressBar) Finish() error

Finish will fill the bar to full

func (*ProgressBar) RenderBlank

func (p *ProgressBar) RenderBlank() error

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

Example
NewOptions(10, OptionSetWidth(10), OptionSetRenderBlankState(true))
Output:

0% |          |  [0s:0s]

func (*ProgressBar) Reset

func (p *ProgressBar) Reset()

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

type Theme

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

Theme defines the elements of the bar

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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