diff --git a/bar.go b/bar.go index a4a99ae..cee3f82 100644 --- a/bar.go +++ b/bar.go @@ -17,18 +17,14 @@ // Bar represents a progress bar. type Bar struct { - priority int // used by heap - index int // used by heap - + index int // used by heap toShutdown bool - toDrop bool - noPop bool hasEwmaDecorators bool frameCh chan *frame operateState chan func(*bState) done chan struct{} - cacheState *bState container *Progress + bs *bState cancel func() recoveredPanic interface{} } @@ -38,7 +34,7 @@ // bState is actual bar's state. type bState struct { id int - priority int + priority int // used by heap reqWidth int total int64 current int64 @@ -73,34 +69,32 @@ func newBar(container *Progress, bs *bState) (*Bar, func()) { ctx, cancel := context.WithCancel(container.ctx) - - bar := &Bar{ - priority: bs.priority, - toDrop: bs.dropOnComplete, - noPop: bs.noPop, - frameCh: make(chan *frame, 1), - operateState: make(chan func(*bState)), - done: make(chan struct{}), - container: container, - cancel: cancel, - } - - bar.subscribeDecorators(bs) - + operateState := make(chan func(*bState)) + done := make(chan struct{}) serve := func() { defer container.bwg.Done() for { select { - case op := <-bar.operateState: + case op := <-operateState: op(bs) case <-ctx.Done(): bs.decoratorShutdownNotify() - bar.cacheState = bs - close(bar.done) + close(done) return } } } + + bar := &Bar{ + frameCh: make(chan *frame, 1), + operateState: operateState, + done: done, + container: container, + bs: bs, + cancel: cancel, + } + + bar.subscribeDecorators(bs) return bar, serve } @@ -121,18 +115,18 @@ case b.operateState <- func(s *bState) { result <- s.id }: return <-result case <-b.done: - return b.cacheState.id - } -} - -// Current returns bar's current number, in other words sum of all increments. + return b.bs.id + } +} + +// Current returns bar's current value, in other words sum of all increments. func (b *Bar) Current() int64 { result := make(chan int64) select { case b.operateState <- func(s *bState) { result <- s.current }: return <-result case <-b.done: - return b.cacheState.current + return b.bs.current } } @@ -251,9 +245,9 @@ } }: case <-b.done: - if b.cacheState.lastIncrement > 0 { - b.cacheState.decoratorEwmaUpdate(dur) - b.cacheState.lastIncrement = 0 + if b.bs.lastIncrement > 0 { + b.bs.decoratorEwmaUpdate(dur) + b.bs.lastIncrement = 0 } } } @@ -351,7 +345,7 @@ b.frameCh <- &frame{reader, lines + 1} }: case <-b.done: - s := b.cacheState + s := b.bs stat := newStatistics(tw, s) var r io.Reader if b.recoveredPanic == nil { @@ -406,7 +400,7 @@ case b.operateState <- func(s *bState) { result <- s.wSyncTable() }: return <-result case <-b.done: - return b.cacheState.wSyncTable() + return b.bs.wSyncTable() } } diff --git a/priority_queue.go b/priority_queue.go index 29d9bd5..7f848bf 100644 --- a/priority_queue.go +++ b/priority_queue.go @@ -6,7 +6,7 @@ func (pq priorityQueue) Len() int { return len(pq) } func (pq priorityQueue) Less(i, j int) bool { - return pq[i].priority < pq[j].priority + return pq[i].bs.priority < pq[j].bs.priority } func (pq priorityQueue) Swap(i, j int) { diff --git a/progress.go b/progress.go index 5335594..de8cfbf 100644 --- a/progress.go +++ b/progress.go @@ -113,7 +113,7 @@ } // Add creates a bar which renders itself by provided filler. -// If `total <= 0` trigger complete event is disabled until reset with *bar.SetTotal(int64, bool). +// If `total <= 0` trigger complete event is disabled until reset with (*bar).SetTotal(int64, bool). // Panics if *Progress instance is done, i.e. called after (*Progress).Wait. func (p *Progress) Add(total int64, filler BarFiller, options ...BarOption) *Bar { if filler == nil { @@ -180,7 +180,7 @@ if b.index < 0 { return } - b.priority = priority + b.bs.priority = priority heap.Fix(&s.bHeap, b.index) }: case <-p.done: @@ -344,16 +344,16 @@ for _, b := range s.barShutdownQueue { if qb, ok := s.queueBars[b]; ok { - qb.bar.priority = b.priority + qb.bar.bs.priority = b.bs.priority heap.Push(&s.bHeap, qb.bar) delete(s.queueBars, b) - b.toDrop = true + b.bs.dropOnComplete = true go qb.serve() - } else if s.popCompleted && !b.noPop { + } else if s.popCompleted && !b.bs.noPop { totalLines -= bm[b] - b.toDrop = true - } - if b.toDrop { + b.bs.dropOnComplete = true + } + if b.bs.dropOnComplete { delete(bm, b) s.heapUpdated = true }