TestDecorStatisticsAvailableWidth
Vladimir Bauer
5 years ago
| 253 | 253 | } |
| 254 | 254 | } |
| 255 | 255 | |
| 256 | func TestDecorStatisticsAvailableWidth(t *testing.T) { | |
| 257 | td1 := func(s decor.Statistics) string { | |
| 258 | if s.AvailableWidth != 80 { | |
| 259 | t.Errorf("expected AvailableWidth %d got %d\n", 80, s.AvailableWidth) | |
| 260 | } | |
| 261 | return fmt.Sprintf("\x1b[31;1;4m%s\x1b[0m", strings.Repeat("1", 20)) | |
| 262 | } | |
| 263 | td2 := func(s decor.Statistics) string { | |
| 264 | if s.AvailableWidth != 60 { | |
| 265 | t.Errorf("expected AvailableWidth %d got %d\n", 60, s.AvailableWidth) | |
| 266 | } | |
| 267 | return "" | |
| 268 | } | |
| 269 | total := 100 | |
| 270 | p := mpb.New( | |
| 271 | mpb.WithWidth(100), | |
| 272 | mpb.WithOutput(ioutil.Discard), | |
| 273 | ) | |
| 274 | bar := p.AddBar(int64(total), | |
| 275 | mpb.BarFillerTrim(), | |
| 276 | mpb.PrependDecorators( | |
| 277 | decor.Name(strings.Repeat("0", 20)), | |
| 278 | decor.Any(td1), | |
| 279 | ), | |
| 280 | mpb.AppendDecorators( | |
| 281 | decor.Any(td2), | |
| 282 | ), | |
| 283 | ) | |
| 284 | for i := 0; i < total; i++ { | |
| 285 | time.Sleep(10 * time.Millisecond) | |
| 286 | bar.Increment() | |
| 287 | } | |
| 288 | } | |
| 289 | ||
| 256 | 290 | func panicDecorator(panicMsg string, cond func(decor.Statistics) bool) decor.Decorator { |
| 257 | 291 | return decor.Any(func(st decor.Statistics) string { |
| 258 | 292 | if cond(st) { |