/
progressbar.go
145 lines (128 loc) · 3.26 KB
/
progressbar.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
package http
import (
"fmt"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
type ConsoleBar struct {
All int64
Now int64
Width int
NowWidth int
Interval int
Last time.Time
LastMsg string
LastWrite string
LastBar string
}
var (
locker = sync.Mutex{}
)
func (pro *ConsoleBar) Add(i int) int {
pro.Now += int64(i)
if time.Now().Sub(pro.Last) > time.Duration(pro.Interval)*time.Second {
nowWidth := int(float32(pro.Now) / float32(pro.All) * float32(pro.Width))
pro.NowWidth = nowWidth
fmt.Print(pro.LastMsg, "\r")
pro.Last = time.Now()
pro.Write()
}
return int(pro.Now)
}
func (pro *ConsoleBar) Update() {
// return pro.Add(1)
}
func (pro *ConsoleBar) Increment() int {
return pro.Add(1)
}
func (pro *ConsoleBar) Reset() {
pro.Now = 0
pro.LastMsg = ""
}
func NewConsoleBar(all int64) (bar *ConsoleBar, err error) {
bar = &ConsoleBar{
All: all,
Interval: 1,
Last: time.Now(),
}
if runtime.GOOS != "windows" {
cmd := exec.Command("stty", "size")
cmd.Stdin = os.Stdin
c, err := cmd.Output()
if err != nil {
bar.Width = 50
// log.Fatal(err)
}
size := strings.Fields(string(c))[1]
o, ierr := strconv.Atoi(size)
if ierr != nil {
return nil, ierr
}
bar.Width = o - 2
} else {
bar.Width = 50
}
return
}
func (pro *ConsoleBar) Write(args ...interface{}) {
var realMsg string
if len(args) == 0 {
if strings.HasPrefix(pro.LastMsg, ":") {
realMsg = fmt.Sprintf("%.2f%% || %d/%d :", pro.GetPercent(), pro.Now, pro.All) + strings.SplitN(pro.LastMsg, ":", 2)[1]
} else {
realMsg = fmt.Sprintf("%.2f%% || %d/%d :", pro.GetPercent(), pro.Now, pro.All) + pro.LastMsg
}
} else {
realMsg = fmt.Sprintf("%.2f%% || %d/%d :", pro.GetPercent(), pro.Now, pro.All) + Blue(args...)
pro.LastMsg = realMsg
}
if len(realMsg) < pro.Width {
realMsg += LANGEMPTY
}
realMsg = realMsg[:pro.Width]
if len(realMsg) >= pro.NowWidth {
pre := realMsg[:pro.NowWidth]
pro.LastBar = BlueBack(pre) + realMsg[pro.NowWidth:] + "\r"
fmt.Print(BlueBack(pre) + realMsg[pro.NowWidth:] + "\r")
} else {
realMsg += strings.Repeat(" ", pro.NowWidth-len(realMsg))
// = BlueBack(pre) + realMsg[pro.NowWidth:] + "\r"
pre := realMsg[:pro.NowWidth]
pro.LastBar = BlueBack(pre) + realMsg[pro.NowWidth:] + "\r"
fmt.Print(BlueBack(pre) + realMsg[pro.NowWidth:] + "\r")
}
}
func (pro *ConsoleBar) GetPercent() float32 {
return float32(pro.Now) / float32(pro.All) * float32(100)
}
func (pro *ConsoleBar) Error(err error) {
locker.Lock()
defer locker.Unlock()
fmt.Print(LANGEMPTY[:pro.Width] + "\r")
fmt.Println(Red(fmt.Sprintf("[!] :")), Bold(err))
fmt.Print(pro.LastBar)
}
func (pro *ConsoleBar) Println(args ...interface{}) {
locker.Lock()
defer locker.Unlock()
fmt.Print(LANGEMPTY[:pro.Width] + "\r")
fmt.Println(Green(fmt.Sprintf("[+] :")), strings.TrimSpace(Bold(args...)))
fmt.Print(pro.LastBar)
}
func (pro *ConsoleBar) Finished() {
locker.Lock()
defer locker.Unlock()
pro.Reset()
fmt.Print(LANGEMPTY[:pro.Width] + "\r")
}
func (pro *ConsoleBar) SetAll(all int) {
pro.All = int64(all)
}
func (pro *ConsoleBar) SetMsg(msg string) {
pro.Write(msg)
}