diff --git a/_examples/reverseBar/main.go b/_examples/reverseBar/main.go index f48b39a..053e5bc 100644 --- a/_examples/reverseBar/main.go +++ b/_examples/reverseBar/main.go @@ -26,6 +26,7 @@ bar := p.AddBar(int64(total), // reverse Bar#1 mpb.BarOptOnCond(mpb.BarReverse(), func() bool { return i == 1 }), + mpb.BarNoBrackets(), mpb.PrependDecorators( // simple name decorator decor.Name(name), diff --git a/bar.go b/bar.go index 1ba0505..d629e78 100644 --- a/bar.go +++ b/bar.go @@ -350,31 +350,27 @@ s.bufA.WriteString(d.Decor(stat)) } + s.bufA.WriteByte('\n') if s.noBufBOnComplete && s.completeFlushed { - s.bufA.WriteByte('\n') return io.MultiReader(s.bufP, s.bufA) } prependCount := utf8.RuneCount(s.bufP.Bytes()) - appendCount := utf8.RuneCount(s.bufA.Bytes()) - - if !s.trimSpace { - // reserve space for edge spaces - termWidth -= 2 - s.bufB.WriteByte(' ') - } - - calcWidth := s.width - if prependCount+s.width+appendCount > termWidth { - calcWidth = termWidth - prependCount - appendCount - } - s.filler.Fill(s.bufB, calcWidth, stat) - - if !s.trimSpace { - s.bufB.WriteByte(' ') - } - - s.bufA.WriteByte('\n') + appendCount := utf8.RuneCount(s.bufA.Bytes()) - 1 + + if fitWidth := s.width; termWidth > 1 { + if !s.trimSpace { + // reserve space for edge spaces + termWidth -= 2 + s.bufB.WriteByte(' ') + defer s.bufB.WriteByte(' ') + } + if prependCount+s.width+appendCount > termWidth { + fitWidth = termWidth - prependCount - appendCount + } + s.filler.Fill(s.bufB, fitWidth, stat) + } + return io.MultiReader(s.bufP, s.bufB, s.bufA) } diff --git a/bar_filler.go b/bar_filler.go index afd714a..a6eb23d 100644 --- a/bar_filler.go +++ b/bar_filler.go @@ -24,6 +24,7 @@ format [][]byte refillAmount int64 reverse bool + noBrackets bool } func newDefaultBarFiller() Filler { @@ -45,26 +46,20 @@ copy(s.format, src) } -func (s *barFiller) setReverse() { - s.reverse = true -} - func (s *barFiller) SetRefill(amount int64) { s.refillAmount = amount } func (s *barFiller) Fill(w io.Writer, width int, stat *decor.Statistics) { - // don't count rLeft and rRight [brackets] - width -= 2 - if width < 2 { - return - } - - w.Write(s.format[rLeft]) - if width == 2 { - w.Write(s.format[rRight]) - return + if !s.noBrackets { + // don't count rLeft and rRight as progress + width -= 2 + if width < 2 { + return + } + w.Write(s.format[rLeft]) + defer w.Write(s.format[rRight]) } bb := make([][]byte, width) @@ -107,5 +102,4 @@ w.Write(bb[i]) } } - w.Write(s.format[rRight]) } diff --git a/bar_option.go b/bar_option.go index db51306..3bb6ec2 100644 --- a/bar_option.go +++ b/bar_option.go @@ -127,6 +127,7 @@ // '+' refill rune, used when *Bar.SetRefill(int64) is called // // It's ok to provide first five runes only, for example mpb.BarStyle("╢▌▌░╟") +// To omit left and right edge runes use BarNoBrackets option. func BarStyle(style string) BarOption { chk := func(filler Filler) (interface{}, bool) { if style == "" { @@ -141,6 +142,19 @@ return MakeFillerTypeSpecificBarOption(chk, cb) } +// BarNoBrackets omits left and right edge runes of the bar. Edges are +// brackets by default, hence the name of the option. +func BarNoBrackets() BarOption { + chk := func(filler Filler) (interface{}, bool) { + t, ok := filler.(*barFiller) + return t, ok + } + cb := func(t interface{}) { + t.(*barFiller).noBrackets = true + } + return MakeFillerTypeSpecificBarOption(chk, cb) +} + // BarReverse reverse mode, bar will progress from right to left. func BarReverse() BarOption { chk := func(filler Filler) (interface{}, bool) { @@ -148,7 +162,7 @@ return t, ok } cb := func(t interface{}) { - t.(*barFiller).setReverse() + t.(*barFiller).reverse = true } return MakeFillerTypeSpecificBarOption(chk, cb) } diff --git a/draw_test.go b/draw_test.go index 9374528..f87753a 100644 --- a/draw_test.go +++ b/draw_test.go @@ -13,9 +13,60 @@ total, current int64 barWidth int trimSpace bool + noBrackets bool rup int64 want string }{ + 0: { + { + name: "t,c,bw{60,20,80}", + total: 60, + current: 20, + barWidth: 80, + want: "", + }, + { + name: "t,c,bw{60,20,80}", + total: 60, + current: 20, + barWidth: 80, + trimSpace: true, + want: "", + }, + { + name: "t,c,bw,noBrackets{60,20,80}", + total: 60, + current: 20, + barWidth: 80, + noBrackets: true, + want: "", + }, + }, + 1: { + { + name: "t,c,bw{60,20,80}", + total: 60, + current: 20, + barWidth: 80, + want: "", + }, + { + name: "t,c,bw{60,20,80}", + total: 60, + current: 20, + barWidth: 80, + trimSpace: true, + want: "", + }, + { + name: "t,c,bw,noBrackets{60,20,80}", + total: 60, + current: 20, + barWidth: 80, + noBrackets: true, + want: "", + }, + }, 2: { { name: "t,c,bw{60,20,80}", @@ -32,6 +83,14 @@ trimSpace: true, want: "", }, + { + name: "t,c,bw,noBrackets{60,20,80,true}", + total: 60, + current: 20, + barWidth: 80, + noBrackets: true, + want: " ", + }, }, 3: { { @@ -49,6 +108,14 @@ trimSpace: true, want: "", }, + { + name: "t,c,bw,trim{60,20,80,true}", + total: 60, + current: 20, + barWidth: 80, + noBrackets: true, + want: " - ", + }, }, 4: { { @@ -64,7 +131,15 @@ current: 20, barWidth: 80, trimSpace: true, - want: "[]", + want: "[>-]", + }, + { + name: "t,c,bw,noBrackets{60,20,80,true}", + total: 60, + current: 20, + barWidth: 80, + noBrackets: true, + want: " >- ", }, }, 5: { @@ -83,6 +158,14 @@ trimSpace: true, want: "[>--]", }, + { + name: "t,c,bw,noBrackets{60,20,80,true}", + total: 60, + current: 20, + barWidth: 80, + noBrackets: true, + want: " >-- ", + }, }, 6: { { @@ -90,7 +173,7 @@ total: 60, current: 20, barWidth: 80, - want: " [] ", + want: " [>-] ", }, { name: "t,c,bw,trim{60,20,80,true}", @@ -100,6 +183,14 @@ trimSpace: true, want: "[>---]", }, + { + name: "t,c,bw,noBrackets{60,20,80,true}", + total: 60, + current: 20, + barWidth: 80, + noBrackets: true, + want: " >--- ", + }, }, 7: { { @@ -117,6 +208,14 @@ trimSpace: true, want: "[=>---]", }, + { + name: "t,c,bw,noBrackets{60,20,80,true}", + total: 60, + current: 20, + barWidth: 80, + noBrackets: true, + want: " =>--- ", + }, }, 8: { { @@ -134,6 +233,14 @@ trimSpace: true, want: "[=>----]", }, + { + name: "t,c,bw,noBrackets{60,20,80,true}", + total: 60, + current: 20, + barWidth: 80, + noBrackets: true, + want: " =>---- ", + }, }, 80: { { @@ -150,6 +257,14 @@ barWidth: 80, trimSpace: true, want: "[=========================>----------------------------------------------------]", + }, + { + name: "t,c,bw,noBrackets{60,20,80,true}", + total: 60, + current: 20, + barWidth: 80, + noBrackets: true, + want: " =========================>---------------------------------------------------- ", }, }, 100: { @@ -273,6 +388,7 @@ s.total = tc.total s.current = tc.current s.trimSpace = tc.trimSpace + s.filler.(*barFiller).noBrackets = tc.noBrackets if tc.rup > 0 { if f, ok := s.filler.(interface{ SetRefill(int64) }); ok { f.SetRefill(tc.rup)