diff --git a/bar.go b/bar.go index ba2d8d2..b2215ea 100644 --- a/bar.go +++ b/bar.go @@ -28,6 +28,11 @@ flushedCh chan struct{} stopCh chan struct{} done chan struct{} +} + +type redrawRequest struct { + width int + respCh chan []byte } // Statistics represents statistics of the progress bar @@ -73,11 +78,13 @@ return b } +// TrimLeftSpace removes space befor LeftEnd charater func (b *Bar) TrimLeftSpace() *Bar { b.trimLeftSpace = true return b } +// TrimRightSpace removes space after RightEnd charater func (b *Bar) TrimRightSpace() *Bar { b.trimRightSpace = true return b @@ -138,7 +145,11 @@ } // Current returns the actual current. +// returns 0 after bar was stopped func (b *Bar) Current() int { + if b.isDone() { + return 0 + } respCh := make(chan int) b.currentReqCh <- respCh return <-respCh @@ -169,11 +180,6 @@ return b } -type redrawRequest struct { - width int - respCh chan []byte -} - func (b *Bar) Bytes(width int) []byte { if width <= 0 { width = b.width @@ -186,13 +192,10 @@ func (b *Bar) server(wg *sync.WaitGroup, total int) { timeStarted := time.Now() blockStartTime := timeStarted - var tpie time.Duration - var timeElapsed time.Duration - var appendFuncs []DecoratorFunc - var prependFuncs []DecoratorFunc - var completed bool + var timePerItem, timeElapsed time.Duration + var appendFuncs, prependFuncs []DecoratorFunc + var completed, wgDoneReported bool var current int - var termWidth int for { select { case i := <-b.incrCh: @@ -200,13 +203,13 @@ if n > total { current = total completed = true - break + break // break out of select } timeElapsed = time.Since(timeStarted) - tpie = calcTimePerItemEstimate(tpie, blockStartTime, b.alpha, i) + timePerItem = calcTimePerItemEstimate(timePerItem, blockStartTime, b.alpha, i) blockStartTime = time.Now() current = n - if current == total && !completed { + if current == total { completed = true } case d := <-b.decoratorCh: @@ -219,21 +222,21 @@ case respCh := <-b.currentReqCh: respCh <- current case r := <-b.redrawReqCh: - termWidth = r.width - stat := &Statistics{total, current, termWidth, timeElapsed, tpie} + stat := &Statistics{total, current, r.width, timeElapsed, timePerItem} r.respCh <- b.draw(stat, appendFuncs, prependFuncs) case respCh := <-b.statusReqCh: respCh <- percentage(total, current, 100) case <-b.flushedCh: - if completed && !b.isDone() { - close(b.done) + if completed && !wgDoneReported { + wgDoneReported = true wg.Done() } case <-b.stopCh: + if !wgDoneReported { + wg.Done() + } close(b.done) - if !completed { - wg.Done() - } + return } } } diff --git a/progress.go b/progress.go index 0a909a8..9974e88 100644 --- a/progress.go +++ b/progress.go @@ -163,6 +163,9 @@ case op, ok := <-p.op: if !ok { t.Stop() + for _, b := range bars { + b.Stop() + } return } switch op.kind {