diff options
Diffstat (limited to 'progress.go')
| -rw-r--r-- | progress.go | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/progress.go b/progress.go index ddc810a..11821c1 100644 --- a/progress.go +++ b/progress.go @@ -9,9 +9,9 @@ // ) // // func main() { -// b := new(progress.Bar) +// b := progress.New(100) // for i := range 100 { -// b.Set(float64(i)/100) +// b.Set(i) // b.Print() // time.Sleep(time.Second) // } @@ -32,20 +32,24 @@ const measurements = 20 type measurement struct { t time.Time - i float64 + i int } -// Bar is a progess bar. The zero value is ready for use. +// Bar is a progess bar. type Bar struct { + max int measurements vecdeque.DQ[measurement] cols int } -// Set sets the current value to val. val must be between 0 and 1, inclusive. -func (b *Bar) Set(val float64) { - if !(0 <= val && val <= 1) { - panic(fmt.Sprintf("progress.Bar.Set: value must be between 0 and 1 (got %f)", val)) - } +func New(max int) *Bar { + b := &Bar{max: max} + b.measurements.Grow(measurements) + return b +} + +// Set sets the current value to val. +func (b *Bar) Set(val int) { if b.measurements.Len() == measurements { b.measurements.PopFront() } @@ -64,10 +68,10 @@ func (b *Bar) Print() { delta := last.i - first.i eta := time.Duration(-1) if delta != 0 { - eta = time.Duration(float64(deltaT) * (1 - last.i) / delta) + eta = time.Duration(float64(b.max-last.i) / float64(delta) * float64(deltaT)) } - p := int(last.i * float64(b.cols)) + p := last.i * b.cols / b.max if b.cols == 0 { var err error if b.cols, _, err = term.GetSize(int(os.Stderr.Fd())); err != nil { |
