-
Notifications
You must be signed in to change notification settings - Fork 38
/
progress.go
66 lines (48 loc) · 1 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package utils
import (
"fmt"
"sync"
"time"
"github.com/briandowns/spinner"
)
var _spinner *spinner.Spinner
var _progressSteps int
var _progressStep int
var _progressShow bool
var _mutex sync.Mutex
var _supressProgress bool
func init() {
_mutex = sync.Mutex{}
_spinner = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
}
func SupressProgress(hide bool) {
_supressProgress = hide
}
func ShowProgress() {
if _supressProgress {
return
}
_progressPercentage := 0
if _progressSteps > 0 {
_progressPercentage = int(float32(_progressStep+1) * 100.0 / float32(_progressSteps))
if _progressPercentage > 100 {
_progressPercentage = 100
}
}
_spinner.Prefix = "["
_spinner.Suffix = fmt.Sprintf("] Progress: %d%%", _progressPercentage)
_spinner.Start()
_progressShow = true
}
func HideProgress() {
_spinner.Stop()
_progressShow = false
}
func IncreaseProgressStep() {
_mutex.Lock()
defer _mutex.Unlock()
_progressStep++
}
func SetProgressSteps(steps int) {
_progressSteps = steps
}