2018-02-05 16:13:06 -05:00
|
|
|
// +build linux darwin freebsd netbsd openbsd solaris dragonfly windows
|
|
|
|
|
|
|
|
package pb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create and start new pool with given bars
|
|
|
|
// You need call pool.Stop() after work
|
|
|
|
func StartPool(pbs ...*ProgressBar) (pool *Pool, err error) {
|
|
|
|
pool = new(Pool)
|
2018-04-27 02:20:56 -04:00
|
|
|
if err = pool.Start(); err != nil {
|
2018-02-05 16:13:06 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
pool.Add(pbs...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-27 02:20:56 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-02-05 16:13:06 -05:00
|
|
|
type Pool struct {
|
|
|
|
Output io.Writer
|
|
|
|
RefreshRate time.Duration
|
|
|
|
bars []*ProgressBar
|
|
|
|
lastBarsCount int
|
2018-04-27 02:20:56 -04:00
|
|
|
shutdownCh chan struct{}
|
|
|
|
workerCh chan struct{}
|
2018-02-05 16:13:06 -05:00
|
|
|
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
|
|
|
|
bar.Start()
|
|
|
|
p.bars = append(p.bars, bar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 02:20:56 -04:00
|
|
|
func (p *Pool) Start() (err error) {
|
2018-02-05 16:13:06 -05:00
|
|
|
p.RefreshRate = DefaultRefreshRate
|
2018-04-27 02:20:56 -04:00
|
|
|
p.shutdownCh, err = lockEcho()
|
2018-02-05 16:13:06 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-04-27 02:20:56 -04:00
|
|
|
p.workerCh = make(chan struct{})
|
|
|
|
go p.writer()
|
2018-02-05 16:13:06 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-27 02:20:56 -04:00
|
|
|
func (p *Pool) writer() {
|
2018-02-05 16:13:06 -05:00
|
|
|
var first = true
|
2018-04-27 02:20:56 -04:00
|
|
|
defer func() {
|
|
|
|
if first == false {
|
|
|
|
p.print(false)
|
|
|
|
} else {
|
|
|
|
p.print(true)
|
|
|
|
p.print(false)
|
|
|
|
}
|
|
|
|
close(p.workerCh)
|
|
|
|
}()
|
|
|
|
|
2018-02-05 16:13:06 -05:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-time.After(p.RefreshRate):
|
|
|
|
if p.print(first) {
|
|
|
|
p.print(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
first = false
|
2018-04-27 02:20:56 -04:00
|
|
|
case <-p.shutdownCh:
|
2018-02-05 16:13:06 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore terminal state and close pool
|
|
|
|
func (p *Pool) Stop() error {
|
|
|
|
p.finishOnce.Do(func() {
|
2018-10-16 12:18:29 -04:00
|
|
|
if p.shutdownCh != nil {
|
|
|
|
close(p.shutdownCh)
|
|
|
|
}
|
2018-02-05 16:13:06 -05:00
|
|
|
})
|
2018-04-27 02:20:56 -04:00
|
|
|
|
|
|
|
// Wait for the worker to complete
|
|
|
|
select {
|
|
|
|
case <-p.workerCh:
|
|
|
|
}
|
|
|
|
|
2018-02-05 16:13:06 -05:00
|
|
|
return unlockEcho()
|
|
|
|
}
|