update example_test
Vladimir Bauer
9 years ago
| 0 | 0 | package mpb_test |
| 1 | 1 | |
| 2 | 2 | import ( |
| 3 | "fmt" | |
| 4 | 3 | "math/rand" |
| 5 | "sync" | |
| 6 | 4 | "time" |
| 7 | "unicode/utf8" | |
| 8 | 5 | |
| 9 | 6 | "github.com/vbauerster/mpb" |
| 7 | "github.com/vbauerster/mpb/decor" | |
| 10 | 8 | ) |
| 11 | 9 | |
| 12 | 10 | func Example() { |
| 13 | 11 | // 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 | ) | |
| 22 | 20 | |
| 21 | total := 100 | |
| 22 | barName := "Single Bar:" | |
| 23 | 23 | // 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 | ) | |
| 27 | 31 | |
| 28 | 32 | for i := 0; i < 100; i++ { |
| 29 | 33 | time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) |
| 36 | 40 | |
| 37 | 41 | func ExampleBar_InProgress() { |
| 38 | 42 | p := mpb.New() |
| 39 | bar := p.AddBar(100).AppendPercentage(5, 0) | |
| 43 | bar := p.AddBar(100, mpb.AppendDecorators(decor.Percentage(5, 0))) | |
| 40 | 44 | |
| 41 | 45 | for bar.InProgress() { |
| 42 | 46 | time.Sleep(time.Millisecond * 20) |
| 43 | 47 | bar.Incr(1) |
| 44 | 48 | } |
| 45 | 49 | } |
| 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 | } | |