prefer *Progress.New in examples
Vladimir Bauer
4 years ago
| 35 | 35 | |
| 36 | 36 | total := 100 |
| 37 | 37 | name := "Single Bar:" |
| 38 | // adding a single bar, which will inherit container's width | |
| 39 | bar := p.Add(int64(total), | |
| 40 | // progress bar filler with customized style | |
| 41 | mpb.NewBarFiller(mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟")), | |
| 38 | // create a single bar, which will inherit container's width | |
| 39 | bar := p.New(int64(total), | |
| 40 | // BarFillerBuilder with custom style | |
| 41 | mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟"), | |
| 42 | 42 | mpb.PrependDecorators( |
| 43 | 43 | // display our name with one space on the right |
| 44 | 44 | decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}), |
| 12 | 12 | p := mpb.New() |
| 13 | 13 | |
| 14 | 14 | total := 100 |
| 15 | bar := p.Add(int64(total), nil, | |
| 15 | bar := p.New(int64(total), | |
| 16 | mpb.NopStyle(), // make main bar style nop, so there are just decorators | |
| 17 | mpb.BarExtender(extended(mpb.BarStyle())), // extend wtih normal bar on the next line | |
| 16 | 18 | mpb.PrependDecorators( |
| 17 | 19 | decor.Name("Percentage: "), |
| 18 | 20 | decor.NewPercentage("%d"), |
| 23 | 25 | decor.AverageETA(decor.ET_STYLE_GO), "done", |
| 24 | 26 | ), |
| 25 | 27 | ), |
| 26 | mpb.BarExtender(nlBarFiller(mpb.NewBarFiller(mpb.BarStyle()))), | |
| 27 | 28 | ) |
| 28 | 29 | // simulating some work |
| 29 | 30 | max := 100 * time.Millisecond |
| 35 | 36 | p.Wait() |
| 36 | 37 | } |
| 37 | 38 | |
| 38 | func nlBarFiller(filler mpb.BarFiller) mpb.BarFiller { | |
| 39 | func extended(builder mpb.BarFillerBuilder) mpb.BarFiller { | |
| 40 | filler := builder.Build() | |
| 39 | 41 | return mpb.BarFillerFunc(func(w io.Writer, reqWidth int, st decor.Statistics) { |
| 40 | 42 | filler.Fill(w, reqWidth, st) |
| 41 | 43 | w.Write([]byte("\n")) |
| 18 | 18 | mpb.WithRefreshRate(180*time.Millisecond), |
| 19 | 19 | ) |
| 20 | 20 | |
| 21 | bar := p.Add(total, | |
| 22 | mpb.NewBarFiller(mpb.BarStyle().Rbound("|")), | |
| 21 | bar := p.New(total, | |
| 22 | mpb.BarStyle().Rbound("|"), | |
| 23 | 23 | mpb.PrependDecorators( |
| 24 | 24 | decor.CountersKibiByte("% .2f / % .2f"), |
| 25 | 25 | ), |
| 19 | 19 | bs.Tip("\u001b[0m⛵\u001b[36;1m") |
| 20 | 20 | bs.Padding("_") |
| 21 | 21 | bs.Rbound("\u001b[0m]") |
| 22 | bar := p.Add(int64(total), | |
| 23 | mpb.NewBarFiller(bs), | |
| 22 | bar := p.New(int64(total), bs, | |
| 24 | 23 | mpb.PrependDecorators(decor.Name(name)), |
| 25 | 24 | mpb.AppendDecorators(decor.Percentage()), |
| 26 | 25 | ) |
| 11 | 11 | |
| 12 | 12 | func main() { |
| 13 | 13 | var wg sync.WaitGroup |
| 14 | // pass &wg (optional), so p will wait for it eventually | |
| 15 | p := mpb.New(mpb.WithWaitGroup(&wg)) | |
| 14 | p := mpb.New( | |
| 15 | // passing &wg will make p.Wait() call wait for it first | |
| 16 | mpb.WithWaitGroup(&wg), | |
| 17 | ) | |
| 16 | 18 | total, numBars := 100, 3 |
| 17 | 19 | wg.Add(numBars) |
| 18 | 20 | |
| 19 | 21 | for i := 0; i < numBars; i++ { |
| 20 | 22 | name := fmt.Sprintf("Bar#%d:", i) |
| 21 | bs := mpb.BarStyle() | |
| 22 | if i == 1 { | |
| 23 | // reverse Bar#1 | |
| 24 | bs = bs.Tip("<").Reverse() | |
| 25 | } | |
| 26 | bar := p.Add(int64(total), | |
| 27 | mpb.NewBarFiller(bs), | |
| 23 | bar := p.New(int64(total), condBuilder(i == 1), | |
| 28 | 24 | mpb.PrependDecorators( |
| 29 | 25 | // simple name decorator |
| 30 | 26 | decor.Name(name), |
| 55 | 51 | } |
| 56 | 52 | }() |
| 57 | 53 | } |
| 58 | // Waiting for passed &wg and for all bars to complete and flush | |
| 54 | // wait for all bars to complete and flush | |
| 59 | 55 | p.Wait() |
| 60 | 56 | } |
| 57 | ||
| 58 | func condBuilder(cond bool) mpb.BarFillerBuilderFunc { | |
| 59 | return mpb.BarFillerBuilderFunc(func() mpb.BarFiller { | |
| 60 | bs := mpb.BarStyle() | |
| 61 | if cond { | |
| 62 | // reverse Bar on cond | |
| 63 | bs = bs.Tip("<").Reverse() | |
| 64 | } | |
| 65 | return bs.Build() | |
| 66 | }) | |
| 67 | } | |
| 13 | 13 | |
| 14 | 14 | total := 100 |
| 15 | 15 | name := "Single Bar:" |
| 16 | // adding a single bar, which will inherit container's width | |
| 17 | bar := p.Add(int64(total), | |
| 18 | // progress bar filler with customized style | |
| 19 | mpb.NewBarFiller(mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟")), | |
| 16 | // create a single bar, which will inherit container's width | |
| 17 | bar := p.New(int64(total), | |
| 18 | // BarFillerBuilder with custom style | |
| 19 | mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟"), | |
| 20 | 20 | mpb.PrependDecorators( |
| 21 | 21 | // display our name with one space on the right |
| 22 | 22 | decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}), |
| 13 | 13 | |
| 14 | 14 | total := 100 |
| 15 | 15 | name := "Single Bar:" |
| 16 | bar := p.Add(int64(total), | |
| 17 | mpb.NewBarFiller(mpb.BarStyle().Tip(`-`, `\`, `|`, `/`)), | |
| 16 | bar := p.New(int64(total), | |
| 17 | mpb.BarStyle().Tip(`-`, `\`, `|`, `/`), | |
| 18 | 18 | mpb.PrependDecorators(decor.Name(name)), |
| 19 | 19 | mpb.AppendDecorators(decor.Percentage()), |
| 20 | 20 | ) |
| 12 | 12 | func main() { |
| 13 | 13 | var wg sync.WaitGroup |
| 14 | 14 | p := mpb.New( |
| 15 | // passing &wg will make p.Wait() call wait for it first | |
| 15 | 16 | mpb.WithWaitGroup(&wg), |
| 16 | mpb.WithWidth(14), | |
| 17 | mpb.WithWidth(16), | |
| 17 | 18 | ) |
| 18 | 19 | total, numBars := 101, 3 |
| 19 | 20 | wg.Add(numBars) |
| 20 | 21 | |
| 21 | spinnerStyle := []string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"} | |
| 22 | ||
| 23 | 22 | for i := 0; i < numBars; i++ { |
| 24 | 23 | name := fmt.Sprintf("Bar#%d:", i) |
| 25 | var bar *mpb.Bar | |
| 26 | if i == 0 { | |
| 27 | bar = p.Add(int64(total), | |
| 28 | mpb.NewBarFiller(mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟")), | |
| 29 | mpb.PrependDecorators( | |
| 30 | // simple name decorator | |
| 31 | decor.Name(name), | |
| 24 | bar := p.New(int64(total), condBuilder(i != 0), | |
| 25 | mpb.PrependDecorators( | |
| 26 | // simple name decorator | |
| 27 | decor.Name(name), | |
| 28 | ), | |
| 29 | mpb.AppendDecorators( | |
| 30 | // replace ETA decorator with "done" message, OnComplete event | |
| 31 | decor.OnComplete( | |
| 32 | // ETA decorator with ewma age of 60 | |
| 33 | decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", | |
| 32 | 34 | ), |
| 33 | mpb.AppendDecorators( | |
| 34 | // replace ETA decorator with "done" message, OnComplete event | |
| 35 | decor.OnComplete( | |
| 36 | // ETA decorator with ewma age of 60 | |
| 37 | decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", | |
| 38 | ), | |
| 39 | ), | |
| 40 | ) | |
| 41 | } else { | |
| 42 | bar = p.Add(int64(total), | |
| 43 | mpb.NewBarFiller(mpb.SpinnerStyle(spinnerStyle...)), | |
| 44 | mpb.PrependDecorators( | |
| 45 | // simple name decorator | |
| 46 | decor.Name(name), | |
| 47 | ), | |
| 48 | mpb.AppendDecorators( | |
| 49 | // replace ETA decorator with "done" message, OnComplete event | |
| 50 | decor.OnComplete( | |
| 51 | // ETA decorator with ewma age of 60 | |
| 52 | decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", | |
| 53 | ), | |
| 54 | ), | |
| 55 | ) | |
| 56 | } | |
| 57 | ||
| 35 | ), | |
| 36 | ) | |
| 58 | 37 | // simulating some work |
| 59 | 38 | go func() { |
| 60 | 39 | defer wg.Done() |
| 74 | 53 | // wait for all bars to complete and flush |
| 75 | 54 | p.Wait() |
| 76 | 55 | } |
| 56 | ||
| 57 | func condBuilder(cond bool) mpb.BarFillerBuilderFunc { | |
| 58 | return mpb.BarFillerBuilderFunc(func() mpb.BarFiller { | |
| 59 | if cond { | |
| 60 | // spinner Bar on cond | |
| 61 | frames := []string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"} | |
| 62 | return mpb.SpinnerStyle(frames...).Build() | |
| 63 | } | |
| 64 | return mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟").Build() | |
| 65 | }) | |
| 66 | } | |
| 13 | 13 | |
| 14 | 14 | total := 100 |
| 15 | 15 | name := "Single Bar:" |
| 16 | bar := p.Add(int64(total), | |
| 17 | mpb.NewBarFiller(mpb.BarStyle().TipOnComplete(">")), | |
| 16 | bar := p.New(int64(total), | |
| 17 | mpb.BarStyle().TipOnComplete(">"), | |
| 18 | 18 | mpb.PrependDecorators(decor.Name(name)), |
| 19 | 19 | mpb.AppendDecorators(decor.Percentage()), |
| 20 | 20 | ) |
| 16 | 16 | |
| 17 | 17 | total := 100 |
| 18 | 18 | name := "Single Bar:" |
| 19 | // adding a single bar, which will inherit container's width | |
| 20 | bar := p.Add(int64(total), | |
| 21 | // progress bar filler with customized style | |
| 22 | mpb.NewBarFiller(mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟")), | |
| 19 | // create a single bar, which will inherit container's width | |
| 20 | bar := p.New(int64(total), | |
| 21 | // BarFillerBuilder with custom style | |
| 22 | mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟"), | |
| 23 | 23 | mpb.PrependDecorators( |
| 24 | 24 | // display our name with one space on the right |
| 25 | 25 | decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}), |