diff --git a/bar_test.go b/bar_test.go index 0247bed..d024af5 100644 --- a/bar_test.go +++ b/bar_test.go @@ -3,7 +3,6 @@ import ( "reflect" "testing" - "time" ) func TestFillBar(t *testing.T) { @@ -25,7 +24,7 @@ barWidth: 100, total: 100, current: 20, - want: []byte("[>]"), + want: []byte("[]"), }, { termWidth: 20, @@ -86,8 +85,8 @@ }, } - prependWs := newWidthSync(rr*time.Millisecond, 1, 0) - appendWs := newWidthSync(rr*time.Millisecond, 1, 0) + prependWs := newWidthSync(nil, 1, 0) + appendWs := newWidthSync(nil, 1, 0) for _, test := range tests { s := newTestState() s.width = test.barWidth diff --git a/progress.go b/progress.go index b4e6fc7..3369890 100644 --- a/progress.go +++ b/progress.go @@ -238,6 +238,7 @@ } wg.Done() } + var beforeRender BeforeRender cw := cwriter.New(os.Stdout) bars := make([]*Bar, 0, 3) @@ -281,8 +282,14 @@ beforeRender(bars) } - prependWs := newWidthSync(userRR, numBars, bars[0].NumOfPrependers()) - appendWs := newWidthSync(userRR, numBars, bars[0].NumOfAppenders()) + quitWidthSyncCh := make(chan struct{}) + time.AfterFunc(userRR, func() { + close(quitWidthSyncCh) + }) + + b0 := bars[0] + prependWs := newWidthSync(quitWidthSyncCh, numBars, b0.NumOfPrependers()) + appendWs := newWidthSync(quitWidthSyncCh, numBars, b0.NumOfAppenders()) width, _, _ := cwriter.GetTermSize() ibars := iBarsGen(bars, width) @@ -327,7 +334,7 @@ } } -func newWidthSync(userRR time.Duration, numBars, numColumn int) *widthSync { +func newWidthSync(quit <-chan struct{}, numBars, numColumn int) *widthSync { ws := &widthSync{ listen: make([]chan int, numColumn), result: make([]chan int, numColumn), @@ -340,7 +347,6 @@ go func(listenCh <-chan int, resultCh chan<- int) { defer close(resultCh) widths := make([]int, 0, numBars) - abandon := time.After(userRR) loop: for { select { @@ -349,7 +355,7 @@ if len(widths) == numBars { break loop } - case <-abandon: + case <-quit: return } }