Documentation ¶
Overview ¶
Package progressbar implements a basic asynchronous and thread-safe progress bar that can perform polling updates.
Create a new progress bar by calling the New function and pass it a callback function that will be called every time the progress bar updates. This callback can update the progress bar's current count and other variables as needed.
The progress bar can be customized with different options at creation.
Index ¶
- Variables
- type Color
- type MovingRate
- type Option
- type ProgressBar
- func (p *ProgressBar) Current() int64
- func (p *ProgressBar) ETA() (hours, minutes, seconds int)
- func (p *ProgressBar) Get(key interface{}) interface{}
- func (p *ProgressBar) Increment(val int64)
- func (p *ProgressBar) Rate() float64
- func (p *ProgressBar) Render() string
- func (p *ProgressBar) Set(key interface{}, value interface{})
- func (p *ProgressBar) SetCurrent(current int64)
- func (p *ProgressBar) Start()
- func (p *ProgressBar) Stop()
- func (p *ProgressBar) Time() (hours, minutes, seconds int)
- func (p *ProgressBar) Total() int64
- type Style
- type Update
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotStarted is an error happening when the MovingRate hasn't been started. ErrNotStarted = errors.New("rate is not started") // ErrAlreadyStarted is an error happening when the MovingRate has already been started. ErrAlreadyStarted = errors.New("rate is already started") // ErrStopped is an error happening when the MovingRate has been stopped. ErrStopped = errors.New("rate has been stopped") // ErrAlreadyStopped is an error happening when the MovingRate has already been stopped. ErrAlreadyStopped = errors.New("rate is already stopped") )
Functions ¶
This section is empty.
Types ¶
type Color ¶
type Color string
Color is a terminal escape string that defines a color.
const ( // ColorBlack is the black foreground color. ColorBlack Color = "\033[0;30m" // ColorGray is the gray foreground color. ColorGray Color = "\033[1;30m" // ColorRed is the red foreground color. ColorRed Color = "\033[0;31m" // ColorBrightRed is the bright red foreground color. ColorBrightRed Color = "\033[1;31m" // ColorGreen is the green foreground color. ColorGreen Color = "\033[0;32m" // ColorBrightGreen is the bright green foreground color. ColorBrightGreen Color = "\033[1;32m" // ColorYellow is the yellow foreground color. ColorYellow Color = "\033[0;33m" // ColorBrightYellow is the yellow foreground color. ColorBrightYellow Color = "\033[1;33m" // ColorBlue is the blue foreground color. ColorBlue Color = "\033[0;34m" // ColorBrightBlue is the bright blue foreground color. ColorBrightBlue Color = "\033[1;34m" // ColorMagenta is the magenta foreground color. ColorMagenta Color = "\033[0;35m" // ColorBrightMagenta is the bright magenta foreground color. ColorBrightMagenta Color = "\033[1;35m" // ColorCyan is the cyan foreground color. ColorCyan Color = "\033[0;36m" // ColorBrightCyan is the bright cyan foreground color. ColorBrightCyan Color = "\033[1;36m" // ColorWhite is the white foreground color. ColorWhite Color = "\033[0;37m" // ColorBrightWhite is the bright white foreground color. ColorBrightWhite Color = "\033[1;37m" // ColorReset is the code to reset all attributes. ColorReset Color = "\033[0m" )
type MovingRate ¶
type MovingRate struct {
// contains filtered or unexported fields
}
MovingRate calculates the rate of elements sampled using a moving average.
func NewMovingRate ¶
func NewMovingRate(samplingRate time.Duration, samples int) *MovingRate
NewMovingRate creates a new MovingRate object with the specified sampling rate and number of samples to consider in the moving average.
func (*MovingRate) Current ¶
func (r *MovingRate) Current() (float64, error)
Current returns the current rate based on the moving average. If the MovingRate object has been stopped, return the global rate instead.
func (*MovingRate) Sample ¶
func (r *MovingRate) Sample(count float64) error
Sample records new data in the moving average. If there is not enough time elapsed between the previous call of Sample, the data is accumulated into a buffer until a proper rate can be calculated.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures a progress bar.
func WithInterval ¶
WithInterval provides an update interval for the progress bar.
func WithTemplate ¶
WithTemplate provides a template string to the progress bar.
func WithWriter ¶
WithWriter provides a custom writer to the progress bar.
type ProgressBar ¶
type ProgressBar struct {
// contains filtered or unexported fields
}
ProgressBar is an asynchronous progress bar that can perform polling updates.
func New ¶
func New(update Update, total int64, opts ...Option) *ProgressBar
New creates a new progress bar from a template string and update function.
func (*ProgressBar) Current ¶
func (p *ProgressBar) Current() int64
Current returns the current progress bar value.
func (*ProgressBar) ETA ¶
func (p *ProgressBar) ETA() (hours, minutes, seconds int)
ETA returns the current estimated time before the task finishes.
func (*ProgressBar) Get ¶
func (p *ProgressBar) Get(key interface{}) interface{}
Get returns a key value if it exists, otherwise it returns nil.
func (*ProgressBar) Increment ¶
func (p *ProgressBar) Increment(val int64)
Increment increments the progress bar by the specified value.
func (*ProgressBar) Render ¶
func (p *ProgressBar) Render() string
Render returns a string containing the progress bar.
func (*ProgressBar) Set ¶
func (p *ProgressBar) Set(key interface{}, value interface{})
Set sets or updates a key value pair on the progress bar.
func (*ProgressBar) SetCurrent ¶
func (p *ProgressBar) SetCurrent(current int64)
SetCurrent sets the current progress bar value.
func (*ProgressBar) Stop ¶
func (p *ProgressBar) Stop()
Stop signals the progress bar to perform one last update and waits for it to finish.
func (*ProgressBar) Time ¶
func (p *ProgressBar) Time() (hours, minutes, seconds int)
Time returns the time since the progress bar has been started until either it is stopped, or the current counter reaches the total.
func (*ProgressBar) Total ¶
func (p *ProgressBar) Total() int64
Total returns the progress bar total value.
type Style ¶
type Style struct { BarPrefix rune BarSuffix rune BarFull rune BarEmpty rune BarPrefixColor Color BarSuffixColor Color BarFullColor Color BarEmptyColor Color }
Style defines a progress bar style.
func DefaultStyle ¶
func DefaultStyle() Style
DefaultStyle create a Style object with the default styling.
type Update ¶
type Update func(bar *ProgressBar)
Update is a function that updates the progress bar.