BarNoBrackets option
Vladimir Bauer
7 years ago
| 25 | 25 | bar := p.AddBar(int64(total), |
| 26 | 26 | // reverse Bar#1 |
| 27 | 27 | mpb.BarOptOnCond(mpb.BarReverse(), func() bool { return i == 1 }), |
| 28 | mpb.BarNoBrackets(), | |
| 28 | 29 | mpb.PrependDecorators( |
| 29 | 30 | // simple name decorator |
| 30 | 31 | decor.Name(name), |
| 349 | 349 | s.bufA.WriteString(d.Decor(stat)) |
| 350 | 350 | } |
| 351 | 351 | |
| 352 | s.bufA.WriteByte('\n') | |
| 352 | 353 | if s.noBufBOnComplete && s.completeFlushed { |
| 353 | s.bufA.WriteByte('\n') | |
| 354 | 354 | return io.MultiReader(s.bufP, s.bufA) |
| 355 | 355 | } |
| 356 | 356 | |
| 357 | 357 | 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 | ||
| 377 | 373 | return io.MultiReader(s.bufP, s.bufB, s.bufA) |
| 378 | 374 | } |
| 379 | 375 |
| 23 | 23 | format [][]byte |
| 24 | 24 | refillAmount int64 |
| 25 | 25 | reverse bool |
| 26 | noBrackets bool | |
| 26 | 27 | } |
| 27 | 28 | |
| 28 | 29 | func newDefaultBarFiller() Filler { |
| 44 | 45 | copy(s.format, src) |
| 45 | 46 | } |
| 46 | 47 | |
| 47 | func (s *barFiller) setReverse() { | |
| 48 | s.reverse = true | |
| 49 | } | |
| 50 | ||
| 51 | 48 | func (s *barFiller) SetRefill(amount int64) { |
| 52 | 49 | s.refillAmount = amount |
| 53 | 50 | } |
| 54 | 51 | |
| 55 | 52 | func (s *barFiller) Fill(w io.Writer, width int, stat *decor.Statistics) { |
| 56 | 53 | |
| 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]) | |
| 67 | 62 | } |
| 68 | 63 | |
| 69 | 64 | bb := make([][]byte, width) |
| 106 | 101 | w.Write(bb[i]) |
| 107 | 102 | } |
| 108 | 103 | } |
| 109 | w.Write(s.format[rRight]) | |
| 110 | 104 | } |
| 126 | 126 | // '+' refill rune, used when *Bar.SetRefill(int64) is called |
| 127 | 127 | // |
| 128 | 128 | // It's ok to provide first five runes only, for example mpb.BarStyle("╢▌▌░╟") |
| 129 | // To omit left and right edge runes use BarNoBrackets option. | |
| 129 | 130 | func BarStyle(style string) BarOption { |
| 130 | 131 | chk := func(filler Filler) (interface{}, bool) { |
| 131 | 132 | if style == "" { |
| 140 | 141 | return MakeFillerTypeSpecificBarOption(chk, cb) |
| 141 | 142 | } |
| 142 | 143 | |
| 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 | ||
| 143 | 157 | // BarReverse reverse mode, bar will progress from right to left. |
| 144 | 158 | func BarReverse() BarOption { |
| 145 | 159 | chk := func(filler Filler) (interface{}, bool) { |
| 147 | 161 | return t, ok |
| 148 | 162 | } |
| 149 | 163 | cb := func(t interface{}) { |
| 150 | t.(*barFiller).setReverse() | |
| 164 | t.(*barFiller).reverse = true | |
| 151 | 165 | } |
| 152 | 166 | return MakeFillerTypeSpecificBarOption(chk, cb) |
| 153 | 167 | } |
| 12 | 12 | total, current int64 |
| 13 | 13 | barWidth int |
| 14 | 14 | trimSpace bool |
| 15 | noBrackets bool | |
| 15 | 16 | rup int64 |
| 16 | 17 | want string |
| 17 | 18 | }{ |
| 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 | }, | |
| 18 | 69 | 2: { |
| 19 | 70 | { |
| 20 | 71 | name: "t,c,bw{60,20,80}", |
| 31 | 82 | trimSpace: true, |
| 32 | 83 | want: "", |
| 33 | 84 | }, |
| 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 | }, | |
| 34 | 93 | }, |
| 35 | 94 | 3: { |
| 36 | 95 | { |
| 48 | 107 | trimSpace: true, |
| 49 | 108 | want: "", |
| 50 | 109 | }, |
| 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 | }, | |
| 51 | 118 | }, |
| 52 | 119 | 4: { |
| 53 | 120 | { |
| 63 | 130 | current: 20, |
| 64 | 131 | barWidth: 80, |
| 65 | 132 | 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: " >- ", | |
| 67 | 142 | }, |
| 68 | 143 | }, |
| 69 | 144 | 5: { |
| 82 | 157 | trimSpace: true, |
| 83 | 158 | want: "[>--]", |
| 84 | 159 | }, |
| 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 | }, | |
| 85 | 168 | }, |
| 86 | 169 | 6: { |
| 87 | 170 | { |
| 89 | 172 | total: 60, |
| 90 | 173 | current: 20, |
| 91 | 174 | barWidth: 80, |
| 92 | want: " [] ", | |
| 175 | want: " [>-] ", | |
| 93 | 176 | }, |
| 94 | 177 | { |
| 95 | 178 | name: "t,c,bw,trim{60,20,80,true}", |
| 99 | 182 | trimSpace: true, |
| 100 | 183 | want: "[>---]", |
| 101 | 184 | }, |
| 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 | }, | |
| 102 | 193 | }, |
| 103 | 194 | 7: { |
| 104 | 195 | { |
| 116 | 207 | trimSpace: true, |
| 117 | 208 | want: "[=>---]", |
| 118 | 209 | }, |
| 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 | }, | |
| 119 | 218 | }, |
| 120 | 219 | 8: { |
| 121 | 220 | { |
| 133 | 232 | trimSpace: true, |
| 134 | 233 | want: "[=>----]", |
| 135 | 234 | }, |
| 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 | }, | |
| 136 | 243 | }, |
| 137 | 244 | 80: { |
| 138 | 245 | { |
| 149 | 256 | barWidth: 80, |
| 150 | 257 | trimSpace: true, |
| 151 | 258 | 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: " =========================>---------------------------------------------------- ", | |
| 152 | 267 | }, |
| 153 | 268 | }, |
| 154 | 269 | 100: { |
| 272 | 387 | s.total = tc.total |
| 273 | 388 | s.current = tc.current |
| 274 | 389 | s.trimSpace = tc.trimSpace |
| 390 | s.filler.(*barFiller).noBrackets = tc.noBrackets | |
| 275 | 391 | if tc.rup > 0 { |
| 276 | 392 | if f, ok := s.filler.(interface{ SetRefill(int64) }); ok { |
| 277 | 393 | f.SetRefill(tc.rup) |