readme examples update
Vladimir Bauer
8 years ago
| 29 | 29 | ```go |
| 30 | 30 | p := mpb.New( |
| 31 | 31 | // override default (80) width |
| 32 | mpb.WithWidth(100), | |
| 32 | mpb.WithWidth(64), | |
| 33 | 33 | // override default "[=>-]" format |
| 34 | 34 | mpb.WithFormat("╢▌▌░╟"), |
| 35 | 35 | // override default 120ms refresh rate |
| 36 | mpb.WithRefreshRate(100*time.Millisecond), | |
| 36 | mpb.WithRefreshRate(180*time.Millisecond), | |
| 37 | 37 | ) |
| 38 | 38 | |
| 39 | 39 | total := 100 |
| 40 | 40 | name := "Single Bar:" |
| 41 | // Add a bar | |
| 42 | // You're not limited to just a single bar, add as many as you need | |
| 41 | // Adding a single bar | |
| 43 | 42 | bar := p.AddBar(int64(total), |
| 44 | // Prepending decorators | |
| 45 | 43 | mpb.PrependDecorators( |
| 46 | // StaticName decorator with one extra space on right | |
| 44 | // Display our static name with one space on the right | |
| 47 | 45 | decor.StaticName(name, len(name)+1, decor.DidentRight), |
| 48 | 46 | // ETA decorator with width reservation of 3 runes |
| 49 | 47 | decor.ETA(3, 0), |
| 50 | 48 | ), |
| 51 | // Appending decorators | |
| 52 | 49 | mpb.AppendDecorators( |
| 53 | 50 | // Percentage decorator with width reservation of 5 runes |
| 54 | 51 | decor.Percentage(5, 0), |
| 55 | 52 | ), |
| 56 | 53 | ) |
| 57 | 54 | |
| 55 | // Simulating some work | |
| 58 | 56 | max := 100 * time.Millisecond |
| 59 | 57 | for i := 0; i < total; i++ { |
| 60 | 58 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) |
| 59 | // Increment by 1 (there is bar.IncrBy(int) method, if needed) | |
| 61 | 60 | bar.Increment() |
| 62 | 61 | } |
| 63 | // Wait for all bars to complete | |
| 62 | // Wait for our bar to complete and flush | |
| 64 | 63 | p.Wait() |
| 65 | 64 | ``` |
| 66 | 65 | |
| 68 | 67 | ```go |
| 69 | 68 | var wg sync.WaitGroup |
| 70 | 69 | p := mpb.New(mpb.WithWaitGroup(&wg)) |
| 71 | total := 100 | |
| 72 | numBars := 3 | |
| 70 | total, numBars := 100, 3 | |
| 73 | 71 | wg.Add(numBars) |
| 74 | 72 | |
| 75 | 73 | for i := 0; i < numBars; i++ { |
| 76 | 74 | name := fmt.Sprintf("Bar#%d:", i) |
| 77 | 75 | bar := p.AddBar(int64(total), |
| 78 | 76 | mpb.PrependDecorators( |
| 79 | decor.StaticName(name, 0, 0), | |
| 80 | // DSyncSpace is shortcut for DwidthSync|DextraSpace | |
| 77 | // Display our static name with one space on the right | |
| 78 | decor.StaticName(name, len(name)+1, decor.DidentRight), | |
| 81 | 79 | // DwidthSync bit enables same column width synchronization |
| 82 | // DextraSpace bit prepends decorator's output with exactly one space | |
| 83 | decor.Percentage(3, decor.DSyncSpace), | |
| 80 | decor.Percentage(0, decor.DwidthSync), | |
| 84 | 81 | ), |
| 85 | 82 | mpb.AppendDecorators( |
| 86 | decor.ETA(3, 0), | |
| 83 | // Replace our ETA decorator with "done!", on bar completion event | |
| 84 | decor.OnComplete(decor.ETA(3, 0), "done!", 0, 0), | |
| 87 | 85 | ), |
| 88 | 86 | ) |
| 87 | // Simulating some work | |
| 89 | 88 | go func() { |
| 90 | 89 | defer wg.Done() |
| 91 | 90 | max := 100 * time.Millisecond |
| 95 | 94 | } |
| 96 | 95 | }() |
| 97 | 96 | } |
| 98 | // Wait for all bars to complete | |
| 97 | // First wait for provided wg, | |
| 98 | // then wait for all bars to complete and flush. | |
| 99 | 99 | p.Wait() |
| 100 | 100 | ``` |
| 101 | 101 | |
| 16 | 16 | func main() { |
| 17 | 17 | var wg sync.WaitGroup |
| 18 | 18 | p := mpb.New(mpb.WithWaitGroup(&wg)) |
| 19 | total := 100 | |
| 20 | numBars := 3 | |
| 19 | total, numBars := 100, 3 | |
| 21 | 20 | wg.Add(numBars) |
| 22 | 21 | |
| 23 | 22 | for i := 0; i < numBars; i++ { |
| 24 | 23 | name := fmt.Sprintf("Bar#%d:", i) |
| 25 | 24 | bar := p.AddBar(int64(total), |
| 26 | 25 | mpb.PrependDecorators( |
| 27 | decor.StaticName(name, 0, 0), | |
| 28 | // DSyncSpace is shortcut for DwidthSync|DextraSpace | |
| 26 | // Display our static name with one space on the right | |
| 27 | decor.StaticName(name, len(name)+1, decor.DidentRight), | |
| 29 | 28 | // DwidthSync bit enables same column width synchronization |
| 30 | // DextraSpace bit prepends decorator's output with exactly one space | |
| 31 | decor.Percentage(3, decor.DSyncSpace), | |
| 29 | decor.Percentage(0, decor.DwidthSync), | |
| 32 | 30 | ), |
| 33 | 31 | mpb.AppendDecorators( |
| 34 | decor.ETA(3, 0), | |
| 32 | // Replace our ETA decorator with "done!", on bar completion event | |
| 33 | decor.OnComplete(decor.ETA(3, 0), "done!", 0, 0), | |
| 35 | 34 | ), |
| 36 | 35 | ) |
| 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 | // Wait for all bars to complete | |
| 46 | // First wait for provided wg, | |
| 47 | // then wait for all bars to complete and flush. | |
| 47 | 48 | p.Wait() |
| 48 | 49 | } |
| 10 | 10 | func main() { |
| 11 | 11 | p := mpb.New( |
| 12 | 12 | // override default (80) width |
| 13 | mpb.WithWidth(100), | |
| 13 | mpb.WithWidth(64), | |
| 14 | 14 | // override default "[=>-]" format |
| 15 | 15 | mpb.WithFormat("╢▌▌░╟"), |
| 16 | 16 | // override default 120ms refresh rate |
| 17 | mpb.WithRefreshRate(100*time.Millisecond), | |
| 17 | mpb.WithRefreshRate(180*time.Millisecond), | |
| 18 | 18 | ) |
| 19 | 19 | |
| 20 | 20 | total := 100 |
| 21 | 21 | name := "Single Bar:" |
| 22 | // Add a bar | |
| 23 | // You're not limited to just a single bar, add as many as you need | |
| 22 | // Adding a single bar | |
| 24 | 23 | bar := p.AddBar(int64(total), |
| 25 | // Prepending decorators | |
| 26 | 24 | mpb.PrependDecorators( |
| 27 | // StaticName decorator with one extra space on right | |
| 25 | // Display our static name with one space on the right | |
| 28 | 26 | decor.StaticName(name, len(name)+1, decor.DidentRight), |
| 29 | 27 | // ETA decorator with width reservation of 3 runes |
| 30 | 28 | decor.ETA(3, 0), |
| 31 | 29 | ), |
| 32 | // Appending decorators | |
| 33 | 30 | mpb.AppendDecorators( |
| 34 | 31 | // Percentage decorator with width reservation of 5 runes |
| 35 | 32 | decor.Percentage(5, 0), |
| 36 | 33 | ), |
| 37 | 34 | ) |
| 38 | 35 | |
| 36 | // Simulating some work | |
| 39 | 37 | max := 100 * time.Millisecond |
| 40 | 38 | for i := 0; i < total; i++ { |
| 41 | 39 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) |
| 40 | // Increment by 1 (there is bar.IncrBy(int) method, if needed) | |
| 42 | 41 | bar.Increment() |
| 43 | 42 | } |
| 44 | // Wait for all bars to complete | |
| 43 | // Wait for our bar to complete and flush | |
| 45 | 44 | p.Wait() |
| 46 | 45 | } |