Codebase list golang-github-vbauerster-mpb / a512f73
BarNoBrackets option Vladimir Bauer 7 years ago
5 changed file(s) with 159 addition(s) and 38 deletion(s). Raw diff Collapse all Expand all
2525 bar := p.AddBar(int64(total),
2626 // reverse Bar#1
2727 mpb.BarOptOnCond(mpb.BarReverse(), func() bool { return i == 1 }),
28 mpb.BarNoBrackets(),
2829 mpb.PrependDecorators(
2930 // simple name decorator
3031 decor.Name(name),
349349 s.bufA.WriteString(d.Decor(stat))
350350 }
351351
352 s.bufA.WriteByte('\n')
352353 if s.noBufBOnComplete && s.completeFlushed {
353 s.bufA.WriteByte('\n')
354354 return io.MultiReader(s.bufP, s.bufA)
355355 }
356356
357357 prependCount := utf8.RuneCount(s.bufP.Bytes())
358 appendCount := utf8.RuneCount(s.bufA.Bytes())
359
360 if !s.trimSpace {
361 // reserve space for edge spaces
362 termWidth -= 2
363 s.bufB.WriteByte(' ')
364 }
365
366 calcWidth := s.width
367 if prependCount+s.width+appendCount > termWidth {
368 calcWidth = termWidth - prependCount - appendCount
369 }
370 s.filler.Fill(s.bufB, calcWidth, stat)
371
372 if !s.trimSpace {
373 s.bufB.WriteByte(' ')
374 }
375
376 s.bufA.WriteByte('\n')
358 appendCount := utf8.RuneCount(s.bufA.Bytes()) - 1
359
360 if fitWidth := s.width; termWidth > 1 {
361 if !s.trimSpace {
362 // reserve space for edge spaces
363 termWidth -= 2
364 s.bufB.WriteByte(' ')
365 defer s.bufB.WriteByte(' ')
366 }
367 if prependCount+s.width+appendCount > termWidth {
368 fitWidth = termWidth - prependCount - appendCount
369 }
370 s.filler.Fill(s.bufB, fitWidth, stat)
371 }
372
377373 return io.MultiReader(s.bufP, s.bufB, s.bufA)
378374 }
379375
2323 format [][]byte
2424 refillAmount int64
2525 reverse bool
26 noBrackets bool
2627 }
2728
2829 func newDefaultBarFiller() Filler {
4445 copy(s.format, src)
4546 }
4647
47 func (s *barFiller) setReverse() {
48 s.reverse = true
49 }
50
5148 func (s *barFiller) SetRefill(amount int64) {
5249 s.refillAmount = amount
5350 }
5451
5552 func (s *barFiller) Fill(w io.Writer, width int, stat *decor.Statistics) {
5653
57 // don't count rLeft and rRight [brackets]
58 width -= 2
59 if width < 2 {
60 return
61 }
62
63 w.Write(s.format[rLeft])
64 if width == 2 {
65 w.Write(s.format[rRight])
66 return
54 if !s.noBrackets {
55 // don't count rLeft and rRight as progress
56 width -= 2
57 if width < 2 {
58 return
59 }
60 w.Write(s.format[rLeft])
61 defer w.Write(s.format[rRight])
6762 }
6863
6964 bb := make([][]byte, width)
106101 w.Write(bb[i])
107102 }
108103 }
109 w.Write(s.format[rRight])
110104 }
126126 // '+' refill rune, used when *Bar.SetRefill(int64) is called
127127 //
128128 // It's ok to provide first five runes only, for example mpb.BarStyle("╢▌▌░╟")
129 // To omit left and right edge runes use BarNoBrackets option.
129130 func BarStyle(style string) BarOption {
130131 chk := func(filler Filler) (interface{}, bool) {
131132 if style == "" {
140141 return MakeFillerTypeSpecificBarOption(chk, cb)
141142 }
142143
144 // BarNoBrackets omits left and right edge runes of the bar. Edges are
145 // brackets by default, hence the name of the option.
146 func BarNoBrackets() BarOption {
147 chk := func(filler Filler) (interface{}, bool) {
148 t, ok := filler.(*barFiller)
149 return t, ok
150 }
151 cb := func(t interface{}) {
152 t.(*barFiller).noBrackets = true
153 }
154 return MakeFillerTypeSpecificBarOption(chk, cb)
155 }
156
143157 // BarReverse reverse mode, bar will progress from right to left.
144158 func BarReverse() BarOption {
145159 chk := func(filler Filler) (interface{}, bool) {
147161 return t, ok
148162 }
149163 cb := func(t interface{}) {
150 t.(*barFiller).setReverse()
164 t.(*barFiller).reverse = true
151165 }
152166 return MakeFillerTypeSpecificBarOption(chk, cb)
153167 }
1212 total, current int64
1313 barWidth int
1414 trimSpace bool
15 noBrackets bool
1516 rup int64
1617 want string
1718 }{
19 0: {
20 {
21 name: "t,c,bw{60,20,80}",
22 total: 60,
23 current: 20,
24 barWidth: 80,
25 want: "",
26 },
27 {
28 name: "t,c,bw{60,20,80}",
29 total: 60,
30 current: 20,
31 barWidth: 80,
32 trimSpace: true,
33 want: "",
34 },
35 {
36 name: "t,c,bw,noBrackets{60,20,80}",
37 total: 60,
38 current: 20,
39 barWidth: 80,
40 noBrackets: true,
41 want: "",
42 },
43 },
44 1: {
45 {
46 name: "t,c,bw{60,20,80}",
47 total: 60,
48 current: 20,
49 barWidth: 80,
50 want: "",
51 },
52 {
53 name: "t,c,bw{60,20,80}",
54 total: 60,
55 current: 20,
56 barWidth: 80,
57 trimSpace: true,
58 want: "",
59 },
60 {
61 name: "t,c,bw,noBrackets{60,20,80}",
62 total: 60,
63 current: 20,
64 barWidth: 80,
65 noBrackets: true,
66 want: "",
67 },
68 },
1869 2: {
1970 {
2071 name: "t,c,bw{60,20,80}",
3182 trimSpace: true,
3283 want: "",
3384 },
85 {
86 name: "t,c,bw,noBrackets{60,20,80,true}",
87 total: 60,
88 current: 20,
89 barWidth: 80,
90 noBrackets: true,
91 want: " ",
92 },
3493 },
3594 3: {
3695 {
48107 trimSpace: true,
49108 want: "",
50109 },
110 {
111 name: "t,c,bw,trim{60,20,80,true}",
112 total: 60,
113 current: 20,
114 barWidth: 80,
115 noBrackets: true,
116 want: " - ",
117 },
51118 },
52119 4: {
53120 {
63130 current: 20,
64131 barWidth: 80,
65132 trimSpace: true,
66 want: "[]",
133 want: "[>-]",
134 },
135 {
136 name: "t,c,bw,noBrackets{60,20,80,true}",
137 total: 60,
138 current: 20,
139 barWidth: 80,
140 noBrackets: true,
141 want: " >- ",
67142 },
68143 },
69144 5: {
82157 trimSpace: true,
83158 want: "[>--]",
84159 },
160 {
161 name: "t,c,bw,noBrackets{60,20,80,true}",
162 total: 60,
163 current: 20,
164 barWidth: 80,
165 noBrackets: true,
166 want: " >-- ",
167 },
85168 },
86169 6: {
87170 {
89172 total: 60,
90173 current: 20,
91174 barWidth: 80,
92 want: " [] ",
175 want: " [>-] ",
93176 },
94177 {
95178 name: "t,c,bw,trim{60,20,80,true}",
99182 trimSpace: true,
100183 want: "[>---]",
101184 },
185 {
186 name: "t,c,bw,noBrackets{60,20,80,true}",
187 total: 60,
188 current: 20,
189 barWidth: 80,
190 noBrackets: true,
191 want: " >--- ",
192 },
102193 },
103194 7: {
104195 {
116207 trimSpace: true,
117208 want: "[=>---]",
118209 },
210 {
211 name: "t,c,bw,noBrackets{60,20,80,true}",
212 total: 60,
213 current: 20,
214 barWidth: 80,
215 noBrackets: true,
216 want: " =>--- ",
217 },
119218 },
120219 8: {
121220 {
133232 trimSpace: true,
134233 want: "[=>----]",
135234 },
235 {
236 name: "t,c,bw,noBrackets{60,20,80,true}",
237 total: 60,
238 current: 20,
239 barWidth: 80,
240 noBrackets: true,
241 want: " =>---- ",
242 },
136243 },
137244 80: {
138245 {
149256 barWidth: 80,
150257 trimSpace: true,
151258 want: "[=========================>----------------------------------------------------]",
259 },
260 {
261 name: "t,c,bw,noBrackets{60,20,80,true}",
262 total: 60,
263 current: 20,
264 barWidth: 80,
265 noBrackets: true,
266 want: " =========================>---------------------------------------------------- ",
152267 },
153268 },
154269 100: {
272387 s.total = tc.total
273388 s.current = tc.current
274389 s.trimSpace = tc.trimSpace
390 s.filler.(*barFiller).noBrackets = tc.noBrackets
275391 if tc.rup > 0 {
276392 if f, ok := s.filler.(interface{ SetRefill(int64) }); ok {
277393 f.SetRefill(tc.rup)