update sort example
Vladimir Bauer
9 years ago
| 13 | 13 | |
| 14 | 14 | func main() { |
| 15 | 15 | |
| 16 | p := mpb.New().RefreshRate(80 * time.Millisecond).WithSort(mpb.SortTop).SetWidth(60) | |
| 16 | p := mpb.New().WithSort(mpb.SortTop).SetWidth(60) | |
| 17 | 17 | |
| 18 | bar1 := p.AddBar(100).AppendETA().PrependFunc(getDecor("Bar#1")) | |
| 18 | name1 := "Bar#1:" | |
| 19 | bar1 := p.AddBar(100).AppendETA().PrependFunc(getDecor()).PrependName(name1, len(name1)) | |
| 19 | 20 | go func() { |
| 20 | 21 | blockSize := rand.Intn(maxBlockSize) + 1 |
| 21 | 22 | for i := 0; i < 100; i++ { |
| 25 | 26 | } |
| 26 | 27 | }() |
| 27 | 28 | |
| 28 | bar2 := p.AddBar(60).AppendETA().PrependFunc(getDecor("Bar#2")) | |
| 29 | bar2 := p.AddBar(60).AppendETA().PrependFunc(getDecor()).PrependName("", 0-len(name1)) | |
| 29 | 30 | go func() { |
| 30 | 31 | blockSize := rand.Intn(maxBlockSize) + 1 |
| 31 | 32 | for i := 0; i < 60; i++ { |
| 35 | 36 | } |
| 36 | 37 | }() |
| 37 | 38 | |
| 38 | bar3 := p.AddBar(80).AppendETA().PrependFunc(getDecor("Bar#3")) | |
| 39 | bar3 := p.AddBar(80).AppendETA().PrependFunc(getDecor()).PrependName("Bar#3:", 0) | |
| 39 | 40 | go func() { |
| 40 | 41 | blockSize := rand.Intn(maxBlockSize) + 1 |
| 41 | 42 | for i := 0; i < 80; i++ { |
| 49 | 50 | // p.RemoveBar(bar2) |
| 50 | 51 | |
| 51 | 52 | p.WaitAndStop() |
| 52 | bar2.Incr(2) | |
| 53 | 53 | fmt.Println("stop") |
| 54 | 54 | // p.AddBar(1) // panic: send on closed channnel |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | func getDecor(name string) mpb.DecoratorFunc { | |
| 57 | func getDecor() mpb.DecoratorFunc { | |
| 58 | 58 | return func(s *mpb.Statistics) string { |
| 59 | str := fmt.Sprintf("%d/%d", s.Completed, s.Total) | |
| 60 | return fmt.Sprintf("%s %-7s", name, str) | |
| 59 | str := fmt.Sprintf("%d/%d", s.Current, s.Total) | |
| 60 | return fmt.Sprintf("%-7s", str) | |
| 61 | 61 | } |
| 62 | 62 | } |