Project Overview: progressbar
The progressbar
is a simple yet effective tool that provides a visual indicator for tracking the progress of various tasks. Created by Zachary Scholl, this progress bar stands out due to its thread-safe design, ensuring smooth operation across all operating systems without compatibility issues. It was originally developed to meet the needs of another project, croc, where other available progress bars fell short. Interestingly, this project avoids multi-line outputs, maintaining simplicity and clarity.
Installation
Getting started with progressbar
is straightforward. Developers can easily integrate it into their Go projects by using the following command:
go get -u github.com/schollz/progressbar/v3
Usage Examples
Basic Usage
The basic implementation of the progressbar
allows developers to track tasks with a defined endpoint. For instance, if a task involves iterating up to 100, the code snippet below demonstrates how the progress bar reflects each step:
bar := progressbar.Default(100)
for i := 0; i < 100; i++ {
bar.Add(1)
time.Sleep(40 * time.Millisecond)
}
This code will produce a visual bar that fills up incrementally, providing a clear representation of task progress.
I/O Operations
The progressbar
can function as an io.Writer
, allowing it to monitor and display the progress of data streams effectively. For example, when downloading a file, the progress bar can show how much of the file has been downloaded:
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)
This utility is particularly useful for longer operations, providing visual feedback during the process.
Handling Unknown Lengths
If the length of the task is unknown, progressbar
can seamlessly transition into a spinner. This flexibility is automatically activated when the length is set to -1
, making it a versatile tool for various scenarios:
// Example with unknown length
resp.ContentLength = -1
Customization Options
Customization is a strong suit of the progressbar
, offering numerous ways to tailor its appearance and functionality. Developers can modify the writer, color, width, description, and even the theme of the bar to suit their project’s aesthetics:
bar := progressbar.NewOptions(1000,
progressbar.OptionSetWriter(ansi.NewAnsiStdout()), // Ensure to 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)
}
This customization ensures that the progress bar aligns with the user’s preferences and the project requirements.
Contributions and Community
progressbar
is an open-source project that welcomes contributions from the community. Developers are encouraged to revise documentation, introduce new features, fix bugs, and propose enhancements. The project has seen valuable contributions from individuals who have helped improve functionality, add features like descriptions and color code support, and enhance overall usability.
Acknowledgements
The progressbar
project extends its gratitude to its contributors who have significantly improved its capabilities across different versions. The collaborative effort from various contributors has been instrumental in its development and success.
Licensing
The project is released under the MIT license, making it free to use, modify, and distribute, provided attribution is given to the original author.