diff --git a/bar.go b/bar.go index 11ff51c..82a76eb 100644 --- a/bar.go +++ b/bar.go @@ -2,7 +2,6 @@ import ( "fmt" - "os" "sync" "time" ) @@ -80,6 +79,8 @@ stopCh chan struct{} done chan struct{} + + stopped bool } type Statistics struct { @@ -168,9 +169,6 @@ } func (b *Bar) Incr(n int) { - // result := make(chan bool) - // b.incrRequestCh <- &incrRequest{n, result} - // return <-result if !b.IsCompleted() { b.incrCh <- n } @@ -187,8 +185,10 @@ select { case i := <-b.incrCh: n := completed + i + // fmt.Fprintf(os.Stderr, "n = %+v\n", n) if n > b.total { completed = b.total + done = true break } b.updateTimePerItemEstimate(i, blockStartTime) @@ -208,16 +208,15 @@ r.bufCh <- b.draw(buf, completed, appendFuncs, prependFuncs) case <-b.flushedCh: if done && !b.IsCompleted() { - fmt.Fprintln(os.Stderr, "flushedCh: wg.Done") + // fmt.Fprintln(os.Stderr, "flushedCh: wg.Done") close(b.done) wg.Done() } case <-b.stopCh: - fmt.Fprintln(os.Stderr, "received stop signal") + // fmt.Fprintln(os.Stderr, "received stop signal") if !done { - fmt.Fprintln(os.Stderr, "closing done chan: done = false") + // fmt.Fprintln(os.Stderr, "closing done chan: done = false") close(b.done) - done = true wg.Done() } return @@ -226,12 +225,10 @@ } func (b *Bar) Stop() { - b.stopCh <- struct{}{} - // if !b.IsCompleted() { - // fmt.Fprintln(os.Stderr, "sending to stopCh") - // } else { - // fmt.Fprintln(os.Stderr, "Stop: already stopped") - // } + if !b.stopped { + b.stopCh <- struct{}{} + b.stopped = true + } } func (b *Bar) IsCompleted() bool { diff --git a/progress.go b/progress.go index 330c51d..f43ca67 100644 --- a/progress.go +++ b/progress.go @@ -27,14 +27,14 @@ // Width is the width of the progress bars // Width int - // lw *uilive.Writer - op chan *operation // new refresh interval to be send over this channel interval chan time.Duration wg *sync.WaitGroup + + stopped bool } type operation struct { @@ -46,8 +46,7 @@ // New returns a new progress bar with defaults func New() *progress { p := &progress{ - out: os.Stdout, - // lw: uilive.New(), + out: os.Stdout, op: make(chan *operation), interval: make(chan time.Duration), wg: new(sync.WaitGroup), @@ -89,11 +88,14 @@ // return p.lw.Bypass() // } -// Stop stops listening -func (p *progress) Stop() { - fmt.Fprintln(os.Stderr, "p.Stop") - p.wg.Wait() - close(p.op) +// WaitAndStop stops listening +func (p *progress) WaitAndStop() { + if !p.stopped { + // fmt.Fprintln(os.Stderr, "p.WaitAndStop") + p.stopped = true + p.wg.Wait() + close(p.op) + } } // server monitors underlying channels and renders any progress bars @@ -106,7 +108,7 @@ select { case op, ok := <-p.op: if !ok { - fmt.Fprintln(os.Stderr, "Sopping bars") + // fmt.Fprintln(os.Stderr, "Sopping bars") for _, b := range bars { b.Stop() }