diff --git a/bar.go b/bar.go index 34af054..d0bc857 100644 --- a/bar.go +++ b/bar.go @@ -39,7 +39,6 @@ current int64 refill int64 trimSpace bool - completed bool aborted bool triggerComplete bool rmOnComplete bool @@ -350,7 +349,7 @@ func (b *Bar) Abort(drop bool) { select { case b.operateState <- func(s *bState) { - if s.completed || s.aborted { + if s.aborted || s.completed() { return } s.aborted = true @@ -376,10 +375,10 @@ func (b *Bar) Completed() bool { result := make(chan bool) select { - case b.operateState <- func(s *bState) { result <- s.completed }: + case b.operateState <- func(s *bState) { result <- s.completed() }: return <-result case <-b.bsOk: - return b.bs.completed + return b.bs.completed() } } @@ -404,7 +403,7 @@ case op := <-b.operateState: op(bs) case <-b.ctx.Done(): - bs.aborted = !bs.completed + bs.aborted = !bs.completed() bs.decoratorShutdownNotify(&b.container.bwg) b.bs = bs close(b.bsOk) @@ -431,7 +430,7 @@ if s.extender != nil { frame.rows, frame.err = s.extender(frame.rows, stat) } - if s.completed || s.aborted { + if s.aborted || s.completed() { frame.shutdown = s.shutdown frame.rmOnComplete = s.rmOnComplete frame.noPop = s.noPop @@ -553,7 +552,7 @@ } func (s *bState) triggerCompletion(b *Bar) { - s.completed = s.current == s.total + s.triggerComplete = true if s.autoRefresh { // Technically this call isn't required, but if refresh rate is set to // one hour for example and bar completes within a few minutes p.Wait() @@ -562,6 +561,10 @@ } else { b.cancel() } +} + +func (s bState) completed() bool { + return s.triggerComplete && s.current == s.total } func (s bState) decoratorEwmaUpdate(n int64, dur time.Duration, wg *sync.WaitGroup) { @@ -620,7 +623,7 @@ Total: s.total, Current: s.current, Refill: s.refill, - Completed: s.completed, + Completed: s.completed(), Aborted: s.aborted, } } diff --git a/draw_test.go b/draw_test.go index 956dc80..cf3a79d 100644 --- a/draw_test.go +++ b/draw_test.go @@ -786,7 +786,6 @@ s.current = tc.current s.trimSpace = tc.trim s.refill = tc.refill - s.completed = tc.total > 0 && tc.current >= tc.total r, err := s.draw(newStatistics(tw, s)) if err != nil { t.Fatalf("tw: %d case %q draw error: %s", tw, tc.name, err.Error()) @@ -1239,7 +1238,6 @@ s.current = tc.current s.trimSpace = tc.trim s.refill = tc.refill - s.completed = tc.total > 0 && tc.current >= tc.total r, err := s.draw(newStatistics(tw, s)) if err != nil { t.Fatalf("tw: %d case %q draw error: %s", tw, tc.name, err.Error()) @@ -1415,7 +1413,6 @@ s.current = tc.current s.trimSpace = tc.trim s.refill = tc.refill - s.completed = tc.total > 0 && tc.current >= tc.total r, err := s.draw(newStatistics(tw, s)) if err != nil { t.Fatalf("tw: %d case %q draw error: %s", tw, tc.name, err.Error())