-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyle.go
63 lines (53 loc) · 1.47 KB
/
style.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
package ps1
import (
"fmt"
"text/template"
"github.com/fatih/color"
)
var colorFuncs = template.FuncMap{
"fgcolor": fgColor,
"bgcolor": bgColor,
"attr": attr,
}
func fgColor(name string, text ...interface{}) (string, error) {
fgColors := map[string]color.Attribute{
"black": color.FgBlack,
"blue": color.FgBlue,
"cyan": color.FgCyan,
"green": color.FgGreen,
"magenta": color.FgMagenta,
"red": color.FgRed,
"white": color.FgWhite,
"yellow": color.FgYellow,
}
return printWithAttr("fgcolor", fgColors, name, text...)
}
func bgColor(name string, text ...interface{}) (string, error) {
bgColors := map[string]color.Attribute{
"black": color.BgBlack,
"blue": color.BgBlue,
"cyan": color.BgCyan,
"green": color.BgGreen,
"magenta": color.BgMagenta,
"red": color.BgRed,
"white": color.BgWhite,
"yellow": color.BgYellow,
}
return printWithAttr("bgcolor", bgColors, name, text...)
}
func attr(name string, text ...interface{}) (string, error) {
attrs := map[string]color.Attribute{
"bold": color.Bold,
"invert": color.ReverseVideo,
"italicize": color.Italic,
"underline": color.Underline,
}
return printWithAttr("attr", attrs, name, text...)
}
func printWithAttr(attrType string, attrs map[string]color.Attribute, attr string, text ...interface{}) (string, error) {
val, ok := attrs[attr]
if !ok {
return "", fmt.Errorf(`%s "%s" not found`, attrType, attr)
}
return color.New(val).Sprint(text...), nil
}