diff options
Diffstat (limited to 'vendor/gopkg.in/cheggaaa/pb.v1/pool.go')
-rw-r--r-- | vendor/gopkg.in/cheggaaa/pb.v1/pool.go | 62 |
1 files changed, 45 insertions, 17 deletions
diff --git a/vendor/gopkg.in/cheggaaa/pb.v1/pool.go b/vendor/gopkg.in/cheggaaa/pb.v1/pool.go index 0b4a4afa8..392e7599c 100644 --- a/vendor/gopkg.in/cheggaaa/pb.v1/pool.go +++ b/vendor/gopkg.in/cheggaaa/pb.v1/pool.go @@ -3,6 +3,7 @@ package pb import ( + "io" "sync" "time" ) @@ -11,22 +12,36 @@ import ( // You need call pool.Stop() after work func StartPool(pbs ...*ProgressBar) (pool *Pool, err error) { pool = new(Pool) - if err = pool.start(); err != nil { + if err = pool.Start(); err != nil { return } pool.Add(pbs...) return } +// NewPool initialises a pool with progress bars, but +// doesn't start it. You need to call Start manually +func NewPool(pbs ...*ProgressBar) (pool *Pool) { + pool = new(Pool) + pool.Add(pbs...) + return +} + type Pool struct { - RefreshRate time.Duration - bars []*ProgressBar - quit chan int - finishOnce sync.Once + Output io.Writer + RefreshRate time.Duration + bars []*ProgressBar + lastBarsCount int + shutdownCh chan struct{} + workerCh chan struct{} + m sync.Mutex + finishOnce sync.Once } // Add progress bars. func (p *Pool) Add(pbs ...*ProgressBar) { + p.m.Lock() + defer p.m.Unlock() for _, bar := range pbs { bar.ManualUpdate = true bar.NotPrint = true @@ -35,30 +50,38 @@ func (p *Pool) Add(pbs ...*ProgressBar) { } } -func (p *Pool) start() (err error) { +func (p *Pool) Start() (err error) { p.RefreshRate = DefaultRefreshRate - quit, err := lockEcho() + p.shutdownCh, err = lockEcho() if err != nil { return } - p.quit = make(chan int) - go p.writer(quit) + p.workerCh = make(chan struct{}) + go p.writer() return } -func (p *Pool) writer(finish chan int) { +func (p *Pool) writer() { var first = true + defer func() { + if first == false { + p.print(false) + } else { + p.print(true) + p.print(false) + } + close(p.workerCh) + }() + for { select { case <-time.After(p.RefreshRate): if p.print(first) { p.print(false) - finish <- 1 return } first = false - case <-p.quit: - finish <- 1 + case <-p.shutdownCh: return } } @@ -66,11 +89,16 @@ func (p *Pool) writer(finish chan int) { // Restore terminal state and close pool func (p *Pool) Stop() error { - // Wait until one final refresh has passed. - time.Sleep(p.RefreshRate) - p.finishOnce.Do(func() { - close(p.quit) + if p.shutdownCh != nil { + close(p.shutdownCh) + } }) + + // Wait for the worker to complete + select { + case <-p.workerCh: + } + return unlockEcho() } |