-
Notifications
You must be signed in to change notification settings - Fork 1
/
decorator.go
79 lines (67 loc) · 2.07 KB
/
decorator.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
package gogress
import (
"math/rand"
"text/template"
"github.com/fatih/color"
)
type Decorator func(frame *FrameContext, cols int) string
const (
adElPlaceholder = "%_ad_el_%"
adElPlaceholderLen = len(adElPlaceholder)
)
var Decorators = template.FuncMap{
"bar": adaptativeDecorator(bar),
"prefix": wrapDecorator(prefix),
"counter": wrapDecorator(counter),
"timeSpent": wrapDecorator(timeSpent),
"speed": wrapDecorator(speed),
"percent": wrapDecorator(percent),
"timeLeft": wrapDecorator(timeLeft),
"spin": wrapDecorator(spin),
"frameNo": wrapDecorator(frameNo),
}
var Colors = template.FuncMap{
"black": color.New(color.FgBlack).SprintFunc(),
"red": color.New(color.FgRed).SprintFunc(),
"green": color.New(color.FgGreen).SprintFunc(),
"yellow": color.New(color.FgYellow).SprintFunc(),
"blue": color.New(color.FgBlue).SprintFunc(),
"magenta": color.New(color.FgMagenta).SprintFunc(),
"cyan": color.New(color.FgCyan).SprintFunc(),
"white": color.New(color.FgWhite).SprintFunc(),
"resetcolor": color.New(color.Reset).SprintFunc(),
"rndcolor": rndcolor,
}
func AddDecorator(name string, decorator Decorator) {
Decorators[name] = wrapDecorator(decorator)
}
func getColWidth(total int) int {
return total / 12
}
func wrapDecorator(decorator Decorator) Decorator {
return Decorator(func(frame *FrameContext, colsGrid int) string {
frame.Width -= 1 // line break
frame.elementNo += 1
cols := getColWidth(frame.Width) * colsGrid
frame.usedWidth += cols
response := decorator(frame, cols)
if len(response) >= cols {
return response[:cols]
} else {
return response
}
})
}
func adaptativeDecorator(decorator Decorator) Decorator {
return Decorator(func(frame *FrameContext, cols int) string {
frame.recalc = append(frame.recalc, decorator)
return adElPlaceholder
})
}
func RemoveDecorator(name string) {
delete(Decorators, name)
}
func rndcolor(s string) string {
c := rand.Intn(int(color.FgWhite-color.FgBlack)) + int(color.FgBlack) - 1
return color.New(color.Attribute(c)).Sprint(s)
}