show Format method
Vladimir Bauer
9 years ago
| 27 | 27 | // If you don't plan to cancel, feed with nil |
| 28 | 28 | // otherwise provide context.Context, see cancel example |
| 29 | 29 | p := mpb.New(nil) |
| 30 | // Set custom format, the default one is "[=>-]" | |
| 31 | p.Format("╢▌▌░╟") | |
| 32 | ||
| 30 | 33 | bar := p.AddBar(100).PrependName(name, 0).AppendPercentage() |
| 31 | 34 | |
| 32 | 35 | for i := 0; i < 100; i++ { |
| 36 | 39 | |
| 37 | 40 | p.Stop() |
| 38 | 41 | ``` |
| 39 | The source code: [example/singleBar/main.go](example/singleBar/main.go) | |
| 40 | 42 | |
| 41 | However **mpb** was designed with concurrency in mind, each new bar renders in its | |
| 42 | own goroutine. Therefore adding multiple bars is easy and safe: | |
| 43 | Running [this](example/singleBar/main.go), will produce: | |
| 44 | ||
| 45 |  | |
| 46 | ||
| 47 | However **mpb** was designed with concurrency in mind. Each new bar renders in its | |
| 48 | own goroutine, therefore adding multiple bars is easy and safe: | |
| 43 | 49 | |
| 44 | 50 | ```go |
| 45 | 51 | var wg sync.WaitGroup |
| 51 | 57 | go func() { |
| 52 | 58 | defer wg.Done() |
| 53 | 59 | // you can p.AddBar() here, but ordering will be non deterministic |
| 60 | // if you still need p.AddBar() here and maintain ordering, use | |
| 61 | // (*mpb.Progress).BeforeRenderFunc(f mpb.BeforeRender) | |
| 54 | 62 | for i := 0; i < 100; i++ { |
| 55 | 63 | time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) |
| 56 | 64 | bar.Incr(1) |
| 63 | 71 | fmt.Println("finish") |
| 64 | 72 | ``` |
| 65 | 73 | |
| 66 | This will produce following: | |
| 67 | ||
| 68 |  | |
| 74 |  | |
| 69 | 75 | |
| 70 | 76 | The source code: [example/simple/main.go](example/simple/main.go) |
| 71 | 77 | |
| 72 | 78 | ### Cancel |
| 73 | 79 | |
| 74 |  | |
| 80 |  | |
| 75 | 81 | |
| 76 | 82 | The source code: [example/cancel/main.go](example/cancel/main.go) |
| 77 | 83 | |
| 78 | 84 | ### Removing bar |
| 79 | 85 | |
| 80 |  | |
| 86 |  | |
| 81 | 87 | |
| 82 | 88 | The source code: [example/remove/main.go](example/remove/main.go) |
| 83 | 89 | |
| 84 | 90 | ### Sorting bars by progress |
| 85 | 91 | |
| 86 |  | |
| 92 |  | |
| 87 | 93 | |
| 88 | 94 | The source code: [example/sort/main.go](example/sort/main.go) |
| 89 | 95 | |
| 90 | 96 | ### Resizing bars on terminal width change |
| 91 | 97 | |
| 92 |  | |
| 98 |  | |
| 93 | 99 | |
| 94 | 100 | The source code: [example/prependETA/main.go](example/prependETA/main.go) |
| 95 | 101 | |
| 96 | 102 | ### Multiple io |
| 97 | 103 | |
| 98 |  | |
| 104 |  | |
| 99 | 105 | |
| 100 | 106 | The source code: [example/io/multiple/main.go](example/io/multiple/main.go) |
| 101 | 107 | |
Binary diff not shown
| 18 | 18 | go func() { |
| 19 | 19 | defer wg.Done() |
| 20 | 20 | // you can p.AddBar() here, but ordering will be non deterministic |
| 21 | // if you still need p.AddBar() here and maintain ordering, use | |
| 22 | // (*mpb.Progress).BeforeRenderFunc(f mpb.BeforeRender) | |
| 21 | 23 | for i := 0; i < 100; i++ { |
| 22 | 24 | time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) |
| 23 | 25 | bar.Incr(1) |