godoc update
Vladimir Bauer
8 years ago
| 38 | 38 | |
| 39 | 39 | total := 100 |
| 40 | 40 | name := "Single Bar:" |
| 41 | // Adding a single bar | |
| 41 | // adding a single bar | |
| 42 | 42 | bar := p.AddBar(int64(total), |
| 43 | 43 | mpb.PrependDecorators( |
| 44 | 44 | // Display our static name with one space on the right |
| 52 | 52 | ), |
| 53 | 53 | ) |
| 54 | 54 | |
| 55 | // Simulating some work | |
| 55 | // simulating some work | |
| 56 | 56 | max := 100 * time.Millisecond |
| 57 | 57 | for i := 0; i < total; i++ { |
| 58 | 58 | 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) | |
| 60 | 60 | bar.Increment() |
| 61 | 61 | } |
| 62 | // Wait for our bar to complete and flush | |
| 62 | // wait for our bar to complete and flush | |
| 63 | 63 | p.Wait() |
| 64 | 64 | ``` |
| 65 | 65 | |
| 84 | 84 | decor.OnComplete(decor.ETA(3, 0), "done!", 0, 0), |
| 85 | 85 | ), |
| 86 | 86 | ) |
| 87 | // Simulating some work | |
| 87 | // simulating some work | |
| 88 | 88 | go func() { |
| 89 | 89 | defer wg.Done() |
| 90 | 90 | max := 100 * time.Millisecond |
| 94 | 94 | } |
| 95 | 95 | }() |
| 96 | 96 | } |
| 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 | |
| 99 | 99 | p.Wait() |
| 100 | 100 | ``` |
| 101 | 101 | |
| 0 | 0 | package mpb_test |
| 1 | 1 | |
| 2 | 2 | import ( |
| 3 | "io" | |
| 3 | 4 | "math/rand" |
| 5 | "net/http" | |
| 4 | 6 | "time" |
| 5 | 7 | |
| 6 | 8 | "github.com/vbauerster/mpb" |
| 19 | 21 | |
| 20 | 22 | total := 100 |
| 21 | 23 | 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 | |
| 24 | 25 | bar := p.AddBar(int64(total), |
| 25 | // Prepending decorators | |
| 26 | 26 | 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), | |
| 32 | 31 | ), |
| 33 | // Appending decorators | |
| 34 | 32 | mpb.AppendDecorators( |
| 35 | // Percentage decorator with minWidth and no extra config | |
| 33 | // Percentage decorator with width reservation of 5 runes | |
| 36 | 34 | decor.Percentage(5, 0), |
| 37 | 35 | ), |
| 38 | 36 | ) |
| 39 | 37 | |
| 38 | // simulating some work | |
| 40 | 39 | max := 100 * time.Millisecond |
| 41 | 40 | for i := 0; i < total; i++ { |
| 42 | 41 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) |
| 42 | // increment by 1 (there is bar.IncrBy(int) method, if needed) | |
| 43 | 43 | bar.Increment() |
| 44 | 44 | } |
| 45 | // Wait for all bars to complete | |
| 45 | // wait for our bar to complete and flush | |
| 46 | 46 | p.Wait() |
| 47 | 47 | } |
| 48 | 48 | |
| 58 | 58 | |
| 59 | 59 | p.Wait() |
| 60 | 60 | } |
| 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 | } | |
| 40 | 40 | bar := p.AddBar(size, |
| 41 | 41 | mpb.PrependDecorators( |
| 42 | 42 | decor.CountersKibiByte("% 6.1f / % 6.1f", 18, 0), |
| 43 | )) | |
| 43 | ), | |
| 44 | ) | |
| 44 | 45 | |
| 45 | 46 | // create proxy reader |
| 46 | 47 | reader := bar.ProxyReader(resp.Body) |
| 48 | 49 | // and copy from reader, ignoring errors |
| 49 | 50 | io.Copy(dest, reader) |
| 50 | 51 | |
| 51 | p.Wait() // if you omit this line, rendering bars goroutine will quit early | |
| 52 | fmt.Println("done") | |
| 52 | p.Wait() | |
| 53 | 53 | } |
| 29 | 29 | decor.Percentage(0, decor.DwidthSync), |
| 30 | 30 | ), |
| 31 | 31 | mpb.AppendDecorators( |
| 32 | // Replace our ETA decorator with "done!", on bar completion event | |
| 32 | // replace our ETA decorator with "done!", on bar completion event | |
| 33 | 33 | decor.OnComplete(decor.ETA(3, 0), "done!", 0, 0), |
| 34 | 34 | ), |
| 35 | 35 | ) |
| 36 | // Simulating some work | |
| 36 | // simulating some work | |
| 37 | 37 | go func() { |
| 38 | 38 | defer wg.Done() |
| 39 | 39 | max := 100 * time.Millisecond |
| 43 | 43 | } |
| 44 | 44 | }() |
| 45 | 45 | } |
| 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 | |
| 48 | 48 | p.Wait() |
| 49 | 49 | } |
| 19 | 19 | |
| 20 | 20 | total := 100 |
| 21 | 21 | name := "Single Bar:" |
| 22 | // Adding a single bar | |
| 22 | // adding a single bar | |
| 23 | 23 | bar := p.AddBar(int64(total), |
| 24 | 24 | mpb.PrependDecorators( |
| 25 | 25 | // Display our static name with one space on the right |
| 33 | 33 | ), |
| 34 | 34 | ) |
| 35 | 35 | |
| 36 | // Simulating some work | |
| 36 | // simulating some work | |
| 37 | 37 | max := 100 * time.Millisecond |
| 38 | 38 | for i := 0; i < total; i++ { |
| 39 | 39 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) |
| 40 | 40 | // Increment by 1 (there is bar.IncrBy(int) method, if needed) |
| 41 | 41 | bar.Increment() |
| 42 | 42 | } |
| 43 | // Wait for our bar to complete and flush | |
| 43 | // wait for our bar to complete and flush | |
| 44 | 44 | p.Wait() |
| 45 | 45 | } |