summaryrefslogtreecommitdiffstats
path: root/progress.go
diff options
context:
space:
mode:
Diffstat (limited to 'progress.go')
-rw-r--r--progress.go38
1 files changed, 26 insertions, 12 deletions
diff --git a/progress.go b/progress.go
index 4c2414e..da7cef0 100644
--- a/progress.go
+++ b/progress.go
@@ -1,3 +1,21 @@
+// Package progress implements a simple command line progress bar.
+//
+// Example:
+//
+// import (
+// time"
+//
+// "gitlab.com/rhogenson/progress-bar/progress"
+// )
+//
+// func main() {
+// b := new(progress.Bar)
+// for i := range 100 {
+// b.Set(float64(i)/100)
+// b.Print()
+// time.Sleep(time.Second)
+// }
+// }
package progress
import (
@@ -14,28 +32,24 @@ const measurements = 20
type measurement struct {
t time.Time
- i int
+ i float64
}
+// Bar is a progess bar. The zero value is ready for use.
type Bar struct {
- max int
measurements vecdeque.DQ[measurement]
cols int
}
-func New(max int) *Bar {
- b := &Bar{max: max}
- b.measurements.Grow(measurements)
- return b
-}
-
-func (b *Bar) Set(i int) {
+// Set sets the current value to val. val must be between 0 and 1, inclusive.
+func (b *Bar) Set(val float64) {
if b.measurements.Len() == measurements {
b.measurements.PopFront()
}
- b.measurements.PushBack(measurement{time.Now(), i})
+ b.measurements.PushBack(measurement{time.Now(), val})
}
+// Print shows the progress bar on standard error.
func (b *Bar) Print() {
first := b.measurements.Get(0)
last := b.measurements.Get(b.measurements.Len() - 1)
@@ -43,10 +57,10 @@ func (b *Bar) Print() {
delta := last.i - first.i
eta := time.Duration(-1)
if delta != 0 {
- eta = time.Duration(float64(b.max-last.i) / float64(delta) * float64(deltaT))
+ eta = time.Duration(float64(deltaT) * (1 - last.i) / delta)
}
- p := last.i * b.cols / b.max
+ p := int(last.i * float64(b.cols))
if b.cols == 0 {
var err error
if b.cols, _, err = term.GetSize(int(os.Stderr.Fd())); err != nil {