refactoring complex example
Vladimir Bauer
4 years ago
| 2 | 2 | import ( |
| 3 | 3 | "fmt" |
| 4 | 4 | "math/rand" |
| 5 | "sync" | |
| 6 | 5 | "time" |
| 7 | 6 | |
| 8 | 7 | "github.com/vbauerster/mpb/v7" |
| 14 | 13 | } |
| 15 | 14 | |
| 16 | 15 | func main() { |
| 17 | doneWg := new(sync.WaitGroup) | |
| 18 | // passed doneWg will be accounted at p.Wait() call | |
| 19 | p := mpb.New(mpb.WithWaitGroup(doneWg)) | |
| 20 | 16 | numBars := 4 |
| 17 | bars := make([]*mpb.Bar, numBars) | |
| 18 | p := mpb.New() | |
| 21 | 19 | |
| 22 | var bars []*mpb.Bar | |
| 23 | var downloadWgg []*sync.WaitGroup | |
| 24 | 20 | for i := 0; i < numBars; i++ { |
| 25 | wg := new(sync.WaitGroup) | |
| 26 | wg.Add(1) | |
| 27 | downloadWgg = append(downloadWgg, wg) | |
| 28 | 21 | task := fmt.Sprintf("Task#%02d:", i) |
| 29 | job := "downloading" | |
| 30 | b := p.AddBar(rand.Int63n(201)+100, | |
| 22 | bars[i] = p.AddBar(rand.Int63n(201)+100, | |
| 31 | 23 | mpb.PrependDecorators( |
| 32 | 24 | decor.Name(task, decor.WC{W: len(task) + 1, C: decor.DidentRight}), |
| 33 | decor.Name(job, decor.WCSyncSpaceR), | |
| 25 | decor.Name("downloading", decor.WCSyncSpaceR), | |
| 34 | 26 | decor.CountersNoUnit("%d / %d", decor.WCSyncWidth), |
| 35 | 27 | ), |
| 36 | 28 | mpb.AppendDecorators(decor.Percentage(decor.WC{W: 5})), |
| 37 | 29 | ) |
| 38 | go newTask(wg, b, i+1) | |
| 39 | bars = append(bars, b) | |
| 30 | go complete(bars[i], i+1) | |
| 40 | 31 | } |
| 41 | 32 | |
| 42 | 33 | for i := 0; i < numBars; i++ { |
| 43 | doneWg.Add(1) | |
| 44 | i := i | |
| 34 | afterBar := bars[i] | |
| 35 | task := fmt.Sprintf("Task#%02d:", i) | |
| 36 | incrBy := numBars - i | |
| 45 | 37 | go func() { |
| 46 | task := fmt.Sprintf("Task#%02d:", i) | |
| 47 | // ANSI escape sequences are not supported on Windows OS | |
| 48 | job := "\x1b[31;1;4mつのだ☆HIRO\x1b[0m" | |
| 49 | // preparing delayed bars | |
| 38 | job := "\x1b[31minstalling\x1b[0m" | |
| 39 | // preparing queued bars | |
| 50 | 40 | b := p.AddBar(rand.Int63n(101)+100, |
| 51 | mpb.BarQueueAfter(bars[i]), | |
| 41 | mpb.BarQueueAfter(afterBar), | |
| 52 | 42 | mpb.BarFillerClearOnComplete(), |
| 53 | 43 | mpb.PrependDecorators( |
| 54 | 44 | decor.Name(task, decor.WC{W: len(task) + 1, C: decor.DidentRight}), |
| 59 | 49 | decor.OnComplete(decor.Percentage(decor.WC{W: 5}), ""), |
| 60 | 50 | ), |
| 61 | 51 | ) |
| 62 | // waiting for download to complete, before starting install job | |
| 63 | downloadWgg[i].Wait() | |
| 64 | go newTask(doneWg, b, numBars-i) | |
| 52 | complete(b, incrBy) // blocks until afterBar completes | |
| 65 | 53 | }() |
| 66 | 54 | } |
| 67 | // wait for passed doneWg and for all bars to complete and flush | |
| 55 | ||
| 68 | 56 | p.Wait() |
| 69 | 57 | } |
| 70 | 58 | |
| 71 | func newTask(wg *sync.WaitGroup, bar *mpb.Bar, incrBy int) { | |
| 72 | defer wg.Done() | |
| 59 | func complete(bar *mpb.Bar, incrBy int) { | |
| 73 | 60 | max := 100 * time.Millisecond |
| 74 | 61 | for !bar.Completed() { |
| 75 | 62 | // start variable is solely for EWMA calculation |