-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress.go
50 lines (43 loc) · 1.17 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
package gokit
import "fmt"
type Bar struct {
percent int64 //百分比
cur int64 //当前进度位置
total int64 //总进度
rate string //进度条
graph string //显示符号
}
func (bar *Bar) NewOption(start, total int64) {
bar.cur = start
bar.total = total
if bar.graph == "" {
bar.graph = "█"
}
bar.percent = bar.getPercent()
for i := 0; i < int(bar.percent); i += 2 {
bar.rate += bar.graph //初始化进度条位置
}
}
func (bar *Bar) getPercent() int64 {
return int64(float32(bar.cur) / float32(bar.total) * 100)
}
func (bar *Bar) NewOptionWithGraph(start, total int64, graph string) {
bar.graph = graph
bar.NewOption(start, total)
}
func (bar *Bar) Play(cur int64) {
bar.cur = cur
last := bar.percent
bar.percent = bar.getPercent()
if bar.percent != last && bar.percent%2 == 0 {
bar.rate += bar.graph
}
// \r 是返回行首,也叫回车,重新复写当前行
// %d 十进制表示
// %% 字面上得百分号
// -50 在右侧而非左侧填充50个字符,100percent每2percent填充一次,填充50次
fmt.Printf("\r[%-50s]%3d%% %8d/%d", bar.rate, bar.percent, bar.cur, bar.total)
}
func (bar *Bar) Finish() {
fmt.Println()
}