diff --git a/bar.go b/bar.go index 70100e7..ee34959 100644 --- a/bar.go +++ b/bar.go @@ -65,9 +65,10 @@ dynamic bool noBarOnComplete bool startTime time.Time + blockStartTime time.Time timeElapsed time.Duration - blockStartTime time.Time - timePerItem time.Duration + timePerItemEstimate time.Duration + timeRemaining time.Duration aDecorators []decor.DecoratorFunc pDecorators []decor.DecoratorFunc refill *refill @@ -249,7 +250,7 @@ s.startTime = now s.blockStartTime = now } else { - s.updateTimePerItemEstimate(n, now) + s.updateETA(int64(n), now) s.timeElapsed = now.Sub(s.startTime) } s.current += int64(n) @@ -423,10 +424,11 @@ } } -func (s *bState) updateTimePerItemEstimate(amount int, now time.Time) { +func (s *bState) updateETA(amount int64, now time.Time) { lastBlockTime := now.Sub(s.blockStartTime) lastItemEstimate := float64(lastBlockTime) / float64(amount) - s.timePerItem = time.Duration((s.etaAlpha * lastItemEstimate) + (1-s.etaAlpha)*float64(s.timePerItem)) + s.timePerItemEstimate = time.Duration((s.etaAlpha * lastItemEstimate) + (1-s.etaAlpha)*float64(s.timePerItemEstimate)) + s.timeRemaining = time.Duration(s.total-s.current+amount) * s.timePerItemEstimate s.blockStartTime = now } @@ -438,7 +440,8 @@ Current: s.current, StartTime: s.startTime, TimeElapsed: s.timeElapsed, - TimePerItemEstimate: s.timePerItem, + TimeRemaining: s.timeRemaining, + TimePerItemEstimate: s.timePerItemEstimate, } } diff --git a/decor/decorators.go b/decor/decorators.go index d1f5de0..3bdcf3e 100644 --- a/decor/decorators.go +++ b/decor/decorators.go @@ -38,12 +38,8 @@ Current int64 StartTime time.Time TimeElapsed time.Duration + TimeRemaining time.Duration TimePerItemEstimate time.Duration -} - -// Eta returns exponential-weighted-moving-average ETA estimator -func (s *Statistics) Eta() time.Duration { - return time.Duration(s.Total-s.Current) * s.TimePerItemEstimate } // DecoratorFunc is a function that can be prepended and appended to the progress bar @@ -189,7 +185,7 @@ } format += "%ds" return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string { - str := fmt.Sprint(time.Duration(s.Eta().Seconds()) * time.Second) + str := fmt.Sprint(time.Duration(s.TimeRemaining.Seconds()) * time.Second) if (conf & DwidthSync) != 0 { widthAccumulator <- utf8.RuneCountInString(str) max := <-widthDistributor