diff --git a/bar.go b/bar.go index e4d4a49..b6016a1 100644 --- a/bar.go +++ b/bar.go @@ -17,16 +17,16 @@ // Bar represents a progress bar. type Bar struct { - index int // used by heap - priority int // used by heap - hasEwmaDecorators bool - frameCh chan *frame - operateState chan func(*bState) - done chan struct{} - container *Progress - bs *bState - cancel func() - recoveredPanic interface{} + index int // used by heap + priority int // used by heap + hasEwma bool + frameCh chan *frame + operateState chan func(*bState) + done chan struct{} + container *Progress + bs *bState + cancel func() + recoveredPanic interface{} } type extenderFunc func(in io.Reader, reqWidth int, st decor.Statistics) (out io.Reader, lines int) @@ -89,6 +89,7 @@ bar := &Bar{ priority: bs.priority, + hasEwma: len(bs.ewmaDecorators) != 0, frameCh: make(chan *frame, 1), operateState: operateState, done: done, @@ -96,8 +97,6 @@ bs: bs, cancel: cancel, } - - bar.subscribeDecorators(bs) return bar, serve } @@ -349,26 +348,6 @@ } } -func (b *Bar) subscribeDecorators(bs *bState) { - for _, decorators := range [...][]decor.Decorator{ - bs.pDecorators, - bs.aDecorators, - } { - for _, d := range decorators { - d = extractBaseDecorator(d) - if d, ok := d.(decor.AverageDecorator); ok { - bs.averageDecorators = append(bs.averageDecorators, d) - } - if d, ok := d.(decor.EwmaDecorator); ok { - bs.ewmaDecorators = append(bs.ewmaDecorators, d) - } - if d, ok := d.(decor.ShutdownListener); ok { - bs.shutdownListeners = append(bs.shutdownListeners, d) - } - } - } -} - func (b *Bar) forceRefreshIfLastUncompleted() { var anyOtherUncompleted bool b.container.traverseBars(func(bar *Bar) bool { @@ -457,6 +436,26 @@ return table } +func (s *bState) subscribeDecorators() { + for _, decorators := range [...][]decor.Decorator{ + s.pDecorators, + s.aDecorators, + } { + for _, d := range decorators { + d = extractBaseDecorator(d) + if d, ok := d.(decor.AverageDecorator); ok { + s.averageDecorators = append(s.averageDecorators, d) + } + if d, ok := d.(decor.EwmaDecorator); ok { + s.ewmaDecorators = append(s.ewmaDecorators, d) + } + if d, ok := d.(decor.ShutdownListener); ok { + s.shutdownListeners = append(s.shutdownListeners, d) + } + } + } +} + func (s bState) decoratorEwmaUpdate(dur time.Duration) { wg := new(sync.WaitGroup) for i := 0; i < len(s.ewmaDecorators); i++ { diff --git a/progress.go b/progress.go index 228ff73..ede745f 100644 --- a/progress.go +++ b/progress.go @@ -406,6 +406,8 @@ bs.buffers[i] = bytes.NewBuffer(make([]byte, 0, 512)) } + bs.subscribeDecorators() + return bs } diff --git a/proxyreader.go b/proxyreader.go index 25f195b..582a55e 100644 --- a/proxyreader.go +++ b/proxyreader.go @@ -65,12 +65,12 @@ pr := proxyReader{toReadCloser(r), b} if wt, ok := r.(io.WriterTo); ok { pw := proxyWriterTo{pr, wt} - if b.hasEwmaDecorators { + if b.hasEwma { rc = ewmaProxyWriterTo{ewmaProxyReader{pr}, pw} } else { rc = pw } - } else if b.hasEwmaDecorators { + } else if b.hasEwma { rc = ewmaProxyReader{pr} } else { rc = pr