-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress.go
100 lines (85 loc) · 2.08 KB
/
progress.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
package main
import (
"fmt"
"sync"
"time"
"github.com/ttacon/chalk"
"github.com/vbauerster/mpb/v5"
"github.com/vbauerster/mpb/v5/decor"
)
const total = 100
func updateBar(bar *mpb.Bar, name string) {
stat := B2Client.Status().Writers[name]
if stat != nil {
prog := stat.Progress
var sum float64
for _, p := range prog {
sum += p
}
bar.SetTotal(int64(total*len(prog)), false)
bar.SetCurrent(int64(total*sum) + 1)
}
}
func makePipeBar(name string) (*mpb.Progress, *mpb.Bar) {
p := mpb.New(mpb.WithWidth(14))
bar := p.AddSpinner(int64(total), mpb.SpinnerOnMiddle,
mpb.SpinnerStyle([]string{"∙∙∙∙", "●∙∙∙", "∙●∙∙", "∙∙●∙", "∙∙∙●", "∙∙∙∙"}),
mpb.PrependDecorators(
decor.Name(name),
),
mpb.AppendDecorators(
decor.OnComplete(
decor.EwmaETA(decor.ET_STYLE_GO, 60),
fmt.Sprintf(" %sdone%s ", chalk.Green, chalk.Reset),
),
),
)
return p, bar
}
func makeFileBar(name string) (*mpb.Progress, *mpb.Bar) {
p := mpb.New(mpb.WithWidth(64))
bar := p.AddBar(int64(total),
mpb.PrependDecorators(
decor.Name(name, decor.WC{W: 1, C: decor.DidentRight}),
decor.OnComplete(
decor.AverageETA(decor.ET_STYLE_GO, decor.WC{W: 4}),
fmt.Sprintf(" %sdone%s ", chalk.Green, chalk.Reset),
),
),
mpb.AppendDecorators(
decor.Name(chalk.Magenta.String(), decor.WC{W: 0}),
decor.Percentage(),
decor.Name(chalk.Reset.String(), decor.WC{W: 0})),
)
return p, bar
}
func showProgress(stop chan bool, wg *sync.WaitGroup, bucketName string,
fileDisplayString string, dstName string, isPipe bool) {
writerName := fmt.Sprintf("%s/%s", bucketName, dstName)
var p *mpb.Progress
var bar *mpb.Bar
if isPipe {
p, bar = makePipeBar(fileDisplayString)
} else {
p, bar = makeFileBar(fileDisplayString)
}
defer func() {
bar.SetTotal(1, true)
bar.SetCurrent(1)
p.Wait()
wg.Done()
}()
for {
updateBar(bar, writerName)
timeoutchan := make(chan bool)
go func() {
<-time.After(time.Millisecond * 25)
timeoutchan <- true
}()
select {
case <-timeoutchan:
case <-stop:
return
}
}
}