/
color.go
99 lines (86 loc) · 2.83 KB
/
color.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
package util
import (
"sort"
"github.com/fatih/color"
)
// ColorInfo returns a new function that returns info-colorized (green) strings for the
// given arguments with fmt.Sprint().
var ColorInfo = color.New(color.FgGreen).SprintFunc()
// ColorStatus returns a new function that returns status-colorized (blue) strings for the
// given arguments with fmt.Sprint().
var ColorStatus = color.New(color.FgBlue).SprintFunc()
// ColorWarning returns a new function that returns warning-colorized (yellow) strings for the
// given arguments with fmt.Sprint().
var ColorWarning = color.New(color.FgYellow).SprintFunc()
// ColorError returns a new function that returns error-colorized (red) strings for the
// given arguments with fmt.Sprint().
var ColorError = color.New(color.FgRed).SprintFunc()
var colorMap = map[string]color.Attribute{
// formatting
"bold": color.Bold,
"faint": color.Faint,
"italic": color.Italic,
"underline": color.Underline,
"blinkslow": color.BlinkSlow,
"blinkrapid": color.BlinkRapid,
"reversevideo": color.ReverseVideo,
"concealed": color.Concealed,
"crossedout": color.CrossedOut,
// Foreground text colors
"black": color.FgBlack,
"red": color.FgRed,
"green": color.FgGreen,
"yellow": color.FgYellow,
"blue": color.FgBlue,
"magenta": color.FgMagenta,
"cyan": color.FgCyan,
"white": color.FgWhite,
// Foreground Hi-Intensity text colors
"hiblack": color.FgHiBlack,
"hired": color.FgHiRed,
"higreen": color.FgHiGreen,
"hiyellow": color.FgHiYellow,
"hiblue": color.FgHiBlue,
"himagenta": color.FgHiMagenta,
"hicyan": color.FgHiCyan,
"hiwhite": color.FgHiWhite,
// Background text colors
"bgblack": color.BgBlack,
"bgred": color.BgRed,
"bggreen": color.BgGreen,
"bgyellow": color.BgYellow,
"BgBlue": color.BgBlue,
"bgmagenta": color.BgMagenta,
"bgcyan": color.BgCyan,
"bgwhite": color.BgWhite,
// Background Hi-Intensity text colors
"bghiblack": color.BgHiBlack,
"bghired": color.BgHiRed,
"bghigreen": color.BgHiGreen,
"bghiyellow": color.BgHiYellow,
"bghiblue": color.BgHiBlue,
"bghimagenta": color.BgHiMagenta,
"bghicyan": color.BgHiCyan,
"bghiwhite": color.BgHiWhite,
}
// GetColor returns the color for the list of colour names and option name
func GetColor(optionName string, colorNames []string) (*color.Color, error) {
attributes := []color.Attribute{}
for _, colorName := range colorNames {
a := colorMap[colorName]
if a == color.Attribute(0) {
return nil, InvalidOption(optionName, colorName, ColorNameValues())
}
attributes = append(attributes, a)
}
return color.New(attributes...), nil
}
// ColorNameValues returns all the color names sorted
func ColorNameValues() []string {
answer := []string{}
for k := range colorMap {
answer = append(answer, k)
}
sort.Strings(answer)
return answer
}