Documentation
¶
Index ¶
- Variables
- func Bar(current, total int64, barWidth int) string
- func DefaultFuncMap() template.FuncMap
- func Format(n int64, t string) string
- func Left(current, total int64, speed float64) time.Duration
- func Percent(current, total int64) float64
- func Speed(speed float64, t string) string
- type Frame
- type ProgressBar
- func (pb *ProgressBar[T]) Add(n T)
- func (pb *ProgressBar[T]) Additional(s string)
- func (pb *ProgressBar[T]) Cancel()
- func (pb *ProgressBar[T]) Current() int64
- func (pb *ProgressBar[T]) Elapsed() time.Duration
- func (pb *ProgressBar[T]) FromReader(r io.Reader, w io.Writer) (int64, error)
- func (pb *ProgressBar[T]) Message(msg string) error
- func (pb *ProgressBar[T]) SetRefreshInterval(interval time.Duration) *ProgressBar[T]
- func (pb *ProgressBar[T]) SetRender(fn func(w io.Writer, f Frame)) error
- func (pb *ProgressBar[T]) SetRenderInterval(interval time.Duration) *ProgressBar[T]
- func (pb *ProgressBar[T]) SetTemplate(t *template.Template) error
- func (pb *ProgressBar[T]) SetUnit(unit string) *ProgressBar[T]
- func (pb *ProgressBar[T]) SetWidth(barWidth int) *ProgressBar[T]
- func (pb *ProgressBar[T]) Speed() float64
- func (pb *ProgressBar[T]) Start() error
- func (pb *ProgressBar[T]) Total() int64
- func (pb *ProgressBar[T]) Wait()
Constants ¶
This section is empty.
Variables ¶
var DefaultRenderFunc = func(w io.Writer, f Frame) { winsize := GetWinsize() n := winsize - f.BarWidth - len(f.Additional) switch { case n >= 60: defaultTemplate.ExecuteTemplate(w, "full", f) case n >= 40: defaultTemplate.ExecuteTemplate(w, "standard", f) case n >= 20: defaultTemplate.ExecuteTemplate(w, "lite", f) case n > 5 && len(f.Additional) > 0: defaultTemplate.ExecuteTemplate(w, "mini", f) default: width := winsize - 5 w.Write([]byte("[")) if f.Current == f.Total { w.Write([]byte(strings.Repeat("=", width))) } else { done := int(float64(width) * float64(f.Current) / float64(f.Total)) if done != 0 { w.Write([]byte(strings.Repeat("=", done-1) + ">")) } w.Write([]byte(strings.Repeat(" ", width-done))) } w.Write([]byte("]")) } }
DefaultRenderFunc is the default function used to render the progress bar.
var GetWinsize func() int
Functions ¶
func Bar ¶ added in v1.0.1
Bar generates a textual progress bar representation based on the current progress.
func DefaultFuncMap ¶ added in v1.0.1
DefaultFuncMap returns the default template function map for progress bar rendering.
func Left ¶ added in v1.0.1
Left estimates the remaining time to complete the progress bar based on the current speed.
Types ¶
type Frame ¶ added in v1.0.1
type Frame struct {
Unit string
BarWidth int
Current, Total int64
Speed float64
Additional string
Elapsed time.Duration
}
Frame represents a snapshot of all display-ready fields used to render the progress bar at a given moment. It contains the computed visual components (e.g., completed blocks, percentages, speed, timing information) that are assembled by the renderer into the final output.
type ProgressBar ¶
ProgressBar represents a customizable progress bar for tracking task progress. It supports configurable templates, units, and refresh intervals.
func New ¶
func New[T int | int64](total T) *ProgressBar[T]
New creates a new ProgressBar with the specified total count and default options. It panics if total is less than or equal to zero.
func (*ProgressBar[T]) Add ¶
func (pb *ProgressBar[T]) Add(n T)
Add adds the specified amount to the progress bar.
func (*ProgressBar[T]) Additional ¶
func (pb *ProgressBar[T]) Additional(s string)
Additional adds the specified string to the progress bar.
func (*ProgressBar[T]) Current ¶
func (pb *ProgressBar[T]) Current() int64
Current returns the current count of the progress bar.
func (*ProgressBar[T]) Elapsed ¶
func (pb *ProgressBar[T]) Elapsed() time.Duration
Elapsed returns the elapsed time since the progress bar started. If the progress bar has not started, it returns zero.
func (*ProgressBar[T]) FromReader ¶
FromReader starts the progress bar from a reader.
func (*ProgressBar[T]) Message ¶
func (pb *ProgressBar[T]) Message(msg string) error
Message sets a message to be displayed on the progress bar.
func (*ProgressBar[T]) SetRefreshInterval ¶
func (pb *ProgressBar[T]) SetRefreshInterval(interval time.Duration) *ProgressBar[T]
SetRefreshInterval sets progress bar refresh interval time for check speed. If interval is less than or equal to zero, it logs an error message and does not change the interval.
func (*ProgressBar[T]) SetRender ¶ added in v1.0.1
func (pb *ProgressBar[T]) SetRender(fn func(w io.Writer, f Frame)) error
SetRender sets progress bar render function.
func (*ProgressBar[T]) SetRenderInterval ¶
func (pb *ProgressBar[T]) SetRenderInterval(interval time.Duration) *ProgressBar[T]
SetRenderInterval sets the interval for updating the progress bar display. If interval is less than or equal to zero, it logs an error message and does not change the interval.
func (*ProgressBar[T]) SetTemplate ¶
func (pb *ProgressBar[T]) SetTemplate(t *template.Template) error
SetTemplate sets progress bar template.
func (*ProgressBar[T]) SetUnit ¶
func (pb *ProgressBar[T]) SetUnit(unit string) *ProgressBar[T]
SetUnit sets progress bar unit.
func (*ProgressBar[T]) SetWidth ¶
func (pb *ProgressBar[T]) SetWidth(barWidth int) *ProgressBar[T]
SetWidth sets the progress bar width. If barWidth is less than or equal to zero, it logs an error message and does not change the width.
func (*ProgressBar[T]) Speed ¶
func (pb *ProgressBar[T]) Speed() float64
Speed returns the current speed of the progress bar.
func (*ProgressBar[T]) Start ¶
func (pb *ProgressBar[T]) Start() error
Start starts the progress bar.
func (*ProgressBar[T]) Total ¶
func (pb *ProgressBar[T]) Total() int64
Total returns the total count of the progress bar.
func (*ProgressBar[T]) Wait ¶
func (pb *ProgressBar[T]) Wait()
Wait blocks until the progress bar is finished.