diff --git a/bar.go b/bar.go index fad9831..489b6ca 100644 --- a/bar.go +++ b/bar.go @@ -244,6 +244,70 @@ } } +// Increment is a shorthand for b.IncrInt64(1). +func (b *Bar) Increment() { + b.IncrInt64(1) +} + +// IncrBy is a shorthand for b.IncrInt64(int64(n)). +func (b *Bar) IncrBy(n int) { + b.IncrInt64(int64(n)) +} + +// IncrInt64 increments progress by amount of n. +func (b *Bar) IncrInt64(n int64) { + select { + case b.operateState <- func(s *bState) { + s.current += n + if s.triggerComplete && s.current >= s.total { + s.current = s.total + s.triggerCompletion(b) + } + }: + case <-b.ctx.Done(): + } +} + +// EwmaIncrement is a shorthand for b.EwmaIncrInt64(1, iterDur). +func (b *Bar) EwmaIncrement(iterDur time.Duration) { + b.EwmaIncrInt64(1, iterDur) +} + +// EwmaIncrBy is a shorthand for b.EwmaIncrInt64(int64(n), iterDur). +func (b *Bar) EwmaIncrBy(n int, iterDur time.Duration) { + b.EwmaIncrInt64(int64(n), iterDur) +} + +// EwmaIncrInt64 increments progress by amount of n and updates EWMA based +// decorators by dur of a single iteration. +func (b *Bar) EwmaIncrInt64(n int64, iterDur time.Duration) { + var wg sync.WaitGroup + iter := make(chan decor.EwmaDecorator) + select { + case b.operateState <- func(s *bState) { + wg.Add(len(s.ewmaDecorators)) + for _, d := range s.ewmaDecorators { + iter <- d + } + close(iter) + s.current += n + if s.triggerComplete && s.current >= s.total { + s.current = s.total + s.triggerCompletion(b) + } + wg.Wait() + }: + for d := range iter { + d := d + go func() { + d.EwmaUpdate(n, iterDur) + wg.Done() + }() + } + case <-b.ctx.Done(): + } +} + // EwmaSetCurrent sets progress' current to an arbitrary value and updates // EWMA based decorators by dur of a single iteration. func (b *Bar) EwmaSetCurrent(current int64, iterDur time.Duration) { @@ -275,70 +339,6 @@ d := d go func() { d.EwmaUpdate(d.n, iterDur) - wg.Done() - }() - } - case <-b.ctx.Done(): - } -} - -// Increment is a shorthand for b.IncrInt64(1). -func (b *Bar) Increment() { - b.IncrInt64(1) -} - -// IncrBy is a shorthand for b.IncrInt64(int64(n)). -func (b *Bar) IncrBy(n int) { - b.IncrInt64(int64(n)) -} - -// IncrInt64 increments progress by amount of n. -func (b *Bar) IncrInt64(n int64) { - select { - case b.operateState <- func(s *bState) { - s.current += n - if s.triggerComplete && s.current >= s.total { - s.current = s.total - s.triggerCompletion(b) - } - }: - case <-b.ctx.Done(): - } -} - -// EwmaIncrement is a shorthand for b.EwmaIncrInt64(1, iterDur). -func (b *Bar) EwmaIncrement(iterDur time.Duration) { - b.EwmaIncrInt64(1, iterDur) -} - -// EwmaIncrBy is a shorthand for b.EwmaIncrInt64(int64(n), iterDur). -func (b *Bar) EwmaIncrBy(n int, iterDur time.Duration) { - b.EwmaIncrInt64(int64(n), iterDur) -} - -// EwmaIncrInt64 increments progress by amount of n and updates EWMA based -// decorators by dur of a single iteration. -func (b *Bar) EwmaIncrInt64(n int64, iterDur time.Duration) { - var wg sync.WaitGroup - iter := make(chan decor.EwmaDecorator) - select { - case b.operateState <- func(s *bState) { - wg.Add(len(s.ewmaDecorators)) - for _, d := range s.ewmaDecorators { - iter <- d - } - close(iter) - s.current += n - if s.triggerComplete && s.current >= s.total { - s.current = s.total - s.triggerCompletion(b) - } - wg.Wait() - }: - for d := range iter { - d := d - go func() { - d.EwmaUpdate(n, iterDur) wg.Done() }() }