|
0 |
package main
|
|
1 |
|
|
2 |
import (
|
|
3 |
"fmt"
|
|
4 |
"io"
|
|
5 |
"math/rand"
|
|
6 |
"sync/atomic"
|
|
7 |
"time"
|
|
8 |
|
|
9 |
"github.com/vbauerster/mpb/v7"
|
|
10 |
"github.com/vbauerster/mpb/v7/decor"
|
|
11 |
)
|
|
12 |
|
|
13 |
var curTask uint32
|
|
14 |
var doneTasks uint32
|
|
15 |
|
|
16 |
type task struct {
|
|
17 |
id int
|
|
18 |
total int
|
|
19 |
bar *mpb.Bar
|
|
20 |
}
|
|
21 |
|
|
22 |
func main() {
|
|
23 |
numTasks := 4
|
|
24 |
|
|
25 |
var total int
|
|
26 |
var filler mpb.BarFiller
|
|
27 |
tasks := make([]*task, numTasks)
|
|
28 |
|
|
29 |
for i := 0; i < numTasks; i++ {
|
|
30 |
task := &task{
|
|
31 |
id: i,
|
|
32 |
total: rand.Intn(101) + 100,
|
|
33 |
}
|
|
34 |
total += task.total
|
|
35 |
filler = middleware(filler, task.id)
|
|
36 |
tasks[i] = task
|
|
37 |
}
|
|
38 |
|
|
39 |
filler = newLineMiddleware(filler)
|
|
40 |
|
|
41 |
p := mpb.New()
|
|
42 |
|
|
43 |
for i := 0; i < numTasks; i++ {
|
|
44 |
var waitBar *mpb.Bar
|
|
45 |
if i != 0 {
|
|
46 |
waitBar = tasks[i-1].bar
|
|
47 |
}
|
|
48 |
total := tasks[i].total
|
|
49 |
bar := p.AddBar(int64(total),
|
|
50 |
mpb.BarExtenderRev(filler),
|
|
51 |
mpb.BarQueueAfter(waitBar, false),
|
|
52 |
mpb.PrependDecorators(
|
|
53 |
decor.Name("current:", decor.WCSyncWidthR),
|
|
54 |
),
|
|
55 |
mpb.AppendDecorators(
|
|
56 |
decor.Percentage(decor.WCSyncWidth),
|
|
57 |
),
|
|
58 |
)
|
|
59 |
tasks[i].bar = bar
|
|
60 |
}
|
|
61 |
|
|
62 |
tb := p.AddBar(int64(total),
|
|
63 |
mpb.PrependDecorators(
|
|
64 |
decor.Any(func(st decor.Statistics) string {
|
|
65 |
var done uint32
|
|
66 |
if st.Completed {
|
|
67 |
done = uint32(len(tasks))
|
|
68 |
} else {
|
|
69 |
done = atomic.LoadUint32(&doneTasks)
|
|
70 |
}
|
|
71 |
return fmt.Sprintf("TOTAL(%d/%d)", done, len(tasks))
|
|
72 |
}, decor.WCSyncWidthR),
|
|
73 |
),
|
|
74 |
mpb.AppendDecorators(
|
|
75 |
decor.Percentage(decor.WCSyncWidth),
|
|
76 |
),
|
|
77 |
)
|
|
78 |
|
|
79 |
for _, t := range tasks {
|
|
80 |
atomic.StoreUint32(&curTask, uint32(t.id))
|
|
81 |
complete(tb, t)
|
|
82 |
atomic.AddUint32(&doneTasks, 1)
|
|
83 |
}
|
|
84 |
|
|
85 |
p.Wait()
|
|
86 |
}
|
|
87 |
|
|
88 |
func middleware(base mpb.BarFiller, id int) mpb.BarFiller {
|
|
89 |
var done bool
|
|
90 |
fn := func(w io.Writer, _ int, st decor.Statistics) {
|
|
91 |
if !done {
|
|
92 |
cur := atomic.LoadUint32(&curTask) == uint32(id)
|
|
93 |
if !st.Completed && cur {
|
|
94 |
fmt.Fprintf(w, "=> Taksk %02d\n", id)
|
|
95 |
return
|
|
96 |
} else if !cur {
|
|
97 |
fmt.Fprintf(w, " Taksk %02d\n", id)
|
|
98 |
return
|
|
99 |
} else {
|
|
100 |
done = cur
|
|
101 |
}
|
|
102 |
}
|
|
103 |
fmt.Fprintf(w, " Taksk %02d: Done!\n", id)
|
|
104 |
}
|
|
105 |
if id == 0 {
|
|
106 |
return mpb.BarFillerFunc(fn)
|
|
107 |
}
|
|
108 |
return mpb.BarFillerFunc(func(w io.Writer, reqWidth int, st decor.Statistics) {
|
|
109 |
fn(w, reqWidth, st)
|
|
110 |
base.Fill(w, reqWidth, st)
|
|
111 |
})
|
|
112 |
}
|
|
113 |
|
|
114 |
func newLineMiddleware(base mpb.BarFiller) mpb.BarFiller {
|
|
115 |
return mpb.BarFillerFunc(func(w io.Writer, reqWidth int, st decor.Statistics) {
|
|
116 |
fmt.Fprintln(w)
|
|
117 |
base.Fill(w, reqWidth, st)
|
|
118 |
})
|
|
119 |
}
|
|
120 |
|
|
121 |
func complete(tb *mpb.Bar, t *task) {
|
|
122 |
bar := t.bar
|
|
123 |
max := 100 * time.Millisecond
|
|
124 |
for !bar.Completed() {
|
|
125 |
n := rand.Int63n(10) + 1
|
|
126 |
bar.IncrInt64(n)
|
|
127 |
tb.IncrInt64(n)
|
|
128 |
time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
|
|
129 |
}
|
|
130 |
bar.Wait()
|
|
131 |
}
|