@@ -3,6 +3,7 @@ package mpb_test
33import (
44 "fmt"
55 "math/rand"
6+ "sync"
67 "time"
78 "unicode/utf8"
89
@@ -52,35 +53,61 @@ func ExampleBar_InProgress() {
5253func ExampleBar_PrependFunc () {
5354 decor := func (s * mpb.Statistics , myWidth chan <- int , maxWidth <- chan int ) string {
5455 str := fmt .Sprintf ("%3d/%3d" , s .Current , s .Total )
56+ // send width to Progress' goroutine
5557 myWidth <- utf8 .RuneCountInString (str )
58+ // receive max width
5659 max := <- maxWidth
5760 return fmt .Sprintf (fmt .Sprintf ("%%%ds" , max + 1 ), str )
5861 }
5962
6063 totalItem := 100
64+ var wg sync.WaitGroup
6165 p := mpb .New ()
62- bar := p .AddBar (int64 (totalItem )).PrependFunc (decor )
63-
64- for i := 0 ; i < totalItem ; i ++ {
65- bar .Incr (1 ) // increment progress bar
66- time .Sleep (time .Duration (rand .Intn (100 )) * time .Millisecond )
66+ wg .Add (3 ) // add wg delta
67+ for i := 0 ; i < 3 ; i ++ {
68+ name := fmt .Sprintf ("Bar#%d:" , i )
69+ bar := p .AddBar (int64 (totalItem )).
70+ PrependName (name , len (name ), 0 ).
71+ PrependFunc (decor )
72+ go func () {
73+ defer wg .Done ()
74+ for i := 0 ; i < totalItem ; i ++ {
75+ bar .Incr (1 )
76+ time .Sleep (time .Duration (rand .Intn (totalItem )) * time .Millisecond )
77+ }
78+ }()
6779 }
80+ wg .Wait () // Wait for goroutines to finish
81+ p .Stop () // Stop mpb's rendering goroutine
6882}
6983
7084func ExampleBar_AppendFunc () {
7185 decor := func (s * mpb.Statistics , myWidth chan <- int , maxWidth <- chan int ) string {
7286 str := fmt .Sprintf ("%3d/%3d" , s .Current , s .Total )
87+ // send width to Progress' goroutine
7388 myWidth <- utf8 .RuneCountInString (str )
89+ // receive max width
7490 max := <- maxWidth
7591 return fmt .Sprintf (fmt .Sprintf ("%%%ds" , max + 1 ), str )
7692 }
7793
7894 totalItem := 100
95+ var wg sync.WaitGroup
7996 p := mpb .New ()
80- bar := p .AddBar (int64 (totalItem )).AppendFunc (decor )
81-
82- for i := 0 ; i < totalItem ; i ++ {
83- bar .Incr (1 ) // increment progress bar
84- time .Sleep (time .Duration (rand .Intn (100 )) * time .Millisecond )
97+ wg .Add (3 ) // add wg delta
98+ for i := 0 ; i < 3 ; i ++ {
99+ name := fmt .Sprintf ("Bar#%d:" , i )
100+ bar := p .AddBar (int64 (totalItem )).
101+ PrependName (name , len (name ), 0 ).
102+ AppendFunc (decor )
103+ go func () {
104+ defer wg .Done ()
105+ for i := 0 ; i < totalItem ; i ++ {
106+ bar .Incr (1 )
107+ time .Sleep (time .Duration (rand .Intn (totalItem )) * time .Millisecond )
108+ }
109+ }()
85110 }
111+ wg .Wait () // Wait for goroutines to finish
112+ p .Stop () // Stop mpb's rendering goroutine
86113}
0 commit comments