Codebase list golang-github-vbauerster-mpb / d46f930
godoc update Vladimir Bauer 8 years ago
5 changed file(s) with 51 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
3838
3939 total := 100
4040 name := "Single Bar:"
41 // Adding a single bar
41 // adding a single bar
4242 bar := p.AddBar(int64(total),
4343 mpb.PrependDecorators(
4444 // Display our static name with one space on the right
5252 ),
5353 )
5454
55 // Simulating some work
55 // simulating some work
5656 max := 100 * time.Millisecond
5757 for i := 0; i < total; i++ {
5858 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
59 // Increment by 1 (there is bar.IncrBy(int) method, if needed)
59 // increment by 1 (there is bar.IncrBy(int) method, if needed)
6060 bar.Increment()
6161 }
62 // Wait for our bar to complete and flush
62 // wait for our bar to complete and flush
6363 p.Wait()
6464 ```
6565
8484 decor.OnComplete(decor.ETA(3, 0), "done!", 0, 0),
8585 ),
8686 )
87 // Simulating some work
87 // simulating some work
8888 go func() {
8989 defer wg.Done()
9090 max := 100 * time.Millisecond
9494 }
9595 }()
9696 }
97 // First wait for provided wg,
98 // then wait for all bars to complete and flush.
97 // first wait for provided wg, then
98 // wait for all bars to complete and flush
9999 p.Wait()
100100 ```
101101
00 package mpb_test
11
22 import (
3 "io"
34 "math/rand"
5 "net/http"
46 "time"
57
68 "github.com/vbauerster/mpb"
1921
2022 total := 100
2123 name := "Single Bar:"
22 // Add a bar
23 // You're not limited to just a single bar, add as many as you need
24 // adding a single bar
2425 bar := p.AddBar(int64(total),
25 // Prepending decorators
2626 mpb.PrependDecorators(
27 // StaticName decorator with minWidth and no extra config
28 // If you need to change name while rendering, use DynamicName
29 decor.StaticName(name, len(name), 0),
30 // ETA decorator with minWidth and no extra config
31 decor.ETA(4, 0),
27 // Display our static name with one space on the right
28 decor.StaticName(name, len(name)+1, decor.DidentRight),
29 // ETA decorator with width reservation of 3 runes
30 decor.ETA(3, 0),
3231 ),
33 // Appending decorators
3432 mpb.AppendDecorators(
35 // Percentage decorator with minWidth and no extra config
33 // Percentage decorator with width reservation of 5 runes
3634 decor.Percentage(5, 0),
3735 ),
3836 )
3937
38 // simulating some work
4039 max := 100 * time.Millisecond
4140 for i := 0; i < total; i++ {
4241 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
42 // increment by 1 (there is bar.IncrBy(int) method, if needed)
4343 bar.Increment()
4444 }
45 // Wait for all bars to complete
45 // wait for our bar to complete and flush
4646 p.Wait()
4747 }
4848
5858
5959 p.Wait()
6060 }
61
62 func ExampleBar_ProxyReader() {
63 p := mpb.New()
64 // make http get request
65 resp, _ := http.Get(url)
66 if err != nil {
67 return err
68 }
69 defer resp.Body.Close()
70
71 // Assuming ContentLength > 0
72 bar := p.AddBar(resp.ContentLength,
73 decor.CountersKibiByte("%6.1f / %6.1f", 12, 0),
74 )
75
76 // create proxy reader
77 reader := bar.ProxyReader(resp.Body)
78
79 // and copy from reader, ignoring errors
80 io.Copy(dest, reader)
81
82 p.Wait()
83 }
4040 bar := p.AddBar(size,
4141 mpb.PrependDecorators(
4242 decor.CountersKibiByte("% 6.1f / % 6.1f", 18, 0),
43 ))
43 ),
44 )
4445
4546 // create proxy reader
4647 reader := bar.ProxyReader(resp.Body)
4849 // and copy from reader, ignoring errors
4950 io.Copy(dest, reader)
5051
51 p.Wait() // if you omit this line, rendering bars goroutine will quit early
52 fmt.Println("done")
52 p.Wait()
5353 }
2929 decor.Percentage(0, decor.DwidthSync),
3030 ),
3131 mpb.AppendDecorators(
32 // Replace our ETA decorator with "done!", on bar completion event
32 // replace our ETA decorator with "done!", on bar completion event
3333 decor.OnComplete(decor.ETA(3, 0), "done!", 0, 0),
3434 ),
3535 )
36 // Simulating some work
36 // simulating some work
3737 go func() {
3838 defer wg.Done()
3939 max := 100 * time.Millisecond
4343 }
4444 }()
4545 }
46 // First wait for provided wg,
47 // then wait for all bars to complete and flush.
46 // first wait for provided wg, then
47 // wait for all bars to complete and flush
4848 p.Wait()
4949 }
1919
2020 total := 100
2121 name := "Single Bar:"
22 // Adding a single bar
22 // adding a single bar
2323 bar := p.AddBar(int64(total),
2424 mpb.PrependDecorators(
2525 // Display our static name with one space on the right
3333 ),
3434 )
3535
36 // Simulating some work
36 // simulating some work
3737 max := 100 * time.Millisecond
3838 for i := 0; i < total; i++ {
3939 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
4040 // Increment by 1 (there is bar.IncrBy(int) method, if needed)
4141 bar.Increment()
4242 }
43 // Wait for our bar to complete and flush
43 // wait for our bar to complete and flush
4444 p.Wait()
4545 }