diff --git a/bar.go b/bar.go index dd244ff..d0a142a 100644 --- a/bar.go +++ b/bar.go @@ -111,9 +111,7 @@ // RemoveAllPrependers removes all prepend functions func (b *Bar) RemoveAllPrependers() { select { - case b.operateState <- func(s *bState) { - s.pDecorators = nil - }: + case b.operateState <- func(s *bState) { s.pDecorators = nil }: case <-b.done: } } @@ -121,9 +119,7 @@ // RemoveAllAppenders removes all append functions func (b *Bar) RemoveAllAppenders() { select { - case b.operateState <- func(s *bState) { - s.aDecorators = nil - }: + case b.operateState <- func(s *bState) { s.aDecorators = nil }: case <-b.done: } } @@ -184,9 +180,7 @@ return } select { - case b.operateState <- func(s *bState) { - s.refill = &refill{r, till} - }: + case b.operateState <- func(s *bState) { s.refill = &refill{r, till} }: case <-b.done: } } diff --git a/progress.go b/progress.go index e7ab8c6..5340dc0 100644 --- a/progress.go +++ b/progress.go @@ -30,6 +30,7 @@ pState struct { bHeap *priorityQueue heapUpdated bool + zeroWait bool idCounter int width int format string @@ -108,9 +109,7 @@ // Zero is highest priority, i.e. bar will be on top. func (p *Progress) UpdateBarPriority(b *Bar, priority int) { select { - case p.operateState <- func(s *pState) { - s.bHeap.update(b, priority) - }: + case p.operateState <- func(s *pState) { s.bHeap.update(b, priority) }: case <-p.done: } } @@ -119,9 +118,7 @@ func (p *Progress) BarCount() int { result := make(chan int, 1) select { - case p.operateState <- func(s *pState) { - result <- s.bHeap.Len() - }: + case p.operateState <- func(s *pState) { result <- s.bHeap.Len() }: return <-result case <-p.done: return 0 @@ -132,6 +129,13 @@ // It's optional to call, in other words if you don't call Progress.Wait(), // it's not guaranteed that all bars will be flushed completely to the underlying io.Writer. func (p *Progress) Wait() { + if p.BarCount() == 0 { + select { + case p.operateState <- func(s *pState) { s.zeroWait = true }: + case <-p.done: + } + return + } <-p.done } diff --git a/progress_posix.go b/progress_posix.go index 97c829f..7f44b59 100644 --- a/progress_posix.go +++ b/progress_posix.go @@ -28,6 +28,10 @@ op(s) case <-s.ticker.C: if s.bHeap.Len() == 0 { + if s.zeroWait { + close(p.done) + return + } runtime.Gosched() break } diff --git a/progress_windows.go b/progress_windows.go index 7cfa269..2299208 100644 --- a/progress_windows.go +++ b/progress_windows.go @@ -18,6 +18,10 @@ op(s) case <-s.ticker.C: if s.bHeap.Len() == 0 { + if s.zeroWait { + close(p.done) + return + } runtime.Gosched() break }