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