diff --git a/bar.go b/bar.go index 0cf5e5a..5671873 100644 --- a/bar.go +++ b/bar.go @@ -58,7 +58,6 @@ pDecorators []decor.Decorator amountReceivers []decor.AmountReceiver shutdownListeners []decor.ShutdownListener - refill *refill bufP, bufB, bufA *bytes.Buffer bufNL *bytes.Buffer panicMsg string @@ -67,10 +66,6 @@ // following options are assigned to the *Bar priority int runningBar *Bar - } - refill struct { - r rune - limit int64 } frameReader struct { io.Reader @@ -200,14 +195,11 @@ } } -// SetRefill sets fill rune to r, up until n. -func (b *Bar) SetRefill(n int, r rune) { - if n <= 0 { - return - } +// SetRefill sets refill, if supported by underlying Filler. +func (b *Bar) SetRefill(upto int) { b.operateState <- func(s *bState) { - if bf, ok := s.filler.(*barFiller); ok { - bf.refill = &refill{r, int64(n)} + if f, ok := s.filler.(interface{ SetRefill(int) }); ok { + f.SetRefill(upto) } } } @@ -352,11 +344,11 @@ s.bufB.WriteByte(' ') } + calcWidth := s.width if prependCount+s.width+appendCount > termWidth { - s.filler.Fill(s.bufB, termWidth-prependCount-appendCount, stat) - } else { - s.filler.Fill(s.bufB, s.width, stat) - } + calcWidth = termWidth - prependCount - appendCount + } + s.filler.Fill(s.bufB, calcWidth, stat) if !s.trimSpace { s.bufB.WriteByte(' ') diff --git a/bar_filler.go b/bar_filler.go index 91c2001..2a7d0e8 100644 --- a/bar_filler.go +++ b/bar_filler.go @@ -14,13 +14,14 @@ rTip rEmpty rRight + rRefill ) -var defaultBarStyle = []rune("[=>-]") +var defaultBarStyle = []rune("[=>-]+") type barFiller struct { format []rune - refill *refill + rup int } func (s *barFiller) Fill(w io.Writer, width int, stat *decor.Statistics) { @@ -42,10 +43,10 @@ progressWidth-- } - if s.refill != nil { - refillCount := internal.Percentage(stat.Total, s.refill.limit, int64(width)) + if s.rup > 0 { + refillCount := internal.Percentage(stat.Total, int64(s.rup), int64(width)) rest := progressWidth - refillCount - str += runeRepeat(s.refill.r, int(refillCount)) + runeRepeat(s.format[rFill], int(rest)) + str += runeRepeat(s.format[rRefill], int(refillCount)) + runeRepeat(s.format[rFill], int(rest)) } else { str += runeRepeat(s.format[rFill], int(progressWidth)) } @@ -60,6 +61,10 @@ io.WriteString(w, str) } +func (s *barFiller) SetRefill(upto int) { + s.rup = upto +} + func runeRepeat(r rune, count int) string { return strings.Repeat(string(r), count) } diff --git a/bar_option.go b/bar_option.go index 71dd091..68cfef9 100644 --- a/bar_option.go +++ b/bar_option.go @@ -118,15 +118,10 @@ return t, ok } cb := func(t interface{}) { - bf := t.(*barFiller) if !utf8.ValidString(style) { - panic("invalid style string") + return } - defaultFormat := bf.format - bf.format = []rune(style) - if len(bf.format) < 5 { - bf.format = defaultFormat - } + copy(t.(*barFiller).format, []rune(style)) } return MakeFillerTypeSpecificBarOption(chk, cb) } diff --git a/bar_test.go b/bar_test.go index 8081ab7..cf7e02f 100644 --- a/bar_test.go +++ b/bar_test.go @@ -9,12 +9,12 @@ "time" "unicode/utf8" - "github.com/vbauerster/mpb/v4" + . "github.com/vbauerster/mpb/v4" "github.com/vbauerster/mpb/v4/decor" ) func TestBarCompleted(t *testing.T) { - p := mpb.New(mpb.WithOutput(ioutil.Discard)) + p := New(WithOutput(ioutil.Discard)) total := 80 bar := p.AddBar(int64(total)) @@ -32,10 +32,10 @@ } func TestBarID(t *testing.T) { - p := mpb.New(mpb.WithOutput(ioutil.Discard)) + p := New(WithOutput(ioutil.Discard)) total := 80 wantID := 11 - bar := p.AddBar(int64(total), mpb.BarID(wantID)) + bar := p.AddBar(int64(total), BarID(wantID)) go func() { for i := 0; i < total; i++ { @@ -57,15 +57,15 @@ var buf bytes.Buffer width := 100 - p := mpb.New(mpb.WithOutput(&buf), mpb.WithWidth(width)) + p := New(WithOutput(&buf), WithWidth(width)) total := 100 till := 30 - refillRune := '+' + refillRune := DefaultBarStyle[len(DefaultBarStyle)-1] - bar := p.AddBar(int64(total), mpb.TrimSpace()) + bar := p.AddBar(int64(total), TrimSpace()) - bar.SetRefill(till, refillRune) + bar.SetRefill(till) bar.IncrBy(till) for i := 0; i < total-till; i++ { @@ -90,9 +90,9 @@ func TestBarStyle(t *testing.T) { var buf bytes.Buffer customFormat := "╢▌▌░╟" - p := mpb.New(mpb.WithOutput(&buf)) + p := New(WithOutput(&buf)) total := 80 - bar := p.AddBar(int64(total), mpb.BarStyle(customFormat), mpb.TrimSpace()) + bar := p.AddBar(int64(total), BarStyle(customFormat), TrimSpace()) for i := 0; i < total; i++ { bar.Increment() @@ -116,12 +116,12 @@ func TestBarPanics(t *testing.T) { var buf bytes.Buffer - p := mpb.New(mpb.WithDebugOutput(&buf), mpb.WithOutput(ioutil.Discard)) + p := New(WithDebugOutput(&buf), WithOutput(ioutil.Discard)) wantPanic := "Upps!!!" total := 100 - bar := p.AddBar(int64(total), mpb.PrependDecorators(panicDecorator(wantPanic))) + bar := p.AddBar(int64(total), PrependDecorators(panicDecorator(wantPanic))) go func() { for i := 0; i < 100; i++ { diff --git a/draw_test.go b/draw_test.go index 0da35c2..6867f81 100644 --- a/draw_test.go +++ b/draw_test.go @@ -11,8 +11,8 @@ name string total, current int64 barWidth int - barRefill *refill trimSpace bool + rup int want string }{ 100: { @@ -54,19 +54,19 @@ want: " [=====================================>----------------------------------------------------------] ", }, { - name: "t,c,bw{100,40,100}:refill{'+', 32}", + name: "t,c,bw,rup{100,40,100,32}", + total: 100, + current: 40, + barWidth: 100, + rup: 32, + want: " [+++++++++++++++++++++++++++++++======>----------------------------------------------------------] ", + }, + { + name: "t,c,bw,rup{100,40,100,32}:trimSpace", total: 100, current: 40, barWidth: 100, - barRefill: &refill{'+', 32}, - want: " [+++++++++++++++++++++++++++++++======>----------------------------------------------------------] ", - }, - { - name: "t,c,bw{100,40,100}:refill{'+', 32}:trimSpace", - total: 100, - current: 40, - barWidth: 100, - barRefill: &refill{'+', 32}, + rup: 32, trimSpace: true, want: "[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]", }, @@ -215,8 +215,10 @@ s.total = tc.total s.current = tc.current s.trimSpace = tc.trimSpace - if tc.barRefill != nil { - s.filler.(*barFiller).refill = tc.barRefill + if tc.rup > 0 { + if f, ok := s.filler.(interface{ SetRefill(int) }); ok { + f.SetRefill(tc.rup) + } } tmpBuf.Reset() tmpBuf.ReadFrom(s.draw(termWidth)) diff --git a/export_test.go b/export_test.go index 0bc28fe..1d05eda 100644 --- a/export_test.go +++ b/export_test.go @@ -1,3 +1,6 @@ package mpb -var SyncWidth = syncWidth +var ( + SyncWidth = syncWidth + DefaultBarStyle = defaultBarStyle +)