Codebase list golang-github-vbauerster-mpb / 48dc86f
update example_test Vladimir Bauer 9 years ago
1 changed file(s) with 19 addition(s) and 77 deletion(s). Raw diff Collapse all Expand all
00 package mpb_test
11
22 import (
3 "fmt"
43 "math/rand"
5 "sync"
64 "time"
7 "unicode/utf8"
85
96 "github.com/vbauerster/mpb"
7 "github.com/vbauerster/mpb/decor"
108 )
119
1210 func Example() {
1311 // Star mpb's rendering goroutine.
14 p := mpb.New()
15 // Set custom width for every bar, which mpb will contain
16 // The default one is 80
17 p.SetWidth(100)
18 // Set custom format for every bar, the default one is "[=>-]"
19 p.Format("╢▌▌░╟")
20 // Set custom refresh rate, the default one is 100 ms
21 p.RefreshRate(120 * time.Millisecond)
12 p := mpb.New(
13 // override default (80) width
14 mpb.WithWidth(100),
15 // override default "[=>-]" format
16 mpb.WithFormat("╢▌▌░╟"),
17 // override default 100ms refresh rate
18 mpb.WithRefreshRate(120*time.Millisecond),
19 )
2220
21 total := 100
22 barName := "Single Bar:"
2323 // Add a bar. You're not limited to just one bar, add many if you need.
24 bar := p.AddBar(100).
25 PrependName("Single Bar:", 0, 0).
26 AppendPercentage(5, 0)
24 bar := p.AddBar(total,
25 mpb.PrependDecorators(
26 decor.Name(barName, 0, decor.DwidthSync|decor.DidentRight),
27 decor.ETA(4, decor.DSyncSpace),
28 ),
29 mpb.AppendDecorators(decor.Percentage(5, 0)),
30 )
2731
2832 for i := 0; i < 100; i++ {
2933 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
3640
3741 func ExampleBar_InProgress() {
3842 p := mpb.New()
39 bar := p.AddBar(100).AppendPercentage(5, 0)
43 bar := p.AddBar(100, mpb.AppendDecorators(decor.Percentage(5, 0)))
4044
4145 for bar.InProgress() {
4246 time.Sleep(time.Millisecond * 20)
4347 bar.Incr(1)
4448 }
4549 }
46
47 func ExampleBar_PrependFunc() {
48 decor := func(s *mpb.Statistics, myWidth chan<- int, maxWidth <-chan int) string {
49 str := fmt.Sprintf("%3d/%3d", s.Current, s.Total)
50 // send width to Progress' goroutine
51 myWidth <- utf8.RuneCountInString(str)
52 // receive max width
53 max := <-maxWidth
54 return fmt.Sprintf(fmt.Sprintf("%%%ds", max+1), str)
55 }
56
57 totalItem := 100
58 var wg sync.WaitGroup
59 p := mpb.New()
60 wg.Add(3) // add wg delta
61 for i := 0; i < 3; i++ {
62 name := fmt.Sprintf("Bar#%d:", i)
63 bar := p.AddBar(int64(totalItem)).
64 PrependName(name, len(name), 0).
65 PrependFunc(decor)
66 go func() {
67 defer wg.Done()
68 for i := 0; i < totalItem; i++ {
69 time.Sleep(time.Duration(rand.Intn(totalItem)) * time.Millisecond)
70 bar.Incr(1)
71 }
72 }()
73 }
74 wg.Wait() // Wait for goroutines to finish
75 p.Stop() // Stop mpb's rendering goroutine
76 }
77
78 func ExampleBar_AppendFunc() {
79 decor := func(s *mpb.Statistics, myWidth chan<- int, maxWidth <-chan int) string {
80 str := fmt.Sprintf("%3d/%3d", s.Current, s.Total)
81 // send width to Progress' goroutine
82 myWidth <- utf8.RuneCountInString(str)
83 // receive max width
84 max := <-maxWidth
85 return fmt.Sprintf(fmt.Sprintf("%%%ds", max+1), str)
86 }
87
88 totalItem := 100
89 var wg sync.WaitGroup
90 p := mpb.New()
91 wg.Add(3) // add wg delta
92 for i := 0; i < 3; i++ {
93 name := fmt.Sprintf("Bar#%d:", i)
94 bar := p.AddBar(int64(totalItem)).
95 PrependName(name, len(name), 0).
96 AppendFunc(decor)
97 go func() {
98 defer wg.Done()
99 for i := 0; i < totalItem; i++ {
100 time.Sleep(time.Duration(rand.Intn(totalItem)) * time.Millisecond)
101 bar.Incr(1)
102 }
103 }()
104 }
105 wg.Wait() // Wait for goroutines to finish
106 p.Stop() // Stop mpb's rendering goroutine
107 }