diff --git a/bar.go b/bar.go index 9b0de15..b151da5 100644 --- a/bar.go +++ b/bar.go @@ -4,7 +4,6 @@ "bytes" "fmt" "io" - "os" "sync" "time" "unicode/utf8" @@ -293,15 +292,16 @@ } func (b *Bar) render(tw int, prependWs, appendWs *widthSync) <-chan []byte { - ch := make(chan []byte) + ch := make(chan []byte, 1) go func() { var st state defer func() { // recovering if external decorators panic if p := recover(); p != nil { - fmt.Fprintf(os.Stderr, "bar %d: %q\n", st.id, p) + ch <- []byte(fmt.Sprintf("bar%02d panic: %q\n", st.id, p)) } + close(ch) }() result := make(chan state, 1) select { @@ -320,7 +320,6 @@ buf = concatenateBlocks(buf, st.bufP.Bytes(), st.bufB.Bytes(), st.bufA.Bytes()) buf = append(buf, '\n') ch <- buf - close(ch) }() return ch diff --git a/bar_test.go b/bar_test.go index 7653a70..48bc62f 100644 --- a/bar_test.go +++ b/bar_test.go @@ -142,32 +142,6 @@ } } -func TestGetSpinner(t *testing.T) { - var buf bytes.Buffer - p := mpb.New(mpb.Output(&buf)) - bar := p.AddBar(0, mpb.BarTrim()) - - for i := 0; i < 100; i++ { - time.Sleep(10 * time.Millisecond) - bar.Incr(1) - } - - p.Stop() - - spinnerChars := []byte(`-\|/`) - seen := make(map[byte]bool) - for _, b := range bytes.Trim(buf.Bytes(), "\n") { - if !seen[b] { - seen[b] = true - } - } - for _, b := range spinnerChars { - if !seen[b] { - t.Errorf("Char %#U not found in bar's output\n", b) - } - } -} - func TestBarGetID(t *testing.T) { var wg sync.WaitGroup var buf bytes.Buffer @@ -238,7 +212,7 @@ func TestBarPanics(t *testing.T) { var wg sync.WaitGroup var buf bytes.Buffer - p := mpb.New(mpb.Output(&buf)) + p := mpb.New(mpb.Output(&buf), mpb.WithWaitGroup(&wg)) wantPanic := "Upps!!!" numBars := 3 @@ -259,19 +233,17 @@ defer wg.Done() for i := 0; i < 100; i++ { time.Sleep(10 * time.Millisecond) - bar.Incr(1) + bar.Increment() } }() } - wg.Wait() - p.Stop() - - bytes := removeLastRune(buf.Bytes()) - out := strings.Split(string(bytes), "\n") + p.Stop() + + out := bytes.Split(removeLastRune(buf.Bytes()), []byte("\n")) gotPanic := out[len(out)-1] - if gotPanic != wantPanic { - t.Errorf("Want panic: %s, got panic: %s\n", wantPanic, gotPanic) + if string(gotPanic) != fmt.Sprintf("bar02 panic: %q", wantPanic) { + t.Errorf("Want: %q, got: %q\n", wantPanic, gotPanic) } } diff --git a/draw_test.go b/draw_test.go index 9457d93..0ee810c 100644 --- a/draw_test.go +++ b/draw_test.go @@ -1,7 +1,7 @@ package mpb import ( - "reflect" + "bytes" "testing" ) @@ -12,54 +12,68 @@ total int64 current int64 barRefill *refill - want []byte + want string }{ - { - termWidth: 1, - barWidth: 100, - want: []byte{}, - }, { termWidth: 2, barWidth: 100, + want: "", + }, + { + termWidth: 3, + barWidth: 100, total: 100, current: 20, - want: []byte("[]"), + want: "[-]", + }, + { + termWidth: 5, + barWidth: 100, + total: 100, + current: 20, + want: "[>--]", + }, + { + termWidth: 6, + barWidth: 100, + total: 100, + current: 20, + want: "[>---]", }, { termWidth: 20, barWidth: 100, total: 100, current: 20, - want: []byte("[===>--------------]"), + want: "[===>--------------]", }, { termWidth: 50, barWidth: 100, total: 100, current: 20, - want: []byte("[=========>--------------------------------------]"), + want: "[=========>--------------------------------------]", }, { termWidth: 100, barWidth: 100, total: 100, current: 0, - want: []byte("[--------------------------------------------------------------------------------------------------]"), + want: "[--------------------------------------------------------------------------------------------------]", }, { termWidth: 100, barWidth: 100, total: 100, current: 1, - want: []byte("[>-------------------------------------------------------------------------------------------------]"), + want: "[>-------------------------------------------------------------------------------------------------]", }, { termWidth: 100, barWidth: 100, total: 100, current: 40, - want: []byte("[======================================>-----------------------------------------------------------]"), + want: "[======================================>-----------------------------------------------------------]", }, { termWidth: 100, @@ -67,21 +81,21 @@ total: 100, current: 40, barRefill: &refill{'+', 32}, - want: []byte("[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]"), + want: "[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]", }, { termWidth: 100, barWidth: 100, total: 100, current: 99, - want: []byte("[================================================================================================>-]"), + want: "[================================================================================================>-]", }, { termWidth: 100, barWidth: 100, total: 100, current: 100, - want: []byte("[==================================================================================================]"), + want: "[==================================================================================================]", }, } @@ -95,8 +109,10 @@ if test.barRefill != nil { s.refill = test.barRefill } - got := draw(s, test.termWidth, prependWs, appendWs) - if !reflect.DeepEqual(test.want, got) { + // got := draw(s, test.termWidth, prependWs, appendWs) + s.draw(test.termWidth, prependWs, appendWs) + got := s.bufB.String() + if got != test.want { t.Errorf("Want: %q, Got: %q\n", test.want, got) } } @@ -106,6 +122,9 @@ s := &state{ trimLeftSpace: true, trimRightSpace: true, + bufP: new(bytes.Buffer), + bufB: new(bytes.Buffer), + bufA: new(bytes.Buffer), } s.updateFormat("[=>-]") return s