Codebase list golang-github-vbauerster-mpb / 7538e5c
Better InProgress() Vladimir Bauer 9 years ago
1 changed file(s) with 27 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
2020
2121 // Bar represents a progress Bar
2222 type Bar struct {
23 stateCh chan state
24 incrCh chan incrReq
25 flushedCh chan struct{}
26 completedCh chan struct{}
27 removeReqCh chan struct{}
28 done chan struct{}
29 cancel <-chan struct{}
23 stateCh chan state
24 incrCh chan incrReq
25 flushedCh chan struct{}
26 completeReqCh chan struct{}
27 removeReqCh chan struct{}
28 done chan struct{}
29 completed chan struct{}
30 cancel <-chan struct{}
3031
3132 // follawing are used after (*Bar.done) is closed
3233 width int
8283
8384 func newBar(id int, total int64, wg *sync.WaitGroup, conf *userConf) *Bar {
8485 b := &Bar{
85 width: conf.width,
86 stateCh: make(chan state),
87 incrCh: make(chan incrReq),
88 flushedCh: make(chan struct{}),
89 removeReqCh: make(chan struct{}),
90 completedCh: make(chan struct{}),
91 done: make(chan struct{}),
92 cancel: conf.cancel,
86 width: conf.width,
87 stateCh: make(chan state),
88 incrCh: make(chan incrReq),
89 flushedCh: make(chan struct{}),
90 removeReqCh: make(chan struct{}),
91 completeReqCh: make(chan struct{}),
92 done: make(chan struct{}),
93 completed: make(chan struct{}),
94 cancel: conf.cancel,
9395 }
9496
9597 s := state{
241243 return b.getState().id
242244 }
243245
244 // InProgress returns true, while progress is running
246 // InProgress returns true, while progress is running.
245247 // Can be used as condition in for loop
246248 func (b *Bar) InProgress() bool {
247 return !isClosed(b.done)
249 select {
250 case <-b.completed:
251 return false
252 default:
253 return true
254 }
248255 }
249256
250257 // Completed signals to the bar, that process has been completed.
252259 // of process completion.
253260 func (b *Bar) Completed() {
254261 select {
255 case b.completedCh <- struct{}{}:
262 case b.completeReqCh <- struct{}{}:
256263 case <-b.done:
257264 return
258265 }
321328 s.updateTimePerItemEstimate(incrStartTime, r.amount)
322329 if n == s.total {
323330 s.completed = true
331 close(b.completed)
324332 }
325333 s.current = n
326334 if r.refill != nil {
332340 if s.completed {
333341 return
334342 }
335 case <-b.completedCh:
343 case <-b.completeReqCh:
336344 s.completed = true
337345 return
338346 case <-b.removeReqCh: