-
Notifications
You must be signed in to change notification settings - Fork 6
/
progress.go
84 lines (72 loc) · 1.42 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package progress
import (
"fmt"
"github.com/gosuri/uilive"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"sync"
)
type State int
const (
InitState State = iota
ErrorState
SuccessState
)
func (s State) String() string {
switch s {
case InitState:
return "⌛️"
case ErrorState:
return "❌"
case SuccessState:
return "✅"
default:
return ""
}
}
type Progress struct {
mu sync.Mutex
names []string
states map[string]State
writer *uilive.Writer
}
func New() Progress {
return Progress{
states: map[string]State{},
writer: uilive.New(),
}
}
func (p *Progress) Draw() {
var output string
for _, name := range p.names {
output += fmt.Sprintf("%s %s\n", p.states[name], name)
}
_, _ = fmt.Fprint(p.writer, output)
_ = p.writer.Flush()
}
func (p *Progress) add(name string, state State) {
p.mu.Lock()
defer p.mu.Unlock()
if _, ok := p.states[name]; !ok {
p.names = append(p.names, name)
}
p.states[name] = state
}
func (p *Progress) Init(name string) {
if !viper.GetBool("verbose") {
p.add(name, InitState)
}
logrus.Debugf("%s transitioned to Init state", name)
}
func (p *Progress) Error(name string) {
if !viper.GetBool("verbose") {
p.add(name, ErrorState)
}
logrus.Debugf("%s transitioned to Error state", name)
}
func (p *Progress) Success(name string) {
if !viper.GetBool("verbose") {
p.add(name, SuccessState)
}
logrus.Debugf("%s transitioned to Success state", name)
}