Codebase list golang-github-vbauerster-mpb / e30c1eb
Don't wait if there are no bars Vladimir Bauer 8 years ago
4 changed file(s) with 21 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
110110 // RemoveAllPrependers removes all prepend functions
111111 func (b *Bar) RemoveAllPrependers() {
112112 select {
113 case b.operateState <- func(s *bState) {
114 s.pDecorators = nil
115 }:
113 case b.operateState <- func(s *bState) { s.pDecorators = nil }:
116114 case <-b.done:
117115 }
118116 }
120118 // RemoveAllAppenders removes all append functions
121119 func (b *Bar) RemoveAllAppenders() {
122120 select {
123 case b.operateState <- func(s *bState) {
124 s.aDecorators = nil
125 }:
121 case b.operateState <- func(s *bState) { s.aDecorators = nil }:
126122 case <-b.done:
127123 }
128124 }
183179 return
184180 }
185181 select {
186 case b.operateState <- func(s *bState) {
187 s.refill = &refill{r, till}
188 }:
182 case b.operateState <- func(s *bState) { s.refill = &refill{r, till} }:
189183 case <-b.done:
190184 }
191185 }
2929 pState struct {
3030 bHeap *priorityQueue
3131 heapUpdated bool
32 zeroWait bool
3233 idCounter int
3334 width int
3435 format string
107108 // Zero is highest priority, i.e. bar will be on top.
108109 func (p *Progress) UpdateBarPriority(b *Bar, priority int) {
109110 select {
110 case p.operateState <- func(s *pState) {
111 s.bHeap.update(b, priority)
112 }:
111 case p.operateState <- func(s *pState) { s.bHeap.update(b, priority) }:
113112 case <-p.done:
114113 }
115114 }
118117 func (p *Progress) BarCount() int {
119118 result := make(chan int, 1)
120119 select {
121 case p.operateState <- func(s *pState) {
122 result <- s.bHeap.Len()
123 }:
120 case p.operateState <- func(s *pState) { result <- s.bHeap.Len() }:
124121 return <-result
125122 case <-p.done:
126123 return 0
131128 // It's optional to call, in other words if you don't call Progress.Wait(),
132129 // it's not guaranteed that all bars will be flushed completely to the underlying io.Writer.
133130 func (p *Progress) Wait() {
131 if p.BarCount() == 0 {
132 select {
133 case p.operateState <- func(s *pState) { s.zeroWait = true }:
134 case <-p.done:
135 }
136 return
137 }
134138 <-p.done
135139 }
136140
2727 op(s)
2828 case <-s.ticker.C:
2929 if s.bHeap.Len() == 0 {
30 if s.zeroWait {
31 close(p.done)
32 return
33 }
3034 runtime.Gosched()
3135 break
3236 }
1717 op(s)
1818 case <-s.ticker.C:
1919 if s.bHeap.Len() == 0 {
20 if s.zeroWait {
21 close(p.done)
22 return
23 }
2024 runtime.Gosched()
2125 break
2226 }