-
Notifications
You must be signed in to change notification settings - Fork 10
/
decorators.go
162 lines (143 loc) · 2.85 KB
/
decorators.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package getparty
import (
"fmt"
"math"
"sync"
"sync/atomic"
"time"
"github.com/vbauerster/mpb/v5/decor"
)
type message struct {
msg string
times int
final bool
done chan struct{}
}
type msgGate struct {
prefix string
msgCh chan *message
done chan struct{}
}
func newMsgGate(prefix string, quiet bool) msgGate {
gate := msgGate{
prefix: prefix,
done: make(chan struct{}),
}
if !quiet {
gate.msgCh = make(chan *message, 4)
}
return gate
}
func (s msgGate) flash(msg *message) {
msg.times = 14
msg.msg = fmt.Sprintf("%s:%s", s.prefix, msg.msg)
select {
case s.msgCh <- msg:
case <-s.done:
if msg.final && msg.done != nil {
close(msg.done)
}
}
}
type mainDecorator struct {
decor.WC
curTry *uint32
name string
format string
flashMsg *message
messages []*message
gate msgGate
}
func newMainDecorator(curTry *uint32, format, name string, gate msgGate, wc decor.WC) decor.Decorator {
d := &mainDecorator{
WC: wc.Init(),
curTry: curTry,
name: name,
format: format,
gate: gate,
}
return d
}
func (d *mainDecorator) depleteMessages() {
for {
select {
case m := <-d.gate.msgCh:
d.messages = append(d.messages, m)
default:
return
}
}
}
func (d *mainDecorator) Decor(stat decor.Statistics) string {
if !stat.Completed && d.flashMsg != nil {
m := d.flashMsg.msg
if d.flashMsg.times > 0 {
d.flashMsg.times--
} else if d.flashMsg.final {
if d.flashMsg.done != nil {
close(d.flashMsg.done)
d.flashMsg.done = nil
}
} else {
d.flashMsg = nil
}
return d.FormatMsg(m)
}
d.depleteMessages()
if len(d.messages) > 0 {
d.flashMsg = d.messages[0]
copy(d.messages, d.messages[1:])
d.messages = d.messages[:len(d.messages)-1]
d.flashMsg.times--
return d.FormatMsg(d.flashMsg.msg)
}
name := d.name
if atomic.LoadUint32(&globTry) > 0 {
name = fmt.Sprintf("%s:R%02d", name, atomic.LoadUint32(d.curTry))
}
return d.FormatMsg(fmt.Sprintf(d.format, name, decor.SizeB1024(stat.Total)))
}
func (d *mainDecorator) Shutdown() {
close(d.gate.done)
}
type peak struct {
decor.WC
format string
msg string
n int64
d time.Duration
max float64
once sync.Once
}
func newSpeedPeak(format string, wc decor.WC) decor.Decorator {
d := &peak{
WC: wc.Init(),
format: format,
}
return d
}
func (s *peak) EwmaUpdate(n int64, dur time.Duration) {
s.n += n
s.d += dur
if s.n >= 1024*64 {
durPerByte := float64(s.d) / float64(s.n)
s.max = math.Max(s.max, 1/durPerByte)
s.n, s.d = 0, 0
}
}
func (s *peak) onComplete() {
if s.max == 0 && s.n != 0 {
durPerByte := float64(s.d) / float64(s.n)
s.max = 1 / durPerByte
}
s.msg = fmt.Sprintf(
s.format,
decor.FmtAsSpeed(decor.SizeB1024(math.Round(s.max*1e9))),
)
}
func (s *peak) Decor(stat decor.Statistics) string {
if stat.Completed {
s.once.Do(s.onComplete)
}
return s.FormatMsg(s.msg)
}