decoratorsOnTop example
Vladimir Bauer
6 years ago
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "io" | |
| 4 | "math/rand" | |
| 5 | "time" | |
| 6 | ||
| 7 | "github.com/vbauerster/mpb/v5" | |
| 8 | "github.com/vbauerster/mpb/v5/decor" | |
| 9 | ) | |
| 10 | ||
| 11 | func main() { | |
| 12 | p := mpb.New() | |
| 13 | ||
| 14 | total := 100 | |
| 15 | bar := p.Add(int64(total), nil, | |
| 16 | mpb.BarExtender(nlBarFiller(mpb.NewBarFiller("╢▌▌░╟", false))), | |
| 17 | // mpb.BarExtender(nlBarFiller(mpb.NewBarFiller("", false))), | |
| 18 | mpb.PrependDecorators( | |
| 19 | decor.Name("Percentage: "), | |
| 20 | decor.NewPercentage("%d"), | |
| 21 | ), | |
| 22 | mpb.AppendDecorators( | |
| 23 | decor.Name("ETA: "), | |
| 24 | decor.OnComplete( | |
| 25 | decor.AverageETA(decor.ET_STYLE_GO), "done", | |
| 26 | ), | |
| 27 | ), | |
| 28 | ) | |
| 29 | // simulating some work | |
| 30 | max := 100 * time.Millisecond | |
| 31 | for i := 0; i < total; i++ { | |
| 32 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) | |
| 33 | bar.Increment() | |
| 34 | } | |
| 35 | // wait for our bar to complete and flush | |
| 36 | p.Wait() | |
| 37 | } | |
| 38 | ||
| 39 | func nlBarFiller(filler mpb.BarFiller) mpb.BarFiller { | |
| 40 | return mpb.BarFillerFunc(func(w io.Writer, width int, s decor.Statistics) { | |
| 41 | filler.Fill(w, width, s) | |
| 42 | w.Write([]byte("\n")) | |
| 43 | }) | |
| 44 | } |