| 16 | 16 |
|
| 17 | 17 |
// Bar represents a progress bar.
|
| 18 | 18 |
type Bar struct {
|
| 19 | |
priority int // used by heap
|
| 20 | |
index int // used by heap
|
| 21 | |
|
|
19 |
index int // used by heap
|
| 22 | 20 |
toShutdown bool
|
| 23 | |
toDrop bool
|
| 24 | |
noPop bool
|
| 25 | 21 |
hasEwmaDecorators bool
|
| 26 | 22 |
frameCh chan *frame
|
| 27 | 23 |
operateState chan func(*bState)
|
| 28 | 24 |
done chan struct{}
|
| 29 | |
cacheState *bState
|
| 30 | 25 |
container *Progress
|
|
26 |
bs *bState
|
| 31 | 27 |
cancel func()
|
| 32 | 28 |
recoveredPanic interface{}
|
| 33 | 29 |
}
|
|
| 37 | 33 |
// bState is actual bar's state.
|
| 38 | 34 |
type bState struct {
|
| 39 | 35 |
id int
|
| 40 | |
priority int
|
|
36 |
priority int // used by heap
|
| 41 | 37 |
reqWidth int
|
| 42 | 38 |
total int64
|
| 43 | 39 |
current int64
|
|
| 72 | 68 |
|
| 73 | 69 |
func newBar(container *Progress, bs *bState) (*Bar, func()) {
|
| 74 | 70 |
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{})
|
| 89 | 73 |
serve := func() {
|
| 90 | 74 |
defer container.bwg.Done()
|
| 91 | 75 |
for {
|
| 92 | 76 |
select {
|
| 93 | |
case op := <-bar.operateState:
|
|
77 |
case op := <-operateState:
|
| 94 | 78 |
op(bs)
|
| 95 | 79 |
case <-ctx.Done():
|
| 96 | 80 |
bs.decoratorShutdownNotify()
|
| 97 | |
bar.cacheState = bs
|
| 98 | |
close(bar.done)
|
|
81 |
close(done)
|
| 99 | 82 |
return
|
| 100 | 83 |
}
|
| 101 | 84 |
}
|
| 102 | 85 |
}
|
|
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)
|
| 103 | 97 |
|
| 104 | 98 |
return bar, serve
|
| 105 | 99 |
}
|
|
| 120 | 114 |
case b.operateState <- func(s *bState) { result <- s.id }:
|
| 121 | 115 |
return <-result
|
| 122 | 116 |
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.
|
| 128 | 122 |
func (b *Bar) Current() int64 {
|
| 129 | 123 |
result := make(chan int64)
|
| 130 | 124 |
select {
|
| 131 | 125 |
case b.operateState <- func(s *bState) { result <- s.current }:
|
| 132 | 126 |
return <-result
|
| 133 | 127 |
case <-b.done:
|
| 134 | |
return b.cacheState.current
|
|
128 |
return b.bs.current
|
| 135 | 129 |
}
|
| 136 | 130 |
}
|
| 137 | 131 |
|
|
| 250 | 244 |
}
|
| 251 | 245 |
}:
|
| 252 | 246 |
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
|
| 256 | 250 |
}
|
| 257 | 251 |
}
|
| 258 | 252 |
}
|
|
| 350 | 344 |
b.frameCh <- &frame{reader, lines + 1}
|
| 351 | 345 |
}:
|
| 352 | 346 |
case <-b.done:
|
| 353 | |
s := b.cacheState
|
|
347 |
s := b.bs
|
| 354 | 348 |
stat := newStatistics(tw, s)
|
| 355 | 349 |
var r io.Reader
|
| 356 | 350 |
if b.recoveredPanic == nil {
|
|
| 405 | 399 |
case b.operateState <- func(s *bState) { result <- s.wSyncTable() }:
|
| 406 | 400 |
return <-result
|
| 407 | 401 |
case <-b.done:
|
| 408 | |
return b.cacheState.wSyncTable()
|
|
402 |
return b.bs.wSyncTable()
|
| 409 | 403 |
}
|
| 410 | 404 |
}
|
| 411 | 405 |
|