update PrependFunc and AppendFunc examples
Vladimir Bauer
9 years ago
| 2 | 2 | import ( |
| 3 | 3 | "fmt" |
| 4 | 4 | "math/rand" |
| 5 | "sync" | |
| 5 | 6 | "time" |
| 6 | 7 | "unicode/utf8" |
| 7 | 8 | |
| 51 | 52 | func ExampleBar_PrependFunc() { |
| 52 | 53 | decor := func(s *mpb.Statistics, myWidth chan<- int, maxWidth <-chan int) string { |
| 53 | 54 | str := fmt.Sprintf("%3d/%3d", s.Current, s.Total) |
| 55 | // send width to Progress' goroutine | |
| 54 | 56 | myWidth <- utf8.RuneCountInString(str) |
| 57 | // receive max width | |
| 55 | 58 | max := <-maxWidth |
| 56 | 59 | return fmt.Sprintf(fmt.Sprintf("%%%ds", max+1), str) |
| 57 | 60 | } |
| 58 | 61 | |
| 59 | 62 | totalItem := 100 |
| 63 | var wg sync.WaitGroup | |
| 60 | 64 | p := mpb.New() |
| 61 | bar := p.AddBar(int64(totalItem)).PrependFunc(decor) | |
| 62 | ||
| 63 | for i := 0; i < totalItem; i++ { | |
| 64 | bar.Incr(1) // increment progress bar | |
| 65 | time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) | |
| 65 | wg.Add(3) // add wg delta | |
| 66 | for i := 0; i < 3; i++ { | |
| 67 | name := fmt.Sprintf("Bar#%d:", i) | |
| 68 | bar := p.AddBar(int64(totalItem)). | |
| 69 | PrependName(name, len(name), 0). | |
| 70 | PrependFunc(decor) | |
| 71 | go func() { | |
| 72 | defer wg.Done() | |
| 73 | for i := 0; i < totalItem; i++ { | |
| 74 | bar.Incr(1) | |
| 75 | time.Sleep(time.Duration(rand.Intn(totalItem)) * time.Millisecond) | |
| 76 | } | |
| 77 | }() | |
| 66 | 78 | } |
| 79 | wg.Wait() // Wait for goroutines to finish | |
| 80 | p.Stop() // Stop mpb's rendering goroutine | |
| 67 | 81 | } |
| 68 | 82 | |
| 69 | 83 | func ExampleBar_AppendFunc() { |
| 70 | 84 | decor := func(s *mpb.Statistics, myWidth chan<- int, maxWidth <-chan int) string { |
| 71 | 85 | str := fmt.Sprintf("%3d/%3d", s.Current, s.Total) |
| 86 | // send width to Progress' goroutine | |
| 72 | 87 | myWidth <- utf8.RuneCountInString(str) |
| 88 | // receive max width | |
| 73 | 89 | max := <-maxWidth |
| 74 | 90 | return fmt.Sprintf(fmt.Sprintf("%%%ds", max+1), str) |
| 75 | 91 | } |
| 76 | 92 | |
| 77 | 93 | totalItem := 100 |
| 94 | var wg sync.WaitGroup | |
| 78 | 95 | p := mpb.New() |
| 79 | bar := p.AddBar(int64(totalItem)).AppendFunc(decor) | |
| 80 | ||
| 81 | for i := 0; i < totalItem; i++ { | |
| 82 | bar.Incr(1) // increment progress bar | |
| 83 | time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) | |
| 96 | wg.Add(3) // add wg delta | |
| 97 | for i := 0; i < 3; i++ { | |
| 98 | name := fmt.Sprintf("Bar#%d:", i) | |
| 99 | bar := p.AddBar(int64(totalItem)). | |
| 100 | PrependName(name, len(name), 0). | |
| 101 | AppendFunc(decor) | |
| 102 | go func() { | |
| 103 | defer wg.Done() | |
| 104 | for i := 0; i < totalItem; i++ { | |
| 105 | bar.Incr(1) | |
| 106 | time.Sleep(time.Duration(rand.Intn(totalItem)) * time.Millisecond) | |
| 107 | } | |
| 108 | }() | |
| 84 | 109 | } |
| 110 | wg.Wait() // Wait for goroutines to finish | |
| 111 | p.Stop() // Stop mpb's rendering goroutine | |
| 85 | 112 | } |