forked from qianlnk/pgbar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbar.go
More file actions
215 lines (183 loc) · 3.64 KB
/
bar.go
File metadata and controls
215 lines (183 loc) · 3.64 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package pgbar
import (
"fmt"
"sync"
"time"
)
type Bar struct {
mu sync.Mutex
line int
prefix string
total int
width int
advance chan bool
done chan bool
currents map[string]int
current int
before int
rate int
speed int
cost int
estimate int
fast int
slow int
srcUnit string
dstUnit string
change int
closed bool
}
var (
bar1 string
bar2 string
)
const (
defaultFast = 20
defaultSlow = 5
)
func initBar(width int) {
for i := 0; i < width; i++ {
bar1 += "="
bar2 += "-"
}
}
func NewBar(line int, prefix string, total int) *Bar {
if total <= 0 {
return nil
}
if line <= 0 {
gMaxLine++
line = gMaxLine
}
bar := &Bar{
line: line,
prefix: prefix,
total: total,
fast: defaultFast,
slow: defaultSlow,
width: 100,
advance: make(chan bool),
done: make(chan bool),
currents: make(map[string]int),
change: 1,
}
initBar(bar.width)
go bar.updateCost()
go bar.run()
return bar
}
func (b *Bar) SetUnit(src string, dst string, change int) {
b.srcUnit = src
b.dstUnit = dst
b.change = change
if b.change == 0 {
b.change = 1
}
}
func (b *Bar) SetSpeedSection(fast, slow int) {
if fast > slow {
b.fast, b.slow = fast, slow
} else {
b.fast, b.slow = slow, fast
}
}
func (b *Bar) Add(n ...int) {
b.mu.Lock()
defer b.mu.Unlock()
step := 1
if len(n) > 0 {
step = n[0]
}
b.current += step
lastRate := b.rate
lastSpeed := b.speed
b.count()
if lastRate != b.rate || lastSpeed != b.speed {
b.advance <- true
}
if b.rate >= 100 && !b.closed {
b.closed = true
close(b.done)
close(b.advance)
}
}
func (b *Bar) count() {
now := time.Now()
nowKey := now.Format("20060102150405")
befKey := now.Add(time.Minute * -1).Format("20060102150405")
b.currents[nowKey] = b.current
if v, ok := b.currents[befKey]; ok {
b.before = v
}
delete(b.currents, befKey)
b.rate = b.current * 100 / b.total
if b.cost == 0 {
b.speed = b.current * 100
} else if b.before == 0 {
b.speed = b.current * 100 / b.cost
} else {
b.speed = (b.current - b.before) * 100 / 60
}
if b.speed != 0 {
b.estimate = (b.total - b.current) * 100 / b.speed
}
}
func (b *Bar) updateCost() {
for {
select {
case <-time.After(time.Second):
b.cost++
b.mu.Lock()
b.count()
b.mu.Unlock()
b.advance <- true
case <-b.done:
return
}
}
}
func (b *Bar) run() {
for range b.advance {
printf(b.line, "\r%s", b.barMsg())
}
}
func (b *Bar) barMsg() string {
unit := ""
change := 1
if b.srcUnit != "" {
unit = b.srcUnit
}
if b.dstUnit != "" {
unit = b.dstUnit
change = b.change
}
prefix := fmt.Sprintf("%s", b.prefix)
rate := fmt.Sprintf("%3d%%", b.rate)
speed := fmt.Sprintf("%3.2f %s ps", 0.01*float64(b.speed)/float64(change), unit)
cost := b.timeFmt(b.cost)
estimate := b.timeFmt(b.estimate)
ct := fmt.Sprintf(" (%d/%d)", b.current, b.total)
barLen := b.width - len(prefix) - len(rate) - len(speed) - len(cost) - len(estimate) - len(ct) - 10
bar1Len := barLen * b.rate / 100
bar2Len := barLen - bar1Len
realBar1 := bar1[:bar1Len]
var realBar2 string
if bar2Len > 0 {
realBar2 = ">" + bar2[:bar2Len-1]
}
msg := fmt.Sprintf(`%s %s%s [%s%s] %s %s in: %s`, prefix, rate, ct, realBar1, realBar2, speed, cost, estimate)
switch {
case b.speed <= b.slow*100:
return "\033[0;31m" + msg + "\033[0m"
case b.speed > b.slow*100 && b.speed < b.fast*100:
return "\033[0;33m" + msg + "\033[0m"
default:
return "\033[0;32m" + msg + "\033[0m"
}
}
func (b *Bar) timeFmt(cost int) string {
var h, m, s int
h = cost / 3600
m = (cost - h*3600) / 60
s = cost - h*3600 - m*60
return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
}