| 34 | 34 |
total int64
|
| 35 | 35 |
current int64
|
| 36 | 36 |
refill int64
|
| 37 | |
lastIncrement int64
|
| 38 | 37 |
trimSpace bool
|
| 39 | 38 |
completed bool
|
| 40 | 39 |
aborted bool
|
|
| 224 | 223 |
func (b *Bar) SetCurrent(current int64) {
|
| 225 | 224 |
select {
|
| 226 | 225 |
case b.operateState <- func(s *bState) {
|
| 227 | |
s.lastIncrement = current - s.current
|
| 228 | 226 |
s.current = current
|
| 229 | 227 |
if s.triggerComplete && s.current >= s.total {
|
| 230 | 228 |
s.current = s.total
|
|
| 253 | 251 |
}
|
| 254 | 252 |
select {
|
| 255 | 253 |
case b.operateState <- func(s *bState) {
|
| 256 | |
s.lastIncrement = n
|
| 257 | 254 |
s.current += n
|
| 258 | 255 |
if s.triggerComplete && s.current >= s.total {
|
| 259 | 256 |
s.current = s.total
|
|
| 265 | 262 |
}
|
| 266 | 263 |
}
|
| 267 | 264 |
|
| 268 | |
// DecoratorEwmaUpdate updates all EWMA based decorators. Should be
|
| 269 | |
// called on each iteration, because EWMA's unit of measure is an
|
| 270 | |
// iteration's duration. Panics if called before *Bar.Incr... family
|
| 271 | |
// methods.
|
| 272 | |
func (b *Bar) DecoratorEwmaUpdate(dur time.Duration) {
|
| 273 | |
select {
|
| 274 | |
case b.operateState <- func(s *bState) {
|
| 275 | |
if s.lastIncrement > 0 {
|
| 276 | |
s.decoratorEwmaUpdate(dur)
|
| 277 | |
s.lastIncrement = 0
|
| 278 | |
} else {
|
| 279 | |
panic("increment required before ewma iteration update")
|
| 280 | |
}
|
| 281 | |
}:
|
| 282 | |
case <-b.done:
|
| 283 | |
if b.bs.lastIncrement > 0 {
|
| 284 | |
b.bs.decoratorEwmaUpdate(dur)
|
| 285 | |
b.bs.lastIncrement = 0
|
| 286 | |
}
|
|
265 |
// EwmaIncrement is a shorthand for b.EwmaIncrInt64(1, dur).
|
|
266 |
func (b *Bar) EwmaIncrement(dur time.Duration) {
|
|
267 |
b.EwmaIncrInt64(1, dur)
|
|
268 |
}
|
|
269 |
|
|
270 |
// EwmaIncrBy is a shorthand for b.EwmaIncrInt64(int64(n), dur).
|
|
271 |
func (b *Bar) EwmaIncrBy(n int, dur time.Duration) {
|
|
272 |
b.EwmaIncrInt64(int64(n), dur)
|
|
273 |
}
|
|
274 |
|
|
275 |
// EwmaIncrInt64 increments progress by amount of n and updates EWMA based
|
|
276 |
// decorators by dur of a single iteration.
|
|
277 |
func (b *Bar) EwmaIncrInt64(n int64, dur time.Duration) {
|
|
278 |
if n <= 0 {
|
|
279 |
return
|
|
280 |
}
|
|
281 |
select {
|
|
282 |
case b.operateState <- func(s *bState) {
|
|
283 |
s.current += n
|
|
284 |
s.ewmaUpdate(n, dur)
|
|
285 |
if s.triggerComplete && s.current >= s.total {
|
|
286 |
s.current = s.total
|
|
287 |
s.completed = true
|
|
288 |
go b.forceRefresh(s.manualRefresh)
|
|
289 |
}
|
|
290 |
}:
|
|
291 |
case <-b.done:
|
| 287 | 292 |
}
|
| 288 | 293 |
}
|
| 289 | 294 |
|
|
| 564 | 569 |
}
|
| 565 | 570 |
}
|
| 566 | 571 |
|
| 567 | |
func (s bState) decoratorEwmaUpdate(dur time.Duration) {
|
|
572 |
func (s bState) ewmaUpdate(n int64, dur time.Duration) {
|
| 568 | 573 |
var wg sync.WaitGroup
|
| 569 | 574 |
for i := 0; i < len(s.ewmaDecorators); i++ {
|
| 570 | 575 |
switch d := s.ewmaDecorators[i]; i {
|
| 571 | 576 |
case len(s.ewmaDecorators) - 1:
|
| 572 | |
d.EwmaUpdate(s.lastIncrement, dur)
|
|
577 |
d.EwmaUpdate(n, dur)
|
| 573 | 578 |
default:
|
| 574 | 579 |
wg.Add(1)
|
| 575 | 580 |
go func() {
|
| 576 | |
d.EwmaUpdate(s.lastIncrement, dur)
|
|
581 |
d.EwmaUpdate(n, dur)
|
| 577 | 582 |
wg.Done()
|
| 578 | 583 |
}()
|
| 579 | 584 |
}
|