Codebase list golang-github-vbauerster-mpb / 6545f6e
PopCompletedMode ContainerOption Vladimir Bauer 7 years ago
5 changed file(s) with 53 addition(s) and 56 deletion(s). Raw diff Collapse all Expand all
320320 frame = io.MultiReader(frame, s.bufE)
321321 }
322322
323 // b.toDrop = s.dropOnComplete
324323 b.toShutdown = s.toComplete && !s.completeFlushed
325324 s.completeFlushed = s.toComplete
326325
410409 }
411410 }
412411
413 // func (b *Bar) dropOnComplete() {
414 // select {
415 // case b.operateState <- func(s *bState) { s.dropOnComplete = true }:
416 // case <-b.done:
417 // }
418 // }
419
420412 func newStatistics(s *bState) *decor.Statistics {
421413 return &decor.Statistics{
422414 ID: s.id,
7373 }
7474 }
7575
76 // BarReplaceOnComplete is deprecated. Refer to BarParkTo option.
76 // BarReplaceOnComplete is deprecated. Use BarParkTo instead.
7777 func BarReplaceOnComplete(runningBar *Bar) BarOption {
7878 return BarParkTo(runningBar)
7979 }
8080
8181 // BarParkTo parks constructed bar into the runningBar. In other words,
82 // constructed bar will start only after runningBar has been completed.
83 // Parked bar inherits priority of the runningBar, if no BarPriority
84 // option is set. Parked bar will replace runningBar eventually.
82 // constructed bar will replace runningBar after it has been completed.
8583 func BarParkTo(runningBar *Bar) BarOption {
8684 if runningBar == nil {
8785 return nil
8886 }
89 runningBar.dropOnComplete()
9087 return func(s *bState) {
9188 s.runningBar = runningBar
9289 }
7878 }
7979 }
8080
81 // PopCompletedMode will pop and stop rendering completed bars.
82 func PopCompletedMode() ContainerOption {
83 return func(s *pState) {
84 s.popCompleted = true
85 }
86 }
87
8188 // ContainerOptOnCond returns option when condition evaluates to true.
8289 func ContainerOptOnCond(option ContainerOption, condition func() bool) ContainerOption {
8390 if condition() {
00 package mpb
1
2 import "container/heap"
31
42 // A priorityQueue implements heap.Interface
53 type priorityQueue []*Bar
3129 bar.index = -1 // for safety
3230 return bar
3331 }
34
35 // update modifies the priority of a Bar in the queue.
36 func (pq *priorityQueue) update(bar *Bar, priority int) {
37 bar.priority = priority
38 heap.Fix(pq, bar.index)
39 }
3838 pMatrix map[int][]chan int
3939 aMatrix map[int][]chan int
4040 barShutdownQueue []*Bar
41 barPopQueue []*Bar
4142
4243 // following are provided/overrided by user
4344 idCount int
4445 width int
46 popCompleted bool
4547 rr time.Duration
4648 uwg *sync.WaitGroup
4749 manualRefreshCh <-chan time.Time
131133 }
132134 bar := newBar(p, bs)
133135 if bs.runningBar != nil {
134 if bar.priority == ps.idCount {
135 bar.priority = bs.runningBar.priority
136 }
137136 ps.parkedBars[bs.runningBar] = bar
138137 } else {
139138 heap.Push(&ps.bHeap, bar)
162161 }
163162 }
164163
165 // UpdateBarPriority is deprecated. Please use *Bar.SetOrder.
164 func (p *Progress) setBarPriority(b *Bar, priority int) {
165 select {
166 case p.operateState <- func(s *pState) {
167 if b.index < 0 {
168 return
169 }
170 b.priority = priority
171 heap.Fix(&s.bHeap, b.index)
172 }:
173 case <-p.done:
174 }
175 }
176
177 // UpdateBarPriority same as *Bar.SetPriority.
166178 func (p *Progress) UpdateBarPriority(b *Bar, priority int) {
167179 p.setBarPriority(b, priority)
168 }
169
170 func (p *Progress) setBarPriority(b *Bar, priority int) {
171 select {
172 case p.operateState <- func(s *pState) { s.bHeap.update(b, priority) }:
173 case <-p.done:
174 }
175180 }
176181
177182 // BarCount returns bars count
254259 }
255260
256261 func (s *pState) flush(cw *cwriter.Writer) error {
257 var rpop bool
258262 var lineCount int
259263 hlen := s.bHeap.Len()
260 tmp := make([]*Bar, hlen)
264 bb := make([]*Bar, hlen)
261265 for i := 0; i < hlen; i++ {
262 bar := heap.Pop(&s.bHeap).(*Bar)
266 b := heap.Pop(&s.bHeap).(*Bar)
263267 defer func() {
264 if bar.toShutdown {
268 if b.toShutdown {
265269 // shutdown at next flush, in other words decrement underlying WaitGroup
266270 // only after the bar with completed state has been flushed. this
267271 // ensures no bar ends up with less than 100% rendered.
268 s.barShutdownQueue = append(s.barShutdownQueue, bar)
269 // if parkedBar := s.parkedBars[bar]; parkedBar != nil {
270 // heap.Push(&s.bHeap, parkedBar)
271 // s.heapUpdated = true
272 // delete(s.parkedBars, bar)
273 // }
274 // if bar.toDrop {
275 // s.heapUpdated = true
276 // return
277 // }
278 // bar.priority = 0
272 s.barShutdownQueue = append(s.barShutdownQueue, b)
273 if s.popCompleted && s.parkedBars[b] == nil {
274 b.priority = -1
275 }
279276 }
280277 }()
281 cw.ReadFrom(<-bar.frameCh)
282 lineCount += bar.extendedLines + 1
283 tmp[i] = bar
284 }
285
286 for _, b := range tmp {
278 cw.ReadFrom(<-b.frameCh)
279 lineCount += b.extendedLines + 1
280 bb[i] = b
281 }
282
283 for _, b := range bb {
287284 heap.Push(&s.bHeap, b)
288285 }
289286
290287 for _, b := range s.barShutdownQueue {
291288 if parkedBar := s.parkedBars[b]; parkedBar != nil {
292 heap.Remove(&s.bHeap, b.index)
289 parkedBar.priority = b.priority
293290 heap.Push(&s.bHeap, parkedBar)
294291 delete(s.parkedBars, b)
295 s.heapUpdated = true
296 } else if b.toDrop {
292 b.toDrop = true
293 }
294 if b.toDrop {
297295 heap.Remove(&s.bHeap, b.index)
298296 s.heapUpdated = true
297 } else if s.popCompleted {
298 defer func() {
299 s.barPopQueue = append(s.barPopQueue, b)
300 }()
299301 }
300302 b.cancel()
301303 }
302304 s.barShutdownQueue = s.barShutdownQueue[0:0]
305
306 for _, b := range s.barPopQueue {
307 heap.Remove(&s.bHeap, b.index)
308 s.heapUpdated = true
309 lineCount -= b.extendedLines + 1
310 }
311 s.barPopQueue = s.barPopQueue[0:0]
303312
304313 return cw.Flush(lineCount)
305314 }