Codebase list golang-github-vbauerster-mpb / b51492b
Bar complete example Vladimir Bauer 8 years ago
2 changed file(s) with 54 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
0 package main
1
2 import (
3 "fmt"
4 "math/rand"
5 "sync"
6 "time"
7
8 "github.com/vbauerster/mpb"
9 "github.com/vbauerster/mpb/decor"
10 )
11
12 func init() {
13 rand.Seed(time.Now().UnixNano())
14 }
15
16 func main() {
17 var wg sync.WaitGroup
18 p := mpb.New(mpb.WithWaitGroup(&wg))
19 total := 100
20 numBars := 3
21 wg.Add(numBars)
22
23 for i := 0; i < numBars; i++ {
24 var name string
25 if i != 1 {
26 name = fmt.Sprintf("Bar#%d:", i)
27 }
28 b := p.AddBar(int64(total), mpb.BarID(i),
29 mpb.PrependDecorators(
30 decor.StaticName(name, 0, decor.DwidthSync|decor.DidentRight),
31 decor.ETA(4, decor.DSyncSpace),
32 ),
33 mpb.AppendDecorators(
34 decor.Percentage(5, 0),
35 ),
36 )
37 go func() {
38 defer wg.Done()
39 for i := 0; i < total; i++ {
40 time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10)
41 b.Increment()
42 if b.ID() == 1 && i == 42 {
43 b.Complete()
44 return
45 }
46 }
47 }()
48 }
49
50 p.Stop()
51 fmt.Println("stop")
52 }
3939 for i := 0; i < total; i++ {
4040 time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10)
4141 b.Increment()
42 if b.ID() == 1 && i >= 42 {
42 if b.ID() == 1 && i == 42 {
4343 p.RemoveBar(b)
4444 return
4545 }