From 6cc740c213aeeed3d2130a01793aaaf704097ceb Mon Sep 17 00:00:00 2001 From: Vladimir Bauer Date: Thu, 15 Aug 2024 16:43:36 +0500 Subject: [PATCH] sort decorators before starting b.serve --- bar.go | 17 +++++++++++++++++ progress.go | 14 ++------------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/bar.go b/bar.go index 06058d2e..2afaf72d 100644 --- a/bar.go +++ b/bar.go @@ -552,6 +552,23 @@ func (s *bState) wSyncTable() (table syncTable) { return table } +func (s *bState) sortDecorators() { + for _, decorators := range s.decorators { + for _, d := range decorators { + d := unwrap(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) triggerCompletion(b *Bar) { s.triggerComplete = true if s.autoRefresh { diff --git a/progress.go b/progress.go index ebb37f8c..b4eaf75b 100644 --- a/progress.go +++ b/progress.go @@ -11,7 +11,6 @@ import ( "time" "github.com/vbauerster/mpb/v8/cwriter" - "github.com/vbauerster/mpb/v8/decor" ) const defaultRefreshRate = 150 * time.Millisecond @@ -153,17 +152,6 @@ func (p *Progress) Add(total int64, filler BarFiller, options ...BarOption) (*Ba case p.operateState <- func(ps *pState) { bs := ps.makeBarState(total, filler, options...) bar := newBar(ps.ctx, p, bs) - bar.TraverseDecorators(func(d decor.Decorator) { - 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) - } - }) if bs.waitBar != nil { ps.queueBars[bs.waitBar] = bar } else { @@ -463,5 +451,7 @@ func (s pState) makeBarState(total int64, filler BarFiller, options ...BarOption bs.buffers[1] = bytes.NewBuffer(make([]byte, 0, 128)) // append bs.buffers[2] = bytes.NewBuffer(make([]byte, 0, 256)) // filler + bs.sortDecorators() + return bs }