diff --git a/bar.go b/bar.go index 9ae55f5..cdc3807 100644 --- a/bar.go +++ b/bar.go @@ -62,8 +62,8 @@ timeElapsed time.Duration blockStartTime time.Time timePerItem time.Duration - appendFuncs []decor.DecoratorFunc - prependFuncs []decor.DecoratorFunc + aDecorators []decor.DecoratorFunc + pDecorators []decor.DecoratorFunc refill *refill bufP, bufB, bufA *bytes.Buffer panicMsg string @@ -113,7 +113,7 @@ func (b *Bar) RemoveAllPrependers() { select { case b.operateState <- func(s *bState) { - s.prependFuncs = nil + s.pDecorators = nil }: case <-b.done: } @@ -123,7 +123,7 @@ func (b *Bar) RemoveAllAppenders() { select { case b.operateState <- func(s *bState) { - s.appendFuncs = nil + s.aDecorators = nil }: case <-b.done: } @@ -196,10 +196,10 @@ func (b *Bar) NumOfAppenders() int { result := make(chan int, 1) select { - case b.operateState <- func(s *bState) { result <- len(s.appendFuncs) }: - return <-result - case <-b.done: - return len(b.cacheState.appendFuncs) + case b.operateState <- func(s *bState) { result <- len(s.aDecorators) }: + return <-result + case <-b.done: + return len(b.cacheState.aDecorators) } } @@ -207,10 +207,10 @@ func (b *Bar) NumOfPrependers() int { result := make(chan int, 1) select { - case b.operateState <- func(s *bState) { result <- len(s.prependFuncs) }: - return <-result - case <-b.done: - return len(b.cacheState.prependFuncs) + case b.operateState <- func(s *bState) { result <- len(s.pDecorators) }: + return <-result + case <-b.done: + return len(b.cacheState.pDecorators) } } @@ -307,7 +307,7 @@ } } -func (b *Bar) render(tw int, prependWs, appendWs *widthSync) <-chan *toRenderReader { +func (b *Bar) render(tw int, pSyncer, aSyncer *widthSyncer) <-chan *toRenderReader { ch := make(chan *toRenderReader, 1) go func() { @@ -318,14 +318,14 @@ // recovering if external decorators panic if p := recover(); p != nil { s.panicMsg = fmt.Sprintf("b#%02d panic: %v\n", s.id, p) - s.prependFuncs = nil - s.appendFuncs = nil + s.pDecorators = nil + s.aDecorators = nil s.completed = true r = strings.NewReader(s.panicMsg) } ch <- &toRenderReader{r, s.completed, s.removed} }() - s.draw(tw, prependWs, appendWs) + s.draw(tw, pSyncer, aSyncer) r = io.MultiReader(s.bufP, s.bufB, s.bufA) }: case <-b.done: @@ -334,7 +334,7 @@ if s.panicMsg != "" { r = strings.NewReader(s.panicMsg) } else { - s.draw(tw, prependWs, appendWs) + s.draw(tw, pSyncer, aSyncer) r = io.MultiReader(s.bufP, s.bufB, s.bufA) } ch <- &toRenderReader{r, s.completed, s.removed} @@ -351,7 +351,7 @@ s.blockStartTime = next } -func (s *bState) draw(termWidth int, prependWs, appendWs *widthSync) { +func (s *bState) draw(termWidth int, pSyncer, aSyncer *widthSyncer) { if termWidth <= 0 { termWidth = s.width } @@ -360,8 +360,8 @@ // render prepend functions to the left of the bar s.bufP.Reset() - for i, f := range s.prependFuncs { - s.bufP.WriteString(f(stat, prependWs.Listen[i], prependWs.Result[i])) + for i, f := range s.pDecorators { + s.bufP.WriteString(f(stat, pSyncer.Accumulator[i], pSyncer.Distributor[i])) } if !s.trimLeftSpace { @@ -374,8 +374,8 @@ s.bufA.WriteByte(' ') } - for i, f := range s.appendFuncs { - s.bufA.WriteString(f(stat, appendWs.Listen[i], appendWs.Result[i])) + for i, f := range s.aDecorators { + s.bufA.WriteString(f(stat, aSyncer.Accumulator[i], aSyncer.Distributor[i])) } prependCount := utf8.RuneCount(s.bufP.Bytes()) diff --git a/bar_option.go b/bar_option.go index 9dbd272..c51aa04 100644 --- a/bar_option.go +++ b/bar_option.go @@ -9,14 +9,14 @@ // AppendDecorators let you inject decorators to the bar's right side func AppendDecorators(appenders ...decor.DecoratorFunc) BarOption { return func(s *bState) { - s.appendFuncs = append(s.appendFuncs, appenders...) + s.aDecorators = append(s.aDecorators, appenders...) } } // PrependDecorators let you inject decorators to the bar's left side func PrependDecorators(prependers ...decor.DecoratorFunc) BarOption { return func(s *bState) { - s.prependFuncs = append(s.prependFuncs, prependers...) + s.pDecorators = append(s.pDecorators, prependers...) } } diff --git a/decorators_test.go b/decorators_test.go index 61cc163..5871d91 100644 --- a/decorators_test.go +++ b/decorators_test.go @@ -127,7 +127,7 @@ res[i] = make(chan string, 1) go func(s step, ch chan string) { defer wg.Done() - ch <- dfn(s.stat, ws.Listen[0], ws.Result[0]) + ch <- dfn(s.stat, ws.Accumulator[0], ws.Distributor[0]) }(columnCase[i], res[i]) } wg.Wait() diff --git a/draw_test.go b/draw_test.go index c056fcf..dab45ad 100644 --- a/draw_test.go +++ b/draw_test.go @@ -178,8 +178,8 @@ }, } - prependWs := newWidthSync(nil, 1, 0) - appendWs := newWidthSync(nil, 1, 0) + prependWs := newWidthSyncer(nil, 1, 0) + appendWs := newWidthSyncer(nil, 1, 0) for termWidth, cases := range testSuite { for name, tc := range cases { s := newTestState() diff --git a/export_test.go b/export_test.go index 6e925d9..5669fc5 100644 --- a/export_test.go +++ b/export_test.go @@ -1,3 +1,3 @@ package mpb -var NewWidthSync = newWidthSync +var NewWidthSync = newWidthSyncer diff --git a/progress.go b/progress.go index c178348..a192888 100644 --- a/progress.go +++ b/progress.go @@ -51,9 +51,10 @@ shutdownNotifier chan struct{} cancel <-chan struct{} } - widthSync struct { - Listen []chan int - Result []chan int + widthSyncer struct { + // Public for easy testing + Accumulator []chan int + Distributor []chan int } toRenderSnapshot struct { bar *Bar @@ -155,23 +156,23 @@ <-p.done } -func newWidthSync(timeout <-chan struct{}, numBars, numColumn int) *widthSync { - ws := &widthSync{ - Listen: make([]chan int, numColumn), - Result: make([]chan int, numColumn), +func newWidthSyncer(timeout <-chan struct{}, numBars, numColumn int) *widthSyncer { + ws := &widthSyncer{ + Accumulator: make([]chan int, numColumn), + Distributor: make([]chan int, numColumn), } for i := 0; i < numColumn; i++ { - ws.Listen[i] = make(chan int, numBars) - ws.Result[i] = make(chan int, numBars) + ws.Accumulator[i] = make(chan int, numBars) + ws.Distributor[i] = make(chan int, numBars) } for i := 0; i < numColumn; i++ { - go func(listenCh <-chan int, resultCh chan<- int) { - defer close(resultCh) + go func(accumulator <-chan int, discharger chan<- int) { + defer close(discharger) widths := make([]int, 0, numBars) loop: for { select { - case w := <-listenCh: + case w := <-accumulator: widths = append(widths, w) if len(widths) == numBars { break loop @@ -185,23 +186,22 @@ } result := max(widths) for i := 0; i < len(widths); i++ { - resultCh <- result + discharger <- result } - }(ws.Listen[i], ws.Result[i]) + }(ws.Accumulator[i], ws.Distributor[i]) } return ws } func (s *pState) writeAndFlush(tw, numP, numA int) (err error) { - wSyncTimeout := make(chan struct{}) + timeout := make(chan struct{}) + pSyncer := newWidthSyncer(timeout, s.bHeap.Len(), numP) + aSyncer := newWidthSyncer(timeout, s.bHeap.Len(), numA) time.AfterFunc(s.rr-s.rr/12, func() { - close(wSyncTimeout) + close(timeout) }) - prependWs := newWidthSync(wSyncTimeout, s.bHeap.Len(), numP) - appendWs := newWidthSync(wSyncTimeout, s.bHeap.Len(), numA) - - for _, trs := range s.renderByPriority(tw, prependWs, appendWs) { + for _, trs := range s.renderByPriority(tw, pSyncer, aSyncer) { r := <-trs.pipe _, err = s.cw.ReadFrom(r) if !trs.bar.completed && r.toComplete { @@ -223,14 +223,14 @@ return } -func (s *pState) renderByPriority(tw int, prependWs, appendWs *widthSync) []*toRenderSnapshot { +func (s *pState) renderByPriority(tw int, pSyncer, aSyncer *widthSyncer) []*toRenderSnapshot { slice := make([]*toRenderSnapshot, 0, s.bHeap.Len()) for s.bHeap.Len() > 0 { b := heap.Pop(s.bHeap).(*Bar) defer heap.Push(s.bHeap, b) slice = append(slice, &toRenderSnapshot{ bar: b, - pipe: b.render(tw, prependWs, appendWs), + pipe: b.render(tw, pSyncer, aSyncer), }) } return slice