Skip to content

Commit e6a0051

Browse files
committed
⌛ added a progress bar
1 parent 11c3173 commit e6a0051

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

progress/progress.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package progress
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type Bar struct {
8+
percent int64 // progress percentage
9+
cur int64 // current progress
10+
total int64 // total value for progress
11+
rate string // the actual progress bar to be printed
12+
graph string // the fill value for progress bar
13+
}
14+
15+
func (bar *Bar) NewOption(start, total int64) {
16+
bar.cur = start
17+
bar.total = total
18+
if bar.graph == "" {
19+
bar.graph = "█"
20+
}
21+
bar.percent = bar.getPercent()
22+
for i := 0; i < int(bar.percent); i += 2 {
23+
bar.rate += bar.graph // initial progress position
24+
}
25+
}
26+
func (bar *Bar) getPercent() int64 {
27+
return int64(float32(bar.cur) / float32(bar.total) * 100)
28+
}
29+
30+
func (bar *Bar) NewOptionWithGraph(start, total int64, graph string) {
31+
bar.graph = graph
32+
bar.NewOption(start, total)
33+
}
34+
35+
func (bar *Bar) Play(cur int64) {
36+
bar.cur = cur
37+
last := bar.percent
38+
bar.percent = bar.getPercent()
39+
if bar.percent != last && bar.percent%2 == 0 {
40+
calc := int(100 / (bar.total * 2))
41+
for x := 0; x <= calc; x++ {
42+
bar.rate = bar.rate + bar.graph
43+
}
44+
}
45+
46+
fmt.Printf("\r\u001b[32m[%-50s]%3d%% %8d/%d \033[5;36m", bar.rate, bar.percent, bar.cur, bar.total)
47+
}
48+
49+
func (bar *Bar) Finish() {
50+
fmt.Println()
51+
}

0 commit comments

Comments
 (0)