IOT>> mpb>> 返回
项目作者: vbauerster

项目描述 :
multi progress bar for Go cli applications
高级语言: Go
项目地址: git://github.com/vbauerster/mpb.git
创建时间: 2016-12-14T11:56:29Z
项目社区:https://github.com/vbauerster/mpb

开源协议:The Unlicense

下载


Multi Progress Bar

GoDoc
Test status
Lint status

mpb is a Go lib for rendering progress bars in terminal applications.

Features

  • Multiple Bars: Multiple progress bars are supported
  • Dynamic Total: Set total while bar is running
  • Dynamic Add/Remove: Dynamically add or remove bars
  • Cancellation: Cancel whole rendering process
  • Predefined Decorators: Elapsed time, ewma based ETA, Percentage, Bytes counter
  • Decorator’s width sync: Synchronized decorator’s width among multiple bars

Usage

Rendering single bar

  1. package main
  2. import (
  3. "math/rand"
  4. "time"
  5. "github.com/vbauerster/mpb/v8"
  6. "github.com/vbauerster/mpb/v8/decor"
  7. )
  8. func main() {
  9. // initialize progress container, with custom width
  10. p := mpb.New(mpb.WithWidth(64))
  11. total := 100
  12. name := "Single Bar:"
  13. // create a single bar, which will inherit container's width
  14. bar := p.New(int64(total),
  15. // BarFillerBuilder with custom style
  16. mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟"),
  17. mpb.PrependDecorators(
  18. // display our name with one space on the right
  19. decor.Name(name, decor.WC{C: decor.DindentRight | decor.DextraSpace}),
  20. // replace ETA decorator with "done" message, OnComplete event
  21. decor.OnComplete(decor.AverageETA(decor.ET_STYLE_GO), "done"),
  22. ),
  23. mpb.AppendDecorators(decor.Percentage()),
  24. )
  25. // simulating some work
  26. max := 100 * time.Millisecond
  27. for i := 0; i < total; i++ {
  28. time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
  29. bar.Increment()
  30. }
  31. // wait for our bar to complete and flush
  32. p.Wait()
  33. }

Rendering multiple bars

  1. var wg sync.WaitGroup
  2. // passed wg will be accounted at p.Wait() call
  3. p := mpb.New(mpb.WithWaitGroup(&wg))
  4. total, numBars := 100, 3
  5. wg.Add(numBars)
  6. for i := 0; i < numBars; i++ {
  7. name := fmt.Sprintf("Bar#%d:", i)
  8. bar := p.AddBar(int64(total),
  9. mpb.PrependDecorators(
  10. // simple name decorator
  11. decor.Name(name),
  12. // decor.DSyncWidth bit enables column width synchronization
  13. decor.Percentage(decor.WCSyncSpace),
  14. ),
  15. mpb.AppendDecorators(
  16. // replace ETA decorator with "done" message, OnComplete event
  17. decor.OnComplete(
  18. // ETA decorator with ewma age of 30
  19. decor.EwmaETA(decor.ET_STYLE_GO, 30, decor.WCSyncWidth), "done",
  20. ),
  21. ),
  22. )
  23. // simulating some work
  24. go func() {
  25. defer wg.Done()
  26. rng := rand.New(rand.NewSource(time.Now().UnixNano()))
  27. max := 100 * time.Millisecond
  28. for i := 0; i < total; i++ {
  29. // start variable is solely for EWMA calculation
  30. // EWMA's unit of measure is an iteration's duration
  31. start := time.Now()
  32. time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
  33. // we need to call EwmaIncrement to fulfill ewma decorator's contract
  34. bar.EwmaIncrement(time.Since(start))
  35. }
  36. }()
  37. }
  38. // wait for passed wg and for all bars to complete and flush
  39. p.Wait()

Dynamic total

dynamic total

Complex example

complex

Bytes counters

byte counters