refactor: examples update to use p.New()
Vladimir Bauer
9 years ago
| 18 | 18 | var wg sync.WaitGroup |
| 19 | 19 | ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second) |
| 20 | 20 | defer cancel() |
| 21 | p := mpb.New(ctx).SetWidth(64) | |
| 21 | p := mpb.New().SetWidth(64).WithContext(ctx) | |
| 22 | 22 | |
| 23 | 23 | name1 := "Bar#1:" |
| 24 | 24 | bar1 := p.AddBar(50). |
| 18 | 18 | url2 := "https://homebrew.bintray.com/bottles/libtiff-4.0.7.sierra.bottle.tar.gz" |
| 19 | 19 | |
| 20 | 20 | var wg sync.WaitGroup |
| 21 | p := mpb.New(nil).SetWidth(60) | |
| 21 | p := mpb.New().SetWidth(60) | |
| 22 | 22 | |
| 23 | 23 | for i, url := range [...]string{url1, url2} { |
| 24 | 24 | wg.Add(1) |
| 34 | 34 | } |
| 35 | 35 | defer dest.Close() |
| 36 | 36 | |
| 37 | p := mpb.New(nil).SetWidth(64) | |
| 37 | p := mpb.New().SetWidth(64) | |
| 38 | 38 | |
| 39 | 39 | bar := p.AddBar(size). |
| 40 | 40 | PrependCounters("%3s / %3s", mpb.UnitBytes, 18, 0). |
| 15 | 15 | func main() { |
| 16 | 16 | |
| 17 | 17 | var wg sync.WaitGroup |
| 18 | p := mpb.New(nil).SetWidth(64) | |
| 19 | // p := mpb.New(nil).RefreshRate(80 * time.Millisecond).SetWidth(64) | |
| 18 | p := mpb.New().SetWidth(64) | |
| 20 | 19 | |
| 21 | 20 | name1 := "Bar#1:" |
| 22 | 21 | bar1 := p.AddBar(50). |
| 15 | 15 | func main() { |
| 16 | 16 | |
| 17 | 17 | var wg sync.WaitGroup |
| 18 | p := mpb.New(nil).SetWidth(64) | |
| 19 | // p := mpb.New(nil).RefreshRate(80 * time.Millisecond).SetWidth(64) | |
| 18 | p := mpb.New().SetWidth(64) | |
| 20 | 19 | |
| 21 | 20 | name1 := "Bar#1:" |
| 22 | 21 | bar1 := p.AddBar(50). |
| 15 | 15 | func main() { |
| 16 | 16 | |
| 17 | 17 | var wg sync.WaitGroup |
| 18 | p := mpb.New(nil).SetWidth(64) | |
| 19 | // p := mpb.New(nil).RefreshRate(100 * time.Millisecond).SetWidth(64) | |
| 18 | p := mpb.New().SetWidth(64) | |
| 20 | 19 | |
| 21 | 20 | name1 := "Bar#1:" |
| 22 | 21 | bar1 := p.AddBar(50). |
| 10 | 10 | |
| 11 | 11 | func main() { |
| 12 | 12 | var wg sync.WaitGroup |
| 13 | p := mpb.New(nil) | |
| 13 | p := mpb.New() | |
| 14 | 14 | wg.Add(3) // add wg delta |
| 15 | 15 | for i := 0; i < 3; i++ { |
| 16 | 16 | name := fmt.Sprintf("Bar#%d:", i) |
| 30 | 30 | } |
| 31 | 31 | wg.Wait() // Wait for goroutines to finish |
| 32 | 32 | p.Stop() // Stop mpb's rendering goroutine |
| 33 | // p.AddBar(1) // panic: you cannot reuse p, create new one! | |
| 34 | fmt.Println("finish") | |
| 35 | 33 | } |
| 8 | 8 | |
| 9 | 9 | func main() { |
| 10 | 10 | // Star mpb's rendering goroutine. |
| 11 | // If you don't plan to cancel, feed with nil | |
| 12 | // otherwise provide context.Context, see cancel example | |
| 13 | p := mpb.New(nil) | |
| 14 | // Set custom width for every bar, which mpb will contain | |
| 11 | p := mpb.New() | |
| 12 | // Set custom width for every bar, which mpb will render | |
| 15 | 13 | // The default one in 70 |
| 16 | 14 | p.SetWidth(80) |
| 17 | 15 | // Set custom format for every bar, the default one is "[=>-]" |
| 43 | 43 | func main() { |
| 44 | 44 | |
| 45 | 45 | var wg sync.WaitGroup |
| 46 | p := mpb.New(nil).SetWidth(60).BeforeRenderFunc(sortByProgressFunc()) | |
| 46 | p := mpb.New().SetWidth(60).BeforeRenderFunc(sortByProgressFunc()) | |
| 47 | 47 | |
| 48 | 48 | name1 := "Bar#1:" |
| 49 | 49 | bar1 := p.AddBar(100). |
| 16 | 16 | func main() { |
| 17 | 17 | |
| 18 | 18 | var wg sync.WaitGroup |
| 19 | p := mpb.New(nil) | |
| 19 | p := mpb.New() | |
| 20 | 20 | wg.Add(totalBars) |
| 21 | 21 | |
| 22 | 22 | for i := 0; i < totalBars; i++ { |
| 3 | 3 | "fmt" |
| 4 | 4 | "math/rand" |
| 5 | 5 | "time" |
| 6 | "unicode/utf8" | |
| 6 | 7 | |
| 7 | 8 | "github.com/vbauerster/mpb" |
| 8 | 9 | ) |
| 11 | 12 | // Star mpb's rendering goroutine. |
| 12 | 13 | // If you don't plan to cancel, feed with nil |
| 13 | 14 | // otherwise provide context.Context, see cancel example |
| 14 | p := mpb.New(nil) | |
| 15 | p := mpb.New() | |
| 15 | 16 | // Set custom width for every bar, which mpb will contain |
| 16 | 17 | // The default one in 70 |
| 17 | 18 | p.SetWidth(80) |
| 38 | 39 | } |
| 39 | 40 | |
| 40 | 41 | func ExampleBar_InProgress() { |
| 41 | p := mpb.New(nil) | |
| 42 | p := mpb.New() | |
| 42 | 43 | bar := p.AddBar(100).AppendPercentage(5, 0) |
| 43 | 44 | |
| 44 | 45 | for bar.InProgress() { |
| 49 | 50 | |
| 50 | 51 | func ExampleBar_PrependFunc() { |
| 51 | 52 | decor := func(s *mpb.Statistics, myWidth chan<- int, maxWidth <-chan int) string { |
| 52 | str := fmt.Sprintf("%d/%d", s.Current, s.Total) | |
| 53 | return fmt.Sprintf("%8s", str) | |
| 53 | str := fmt.Sprintf("%3d/%3d", s.Current, s.Total) | |
| 54 | myWidth <- utf8.RuneCountInString(str) | |
| 55 | max := <-maxWidth | |
| 56 | return fmt.Sprintf(fmt.Sprintf("%%%ds", max+1), str) | |
| 54 | 57 | } |
| 55 | 58 | |
| 56 | 59 | totalItem := 100 |
| 57 | p := mpb.New(nil) | |
| 60 | p := mpb.New() | |
| 58 | 61 | bar := p.AddBar(int64(totalItem)).PrependFunc(decor) |
| 59 | 62 | |
| 60 | 63 | for i := 0; i < totalItem; i++ { |
| 65 | 68 | |
| 66 | 69 | func ExampleBar_AppendFunc() { |
| 67 | 70 | decor := func(s *mpb.Statistics, myWidth chan<- int, maxWidth <-chan int) string { |
| 68 | str := fmt.Sprintf("%d/%d", s.Current, s.Total) | |
| 69 | return fmt.Sprintf("%8s", str) | |
| 71 | str := fmt.Sprintf("%3d/%3d", s.Current, s.Total) | |
| 72 | myWidth <- utf8.RuneCountInString(str) | |
| 73 | max := <-maxWidth | |
| 74 | return fmt.Sprintf(fmt.Sprintf("%%%ds", max+1), str) | |
| 70 | 75 | } |
| 71 | 76 | |
| 72 | 77 | totalItem := 100 |
| 73 | p := mpb.New(nil) | |
| 78 | p := mpb.New() | |
| 74 | 79 | bar := p.AddBar(int64(totalItem)).AppendFunc(decor) |
| 75 | 80 | |
| 76 | 81 | for i := 0; i < totalItem; i++ { |
| 6 | 6 | |
| 7 | 7 | func TestAddBar(t *testing.T) { |
| 8 | 8 | var buf bytes.Buffer |
| 9 | p := New(nil).SetWidth(60).SetOut(&buf) | |
| 9 | p := New().SetWidth(60).SetOut(&buf) | |
| 10 | 10 | count := p.BarCount() |
| 11 | 11 | if count != 0 { |
| 12 | 12 | t.Errorf("Count want: %q, got: %q\n", 0, count) |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | func TestRemoveBar(t *testing.T) { |
| 26 | p := New(nil) | |
| 26 | p := New() | |
| 27 | 27 | b := p.AddBar(10) |
| 28 | 28 | |
| 29 | 29 | if !p.RemoveBar(b) { |