Codebase list golang-github-vbauerster-mpb / a439b16
single bar example Vladimir Bauer 9 years ago
3 changed file(s) with 34 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
+0
-37
example/interrupt/main.go less more
0 package main
1
2 import (
3 "fmt"
4 "math/rand"
5 "time"
6
7 "github.com/vbauerster/mpb"
8 )
9
10 const (
11 totalItem = 100
12 maxBlockSize = 14
13 )
14
15 func main() {
16 decor := func(s *mpb.Statistics) string {
17 str := fmt.Sprintf("%d/%d", s.Current, s.Total)
18 return fmt.Sprintf("%-7s", str)
19 }
20
21 p := mpb.New()
22 bar := p.AddBar(totalItem).AppendETA().PrependFunc(decor)
23
24 blockSize := rand.Intn(maxBlockSize) + 1
25 for i := 0; bar.InProgress(); i++ {
26 time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
27 bar.Incr(blockSize)
28 if bar.Current() > 42 && p.RemoveBar(bar) {
29 break
30 }
31 blockSize = rand.Intn(maxBlockSize) + 1
32 }
33
34 p.Stop()
35 fmt.Println("stop")
36 }
(New empty file)
0 package main
1
2 import (
3 "fmt"
4 "math/rand"
5 "time"
6
7 "github.com/vbauerster/mpb"
8 )
9
10 const (
11 totalItem = 100
12 maxBlockSize = 10
13 )
14
15 func main() {
16
17 name := "Single:"
18 p := mpb.New()
19 bar := p.AddBar(totalItem).
20 PrependName(name, 0).
21 AppendPercentage().
22 TrimRightSpace()
23
24 blockSize := rand.Intn(maxBlockSize) + 1
25 for i := 0; i < totalItem; i++ {
26 time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
27 bar.Incr(1)
28 blockSize = rand.Intn(maxBlockSize) + 1
29 }
30
31 p.Stop()
32 fmt.Println("stop")
33 }