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