argo-cd
47 строк · 984.0 Байт
1package util
2
3import (
4"fmt"
5)
6
7// Bar ...
8type Bar struct {
9percent int64 // progress percentage
10cur int64 // current progress
11total int64 // total value for progress
12rate string // the actual progress bar to be printed
13graph string // the fill value for progress bar
14}
15
16func (bar *Bar) NewOption(start, total int64) {
17bar.cur = start
18bar.total = total
19if bar.graph == "" {
20bar.graph = "█"
21}
22bar.percent = bar.getPercent()
23for i := 0; i < int(bar.percent); i += 2 {
24bar.rate += bar.graph // initial progress position
25}
26}
27
28func (bar *Bar) getPercent() int64 {
29return int64((float32(bar.cur) / float32(bar.total)) * 100)
30}
31
32func (bar *Bar) Increment() {
33bar.cur += 1
34}
35
36func (bar *Bar) Play() {
37last := bar.percent
38bar.percent = bar.getPercent()
39if bar.percent != last && bar.percent%2 == 0 {
40bar.rate += bar.graph
41}
42fmt.Printf("\r[%-50s]%3d%% %8d/%d", bar.rate, bar.percent, bar.cur, bar.total)
43}
44
45func (bar *Bar) Finish() {
46fmt.Println()
47}
48