Codebase list golang-github-vbauerster-mpb / 2f49b6a
start server in New call Vladimir Bauer 9 years ago
1 changed file(s) with 16 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
2626 // Bars []*Bar
2727
2828 // RefreshInterval in the time duration to wait for refreshing the output
29 RefreshInterval time.Duration
29 // RefreshInterval time.Duration
3030
31 lw *uilive.Writer
32 stopChan chan struct{}
31 lw *uilive.Writer
32 // stopChan chan struct{}
3333 // mtx *sync.RWMutex
3434 bars chan Bar
3535 ticker *time.Ticker
3737
3838 // New returns a new progress bar with defaults
3939 func New() *Progress {
40 return &Progress{
41 Width: Width,
42 Out: Out,
43 RefreshInterval: RefreshInterval,
40 p := &Progress{
41 Width: Width,
42 Out: Out,
43 // RefreshInterval: RefreshInterval,
4444
45 lw: uilive.New(),
46 stopChan: make(chan struct{}),
47 bars: make(chan Bar),
45 lw: uilive.New(),
46 // stopChan: make(chan struct{}),
47 bars: make(chan Bar),
48 ticker: time.NewTicker(RefreshInterval),
4849 }
50 go p.server()
51 return p
4952 }
5053
5154 // Listen listens for updates and renders the progress bars
52 func (p *Progress) Listen() {
55 func (p *Progress) server() {
5356 bars := make([]Bar, 0)
5457 p.lw.Out = p.Out
5558 loop:
5659 for {
5760 select {
58 case <-p.stopChan: // interrupt
59 return
6061 case bar, ok := <-p.bars:
6162 if !ok {
6263 break loop
6364 }
6465 bars = append(bars, bar)
65 default:
66 time.Sleep(p.RefreshInterval)
67 p.mtx.RLock()
68 for _, bar := range p.Bars {
66 case <-p.ticker.C:
67 for _, bar := range bars {
6968 fmt.Fprintln(p.lw, bar.String())
7069 }
7170 p.lw.Flush()
72 p.mtx.RUnlock()
7371 }
7472 }
7573 }