-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpog.go
205 lines (176 loc) · 4.46 KB
/
pog.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package pog
import (
"fmt"
"io"
"log"
"os"
"strings"
"sync"
"time"
"github.com/fatih/color"
)
type Pogger struct {
out io.Writer
logger *log.Logger
status Status
stopCh chan struct{}
m sync.Mutex
initOnce sync.Once
exitHooks []func()
}
func NewPogger(out io.Writer, prefix string, flag int) *Pogger {
pogger := Pogger{
out: out,
logger: log.New(out, prefix, flag),
stopCh: make(chan struct{}),
}
go pogger.loop()
return &pogger
}
func (p *Pogger) SetStatus(status Status) {
p.m.Lock()
p.status = status
p.m.Unlock()
}
func (p *Pogger) Debug(v ...any) {
a := []any{color.WhiteString("[d]") + " "}
a = append(a, v...)
p.logger.Print(a...)
}
func (p *Pogger) Debugln(v ...any) {
a := []any{color.WhiteString("[d]")}
a = append(a, v...)
p.logger.Println(a...)
}
func (p *Pogger) Debugf(format string, v ...any) {
p.logger.Printf(color.WhiteString("[d]")+" "+format, v...)
}
func (p *Pogger) Info(v ...any) {
a := []any{"[i] "}
a = append(a, v...)
p.logger.Print(a...)
}
func (p *Pogger) Infoln(v ...any) {
a := []any{"[i]"}
a = append(a, v...)
p.logger.Println(a...)
}
func (p *Pogger) Infof(format string, v ...any) {
p.logger.Printf("[i] "+format, v...)
}
func (p *Pogger) Warn(v ...any) {
a := []any{color.YellowString("[w]") + " "}
a = append(a, v...)
p.logger.Print(a...)
}
func (p *Pogger) Warnln(v ...any) {
a := []any{color.YellowString("[w]")}
a = append(a, v...)
p.logger.Println(a...)
}
func (p *Pogger) Warnf(format string, v ...any) {
p.logger.Printf(color.YellowString("[w]")+" "+format, v...)
}
func (p *Pogger) Error(v ...any) {
a := []any{color.RedString("[E]") + " "}
a = append(a, v...)
p.logger.Print(a...)
}
func (p *Pogger) Errorln(v ...any) {
a := []any{color.RedString("[E]")}
a = append(a, v...)
p.logger.Println(a...)
}
func (p *Pogger) Errorf(format string, v ...any) {
p.logger.Printf(color.RedString("[E]")+" "+format, v...)
}
func (p *Pogger) Fatal(v ...any) {
p.Error(v...)
p.exit(1)
}
func (p *Pogger) Fatalln(v ...any) {
p.Errorln(v...)
p.exit(1)
}
func (p *Pogger) Fatalf(format string, v ...any) {
p.Errorf(format, v...)
p.exit(1)
}
func (p *Pogger) AddExitHook(fn func()) {
p.exitHooks = append(p.exitHooks, fn)
}
func (p *Pogger) Stop() {
close(p.stopCh)
}
func (p *Pogger) loop() {
cur := ""
pad := ""
if p.logger.Flags() != 0 {
pad = strings.Repeat(" ", 20)
}
L:
for i := 0; ; i = (i + 1) % 10 {
var s string
p.m.Lock()
b := []byte{' '}
if p.status != nil {
if i < 5 || !p.status.Throb() {
b[0] = p.status.Icon()
}
s = "[" + string(b) + "]"
if color := p.status.Color(); color != nil {
s = color.Sprint(s)
}
s += " " + p.status.Text()
}
p.m.Unlock()
if s != cur {
fmt.Fprintf(p.out, "\033[2K\r%s%s\r", pad, s)
cur = s
}
select {
case <-p.stopCh:
break L
case <-time.After(125 * time.Millisecond):
}
}
}
func (p *Pogger) exit(code int) {
for _, fn := range p.exitHooks {
fn()
}
os.Exit(code)
}
type Status interface {
Icon() byte
Text() string
Color() *color.Color
Throb() bool
}
var (
defaultPogger *Pogger
)
func InitDefault() {
defaultPogger = NewPogger(log.Default().Writer(), log.Default().Prefix(), log.Default().Flags())
}
func Default() *Pogger {
return defaultPogger
}
func SetStatus(status Status) { defaultPogger.SetStatus(status) }
func Debug(v ...any) { defaultPogger.Debug(v...) }
func Debugln(v ...any) { defaultPogger.Debugln(v...) }
func Debugf(format string, v ...any) { defaultPogger.Debugf(format, v...) }
func Info(v ...any) { defaultPogger.Info(v...) }
func Infoln(v ...any) { defaultPogger.Infoln(v...) }
func Infof(format string, v ...any) { defaultPogger.Infof(format, v...) }
func Warn(v ...any) { defaultPogger.Warn(v...) }
func Warnln(v ...any) { defaultPogger.Warnln(v...) }
func Warnf(format string, v ...any) { defaultPogger.Warnf(format, v...) }
func Error(v ...any) { defaultPogger.Error(v...) }
func Errorln(v ...any) { defaultPogger.Errorln(v...) }
func Errorf(format string, v ...any) { defaultPogger.Errorf(format, v...) }
func Fatal(v ...any) { defaultPogger.Fatal(v...) }
func Fatalln(v ...any) { defaultPogger.Fatalln(v...) }
func Fatalf(format string, v ...any) { defaultPogger.Fatalf(format, v...) }
func Stop() { defaultPogger.Stop() }
func AddExitHook(fn func()) { defaultPogger.AddExitHook(fn) }