refactoring: specific types
Vladimir Bauer
3 years ago
| 14 | 14 | var doneTasks uint32 |
| 15 | 15 | |
| 16 | 16 | type task struct { |
| 17 | id int | |
| 18 | total int | |
| 17 | id uint32 | |
| 18 | total int64 | |
| 19 | 19 | bar *mpb.Bar |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | func main() { |
| 23 | 23 | numTasks := 4 |
| 24 | 24 | |
| 25 | var total int | |
| 25 | var total int64 | |
| 26 | 26 | var filler mpb.BarFiller |
| 27 | 27 | tasks := make([]*task, numTasks) |
| 28 | 28 | |
| 29 | 29 | for i := 0; i < numTasks; i++ { |
| 30 | 30 | task := &task{ |
| 31 | id: i, | |
| 32 | total: rand.Intn(101) + 100, | |
| 31 | id: uint32(i), | |
| 32 | total: rand.Int63n(666) + 100, | |
| 33 | 33 | } |
| 34 | 34 | total += task.total |
| 35 | 35 | filler = middleware(filler, task.id) |
| 45 | 45 | if i != 0 { |
| 46 | 46 | waitBar = tasks[i-1].bar |
| 47 | 47 | } |
| 48 | total := tasks[i].total | |
| 49 | bar := p.AddBar(int64(total), | |
| 48 | bar := p.AddBar(tasks[i].total, | |
| 50 | 49 | mpb.BarExtenderRev(filler), |
| 51 | 50 | mpb.BarQueueAfter(waitBar, false), |
| 52 | 51 | mpb.PrependDecorators( |
| 83 | 82 | p.Wait() |
| 84 | 83 | } |
| 85 | 84 | |
| 86 | func middleware(base mpb.BarFiller, id int) mpb.BarFiller { | |
| 85 | func middleware(base mpb.BarFiller, id uint32) mpb.BarFiller { | |
| 87 | 86 | var done bool |
| 88 | 87 | fn := func(w io.Writer, _ int, st decor.Statistics) { |
| 89 | 88 | if !done { |
| 90 | cur := atomic.LoadUint32(&curTask) == uint32(id) | |
| 89 | cur := atomic.LoadUint32(&curTask) == id | |
| 91 | 90 | if !cur { |
| 92 | 91 | fmt.Fprintf(w, " Taksk %02d\n", id) |
| 93 | 92 | return |