-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathmain.go
More file actions
50 lines (42 loc) · 1.04 KB
/
Copy pathmain.go
File metadata and controls
50 lines (42 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"io"
"math/rand"
"time"
"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
)
func main() {
p := mpb.New(mpb.WithWidth(64))
// new bar with 'trigger complete event' disabled, because total is zero
bar := p.AddBar(0,
mpb.PrependDecorators(decor.Counters(decor.SizeB1024(0), "% .1f / % .1f")),
mpb.AppendDecorators(decor.Percentage()),
)
var written int64
maxSleep := 100 * time.Millisecond
read := makeStream(200)
for {
n, err := read()
if err == io.EOF {
break
}
written += int64(n)
// following call is not required, it's called to show some progress instead of an empty bar
bar.SetTotal(written+1024, false)
// increment method won't trigger completion because bar was constructed with total = 0
bar.IncrBy(n)
time.Sleep(time.Duration(rand.Intn(10)+1) * maxSleep / 10)
}
bar.SetTotal(written, true)
p.Wait()
}
func makeStream(limit int) func() (int, error) {
return func() (int, error) {
if limit <= 0 {
return 0, io.EOF
}
limit--
return rand.Intn(1024) + 1, nil
}
}