forked from vbauerster/mpb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (70 loc) · 1.92 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
package main
import (
"fmt"
"math/rand"
"sync"
"time"
"github.com/mikewiacek/mpb"
"github.com/mikewiacek/mpb/decor"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
doneWg := new(sync.WaitGroup)
p := mpb.New(mpb.WithWidth(64), mpb.WithWaitGroup(doneWg))
numBars := 4
var bars []*mpb.Bar
var downloadWgg []*sync.WaitGroup
for i := 0; i < numBars; i++ {
wg := new(sync.WaitGroup)
wg.Add(1)
downloadWgg = append(downloadWgg, wg)
task := fmt.Sprintf("Task#%02d:", i)
job := "downloading"
b := p.AddBar(rand.Int63n(201)+100,
mpb.PrependDecorators(
decor.Name(task, decor.WC{W: len(task) + 1, C: decor.DidentRight}),
decor.Name(job, decor.WCSyncSpaceR),
decor.CountersNoUnit("%d / %d", decor.WCSyncWidth),
),
mpb.AppendDecorators(decor.Percentage(decor.WC{W: 5})),
)
go newTask(wg, b, i+1)
bars = append(bars, b)
}
for i := 0; i < numBars; i++ {
doneWg.Add(1)
i := i
go func() {
task := fmt.Sprintf("Task#%02d:", i)
job := "installing"
// preparing delayed bars
b := p.AddBar(rand.Int63n(101)+100,
mpb.BarParkTo(bars[i]),
mpb.BarClearOnComplete(),
mpb.PrependDecorators(
decor.Name(task, decor.WC{W: len(task) + 1, C: decor.DidentRight}),
decor.OnComplete(decor.Name(job, decor.WCSyncSpaceR), "done!"),
decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_MMSS, 0, decor.WCSyncWidth), "")),
mpb.AppendDecorators(
decor.OnComplete(decor.Percentage(decor.WC{W: 5}), ""),
),
)
// waiting for download to complete, before starting install job
downloadWgg[i].Wait()
go newTask(doneWg, b, numBars-i)
}()
}
p.Wait()
}
func newTask(wg *sync.WaitGroup, b *mpb.Bar, incrBy int) {
defer wg.Done()
max := 100 * time.Millisecond
for !b.Completed() {
start := time.Now()
time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
// ewma based decorators require work duration measurement
b.IncrBy(incrBy, time.Since(start))
}
}