Codebase list golang-github-vbauerster-mpb / ae251d6
more examples Vladimir Bauer 9 years ago
5 changed file(s) with 93 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
155155
156156 // String returns the string representation of the bar
157157 func (b *Bar) String() string {
158 // if b.IsStopped() {
159 // return "bar stopped"
160 // }
161158 bufCh := make(chan []byte)
162159 b.redrawRequestCh <- &redrawRequest{bufCh}
163160 return string(<-bufCh)
0 package main
1
2 import (
3 "fmt"
4 "math/rand"
5 "runtime"
6 "time"
7
8 "github.com/vbauerster/uiprogress"
9 )
10
11 const (
12 totalItem = 100
13 maxBlockSize = 10
14 )
15
16 func main() {
17 runtime.GOMAXPROCS(runtime.NumCPU())
18 decor := func(s *uiprogress.Statistics) string {
19 str := fmt.Sprintf("%d/%d", s.Completed, s.Total)
20 return fmt.Sprintf("%-7s", str)
21 }
22
23 p := uiprogress.New()
24 bar := p.AddBar(totalItem).AppendETA().PrependFunc(decor)
25
26 blockSize := rand.Intn(maxBlockSize) + 1
27 // Fallowing will hang, in order not to hang
28 // use !bar.IsCompleted in loop condition
29 // for i := 0; !bar.IsCompleted(); i += blockSize {
30 for i := 0; i < totalItem; i += blockSize {
31 time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
32 bar.Incr(blockSize)
33 blockSize = rand.Intn(maxBlockSize) + 1
34 }
35
36 p.WaitAndStop()
37 fmt.Println("stop")
38 }
5555 // time.Sleep(time.Second)
5656 // p.RemoveBar(bar2)
5757
58 p.Stop()
58 p.WaitAndStop()
59 bar2.Incr(2)
5960 fmt.Println("stop")
6061 // p.AddBar(1) // panic: send on closed channnel
6162 }
0 package main
1
2 import (
3 "fmt"
4 "math/rand"
5 "runtime"
6 "time"
7
8 "github.com/vbauerster/uiprogress"
9 )
10
11 const (
12 totalItem = 100
13 maxBlockSize = 20
14 )
15
16 func main() {
17 runtime.GOMAXPROCS(runtime.NumCPU())
18 decor := func(s *uiprogress.Statistics) string {
19 str := fmt.Sprintf("%d/%d", s.Completed, s.Total)
20 return fmt.Sprintf("%-7s", str)
21 }
22
23 p := uiprogress.New()
24 bar := p.AddBar(totalItem).AppendETA().PrependFunc(decor)
25
26 blockSize := rand.Intn(maxBlockSize) + 1
27 for i := 0; !bar.IsCompleted(); i += 1 {
28 time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
29 bar.Incr(1)
30 if i == 42 {
31 p.RemoveBar(bar)
32 }
33 blockSize = rand.Intn(maxBlockSize) + 1
34 }
35
36 p.WaitAndStop()
37 fmt.Println("stop")
38 }
22 import (
33 "fmt"
44 "math/rand"
5 "runtime"
56 "time"
67
78 "github.com/vbauerster/uiprogress"
89 )
910
1011 const (
11 totalItem = 1000
12 maxBlockSize = 20
12 totalItem = 100
13 maxBlockSize = 12
1314 )
1415
1516 func main() {
17 runtime.GOMAXPROCS(runtime.NumCPU())
18 decor := func(s *uiprogress.Statistics) string {
19 str := fmt.Sprintf("%d/%d", s.Completed, s.Total)
20 return fmt.Sprintf("%-7s", str)
21 }
22
1623 p := uiprogress.New()
17 bar := p.AddBar(totalItem) // Add a new bar
18
19 // optionally, append and prepend completion and elapsed time
20 bar.AppendETA()
21 // bar.PrependElapsed()
24 bar := p.AddBar(totalItem).AppendETA().PrependFunc(decor)
2225
2326 blockSize := rand.Intn(maxBlockSize) + 1
24 for i := 1; i <= totalItem; i += blockSize {
27 for i := 0; i < 100; i += 1 {
2528 time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
26 bar.Incr(blockSize)
29 bar.Incr(1)
2730 blockSize = rand.Intn(maxBlockSize) + 1
2831 }
2932
33 p.WaitAndStop()
3034 fmt.Println("stop")
31
32 p.Stop()
3335 }