Codebase list golang-github-vbauerster-mpb / aaed296
refactoring: bar.cacheState to bar.bs Vladimir Bauer 4 years ago
3 changed file(s) with 38 addition(s) and 44 deletion(s). Raw diff Collapse all Expand all
1616
1717 // Bar represents a progress bar.
1818 type Bar struct {
19 priority int // used by heap
20 index int // used by heap
21
19 index int // used by heap
2220 toShutdown bool
23 toDrop bool
24 noPop bool
2521 hasEwmaDecorators bool
2622 frameCh chan *frame
2723 operateState chan func(*bState)
2824 done chan struct{}
29 cacheState *bState
3025 container *Progress
26 bs *bState
3127 cancel func()
3228 recoveredPanic interface{}
3329 }
3733 // bState is actual bar's state.
3834 type bState struct {
3935 id int
40 priority int
36 priority int // used by heap
4137 reqWidth int
4238 total int64
4339 current int64
7268
7369 func newBar(container *Progress, bs *bState) (*Bar, func()) {
7470 ctx, cancel := context.WithCancel(container.ctx)
75
76 bar := &Bar{
77 priority: bs.priority,
78 toDrop: bs.dropOnComplete,
79 noPop: bs.noPop,
80 frameCh: make(chan *frame, 1),
81 operateState: make(chan func(*bState)),
82 done: make(chan struct{}),
83 container: container,
84 cancel: cancel,
85 }
86
87 bar.subscribeDecorators(bs)
88
71 operateState := make(chan func(*bState))
72 done := make(chan struct{})
8973 serve := func() {
9074 defer container.bwg.Done()
9175 for {
9276 select {
93 case op := <-bar.operateState:
77 case op := <-operateState:
9478 op(bs)
9579 case <-ctx.Done():
9680 bs.decoratorShutdownNotify()
97 bar.cacheState = bs
98 close(bar.done)
81 close(done)
9982 return
10083 }
10184 }
10285 }
86
87 bar := &Bar{
88 frameCh: make(chan *frame, 1),
89 operateState: operateState,
90 done: done,
91 container: container,
92 bs: bs,
93 cancel: cancel,
94 }
95
96 bar.subscribeDecorators(bs)
10397
10498 return bar, serve
10599 }
120114 case b.operateState <- func(s *bState) { result <- s.id }:
121115 return <-result
122116 case <-b.done:
123 return b.cacheState.id
124 }
125 }
126
127 // Current returns bar's current number, in other words sum of all increments.
117 return b.bs.id
118 }
119 }
120
121 // Current returns bar's current value, in other words sum of all increments.
128122 func (b *Bar) Current() int64 {
129123 result := make(chan int64)
130124 select {
131125 case b.operateState <- func(s *bState) { result <- s.current }:
132126 return <-result
133127 case <-b.done:
134 return b.cacheState.current
128 return b.bs.current
135129 }
136130 }
137131
250244 }
251245 }:
252246 case <-b.done:
253 if b.cacheState.lastIncrement > 0 {
254 b.cacheState.decoratorEwmaUpdate(dur)
255 b.cacheState.lastIncrement = 0
247 if b.bs.lastIncrement > 0 {
248 b.bs.decoratorEwmaUpdate(dur)
249 b.bs.lastIncrement = 0
256250 }
257251 }
258252 }
350344 b.frameCh <- &frame{reader, lines + 1}
351345 }:
352346 case <-b.done:
353 s := b.cacheState
347 s := b.bs
354348 stat := newStatistics(tw, s)
355349 var r io.Reader
356350 if b.recoveredPanic == nil {
405399 case b.operateState <- func(s *bState) { result <- s.wSyncTable() }:
406400 return <-result
407401 case <-b.done:
408 return b.cacheState.wSyncTable()
402 return b.bs.wSyncTable()
409403 }
410404 }
411405
55 func (pq priorityQueue) Len() int { return len(pq) }
66
77 func (pq priorityQueue) Less(i, j int) bool {
8 return pq[i].priority < pq[j].priority
8 return pq[i].bs.priority < pq[j].bs.priority
99 }
1010
1111 func (pq priorityQueue) Swap(i, j int) {
112112 }
113113
114114 // Add creates a bar which renders itself by provided filler.
115 // If `total <= 0` trigger complete event is disabled until reset with *bar.SetTotal(int64, bool).
115 // If `total <= 0` trigger complete event is disabled until reset with (*bar).SetTotal(int64, bool).
116116 // Panics if *Progress instance is done, i.e. called after (*Progress).Wait.
117117 func (p *Progress) Add(total int64, filler BarFiller, options ...BarOption) *Bar {
118118 if filler == nil {
179179 if b.index < 0 {
180180 return
181181 }
182 b.priority = priority
182 b.bs.priority = priority
183183 heap.Fix(&s.bHeap, b.index)
184184 }:
185185 case <-p.done:
343343
344344 for _, b := range s.barShutdownQueue {
345345 if qb, ok := s.queueBars[b]; ok {
346 qb.bar.priority = b.priority
346 qb.bar.bs.priority = b.bs.priority
347347 heap.Push(&s.bHeap, qb.bar)
348348 delete(s.queueBars, b)
349 b.toDrop = true
349 b.bs.dropOnComplete = true
350350 go qb.serve()
351 } else if s.popCompleted && !b.noPop {
351 } else if s.popCompleted && !b.bs.noPop {
352352 totalLines -= bm[b]
353 b.toDrop = true
354 }
355 if b.toDrop {
353 b.bs.dropOnComplete = true
354 }
355 if b.bs.dropOnComplete {
356356 delete(bm, b)
357357 s.heapUpdated = true
358358 }