| 28 | 28 |
To get the package, execute:
|
| 29 | 29 |
|
| 30 | 30 |
```sh
|
| 31 | |
go get gopkg.in/vbauerster/mpb.v2
|
|
31 |
go get gopkg.in/vbauerster/mpb.v3
|
| 32 | 32 |
```
|
| 33 | 33 |
|
| 34 | 34 |
## Usage
|
|
| 36 | 36 |
Following is the simplest use case:
|
| 37 | 37 |
|
| 38 | 38 |
```go
|
| 39 | |
// Star mpb's rendering goroutine.
|
| 40 | |
p := mpb.New()
|
| 41 | |
// Set custom width for every bar, which mpb will render
|
| 42 | |
// The default one is 80
|
| 43 | |
p.SetWidth(100)
|
| 44 | |
// Set custom format for every bar, the default one is "[=>-]"
|
| 45 | |
p.Format("╢▌▌░╟")
|
| 46 | |
// Set custom refresh rate, the default one is 100 ms
|
| 47 | |
p.RefreshRate(120 * time.Millisecond)
|
|
39 |
p := mpb.New(
|
|
40 |
// override default (80) width
|
|
41 |
mpb.WithWidth(100),
|
|
42 |
// override default "[=>-]" format
|
|
43 |
mpb.WithFormat("╢▌▌░╟"),
|
|
44 |
// override default 100ms refresh rate
|
|
45 |
mpb.WithRefreshRate(120*time.Millisecond),
|
|
46 |
)
|
| 48 | 47 |
|
|
48 |
total := 100
|
|
49 |
name := "Single Bar:"
|
| 49 | 50 |
// Add a bar. You're not limited to just one bar, add many if you need.
|
| 50 | |
bar := p.AddBar(100).PrependName("Single Bar:", 0, 0).AppendPercentage(5, 0)
|
|
51 |
bar := p.AddBar(int64(total),
|
|
52 |
// Prepending decorators
|
|
53 |
mpb.PrependDecorators(
|
|
54 |
// Name decorator with minWidth and no width sync options
|
|
55 |
decor.Name(name, len(name), 0),
|
|
56 |
// ETA decorator with minWidth and width sync options DwidthSync|DextraSpace
|
|
57 |
decor.ETA(4, decor.DSyncSpace),
|
|
58 |
),
|
|
59 |
// Appending decorators
|
|
60 |
mpb.AppendDecorators(
|
|
61 |
// Percentage decorator with minWidth and no width sync options
|
|
62 |
decor.Percentage(5, 0),
|
|
63 |
),
|
|
64 |
)
|
| 51 | 65 |
|
| 52 | |
for i := 0; i < 100; i++ {
|
|
66 |
for i := 0; i < total; i++ {
|
| 53 | 67 |
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
|
| 54 | 68 |
bar.Incr(1) // increment progress bar
|
| 55 | 69 |
}
|
| 56 | 70 |
|
| 57 | |
// Don't forget to stop mpb's rendering goroutine
|
| 58 | 71 |
p.Stop()
|
| 59 | 72 |
```
|
| 60 | 73 |
|
| 61 | |
Running [this](example/singleBar/main.go), will produce:
|
|
74 |
Running [this](examples/singleBar/main.go), will produce:
|
| 62 | 75 |
|
| 63 | |

|
|
76 |

|
| 64 | 77 |
|
| 65 | 78 |
However **mpb** was designed with concurrency in mind. Each new bar renders in its
|
| 66 | 79 |
own goroutine, therefore adding multiple bars is easy and safe:
|
| 67 | 80 |
|
| 68 | 81 |
```go
|
|
82 |
p := mpb.New()
|
|
83 |
total := 100
|
|
84 |
numBars := 3
|
| 69 | 85 |
var wg sync.WaitGroup
|
| 70 | |
p := mpb.New()
|
| 71 | |
wg.Add(3) // add wg delta
|
| 72 | |
for i := 0; i < 3; i++ {
|
|
86 |
wg.Add(numBars)
|
|
87 |
|
|
88 |
for i := 0; i < numBars; i++ {
|
| 73 | 89 |
name := fmt.Sprintf("Bar#%d:", i)
|
| 74 | |
bar := p.AddBar(100).
|
| 75 | |
PrependName(name, len(name), 0).
|
| 76 | |
// Prepend Percentage decorator and sync width
|
| 77 | |
PrependPercentage(3, mpb.DwidthSync|mpb.DextraSpace).
|
| 78 | |
// Append ETA and don't sync width
|
| 79 | |
AppendETA(2, 0)
|
|
90 |
bar := p.AddBar(int64(total),
|
|
91 |
mpb.PrependDecorators(
|
|
92 |
decor.Name(name, len(name), 0),
|
|
93 |
decor.Percentage(3, decor.DSyncSpace),
|
|
94 |
),
|
|
95 |
mpb.AppendDecorators(
|
|
96 |
decor.ETA(2, 0),
|
|
97 |
),
|
|
98 |
)
|
| 80 | 99 |
go func() {
|
| 81 | 100 |
defer wg.Done()
|
| 82 | |
// you can p.AddBar() here, but ordering will be non deterministic
|
| 83 | |
// if you still need p.AddBar() here and maintain ordering, use
|
| 84 | |
// (*mpb.Progress).BeforeRenderFunc(f mpb.BeforeRender)
|
| 85 | |
for i := 0; i < 100; i++ {
|
|
101 |
for i := 0; i < total; i++ {
|
| 86 | 102 |
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
|
| 87 | 103 |
bar.Incr(1)
|
| 88 | 104 |
}
|
|
| 92 | 108 |
p.Stop() // Stop mpb's rendering goroutine
|
| 93 | 109 |
```
|
| 94 | 110 |
|
| 95 | |

|
|
111 |

|
| 96 | 112 |
|
| 97 | |
The source code: [example/simple/main.go](example/simple/main.go)
|
|
113 |
The source code: [examples/simple/main.go](examples/simple/main.go)
|
| 98 | 114 |
|
| 99 | 115 |
### Cancel
|
| 100 | 116 |
|
| 101 | |
To cancel use either
|
| 102 | |
[WithCancel](https://godoc.org/github.com/vbauerster/mpb#Progress.WithCancel) or
|
| 103 | |
[WithContext](https://godoc.org/github.com/vbauerster/mpb#Progress.WithContext)
|
| 104 | |
method. The last one requires Go 1.7
|
|
117 |

|
| 105 | 118 |
|
| 106 | |

|
| 107 | |
|
| 108 | |
The source code: [example/cancel/main.go](example/cancel/main.go)
|
|
119 |
The source code: [examples/cancel/main.go](examples/cancel/main.go)
|
| 109 | 120 |
|
| 110 | 121 |
### Removing bar
|
| 111 | 122 |
|
| 112 | |

|
|
123 |

|
| 113 | 124 |
|
| 114 | |
The source code: [example/remove/main.go](example/remove/main.go)
|
|
125 |
The source code: [examples/remove/main.go](examples/remove/main.go)
|
| 115 | 126 |
|
| 116 | 127 |
### Sorting bars by progress
|
| 117 | 128 |
|
| 118 | |

|
|
129 |

|
| 119 | 130 |
|
| 120 | |
The source code: [example/sort/main.go](example/sort/main.go)
|
|
131 |
The source code: [examples/sort/main.go](examples/sort/main.go)
|
| 121 | 132 |
|
| 122 | 133 |
### Resizing bars on terminal width change
|
| 123 | 134 |
|
| 124 | |

|
|
135 |

|
| 125 | 136 |
|
| 126 | |
The source code: [example/prependETA/main.go](example/prependETA/main.go)
|
|
137 |
The source code: [examples/prependETA/main.go](examples/prependETA/main.go)
|
| 127 | 138 |
|
| 128 | 139 |
### Multiple io
|
| 129 | 140 |
|
| 130 | |

|
|
141 |

|
| 131 | 142 |
|
| 132 | |
The source code: [example/io/multiple/main.go](example/io/multiple/main.go)
|
| 133 | |
|
| 134 | |
### Custom Decorators
|
| 135 | |
|
| 136 | |
Refer to godoc [example](https://godoc.org/github.com/vbauerster/mpb#example-Bar-PrependFunc).
|
|
143 |
The source code: [examples/io/multiple/main.go](examples/io/multiple/main.go)
|
| 137 | 144 |
|
| 138 | 145 |
## License
|
| 139 | 146 |
|