-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
lib.go
171 lines (154 loc) · 3.54 KB
/
lib.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
package print
import (
"github.com/jedib0t/go-pretty/v6/text"
"github.com/alajmo/mani/core"
)
// Format map against go-pretty/table
func GetFormat(s string) text.Format {
switch s {
case "default":
return text.FormatDefault
case "lower":
return text.FormatLower
case "title":
return text.FormatTitle
case "upper":
return text.FormatUpper
default:
return text.FormatDefault
}
}
// Align map against go-pretty/table
func GetAlign(s string) text.Align {
switch s {
case "left":
return text.AlignLeft
case "center":
return text.AlignCenter
case "justify":
return text.AlignJustify
case "right":
return text.AlignRight
default:
return text.AlignLeft
}
}
// Foreground color map against go-pretty/table
func GetFg(s string) *text.Color {
switch s {
// Normal colors
case "black":
return core.Ptr(text.FgBlack)
case "red":
return core.Ptr(text.FgRed)
case "green":
return core.Ptr(text.FgGreen)
case "yellow":
return core.Ptr(text.FgYellow)
case "blue":
return core.Ptr(text.FgBlue)
case "magenta":
return core.Ptr(text.FgMagenta)
case "cyan":
return core.Ptr(text.FgCyan)
case "white":
return core.Ptr(text.FgWhite)
// High-intensity colors
case "hi_black":
return core.Ptr(text.FgHiBlack)
case "hi_red":
return core.Ptr(text.FgHiRed)
case "hi_green":
return core.Ptr(text.FgHiGreen)
case "hi_yellow":
return core.Ptr(text.FgHiYellow)
case "hi_blue":
return core.Ptr(text.FgHiBlue)
case "hi_magenta":
return core.Ptr(text.FgHiMagenta)
case "hi_cyan":
return core.Ptr(text.FgHiCyan)
case "hi_white":
return core.Ptr(text.FgHiWhite)
default:
return nil
}
}
// Background color map against go-pretty/table
func GetBg(s string) *text.Color {
switch s {
// Normal colors
case "black":
return core.Ptr(text.BgBlack)
case "red":
return core.Ptr(text.BgRed)
case "green":
return core.Ptr(text.BgGreen)
case "yellow":
return core.Ptr(text.BgYellow)
case "blue":
return core.Ptr(text.BgBlue)
case "magenta":
return core.Ptr(text.BgMagenta)
case "cyan":
return core.Ptr(text.BgCyan)
case "white":
return core.Ptr(text.BgWhite)
// High-intensity colors
case "hi_black":
return core.Ptr(text.BgHiBlack)
case "hi_red":
return core.Ptr(text.BgHiRed)
case "hi_green":
return core.Ptr(text.BgHiGreen)
case "hi_yellow":
return core.Ptr(text.BgHiYellow)
case "hi_blue":
return core.Ptr(text.BgHiBlue)
case "hi_magenta":
return core.Ptr(text.BgHiMagenta)
case "hi_cyan":
return core.Ptr(text.BgHiCyan)
case "hi_white":
return core.Ptr(text.BgHiWhite)
default:
return nil
}
}
// Attr (color) map against go-pretty/table (attributes belong to the same types as fg/bg)
func GetAttr(s string) *text.Color {
switch s {
case "normal":
return nil
case "bold":
return core.Ptr(text.Bold)
case "faint":
return core.Ptr(text.Faint)
case "italic":
return core.Ptr(text.Italic)
case "underline":
return core.Ptr(text.Underline)
case "crossed_out":
return core.Ptr(text.CrossedOut)
default:
return nil
}
}
// Combine colors and attributes in one slice. We check if the values are valid, otherwise
// we get a nil pointer, in which case the values are not appended to the colors slice.
func combineColors(fg *string, bg *string, attr *string) text.Colors {
colors := text.Colors{}
fgVal := GetFg(*fg)
if *fg != "" && fgVal != nil {
colors = append(colors, *fgVal)
}
bgVal := GetBg(*bg)
if *bg != "" && bgVal != nil {
colors = append(colors, *bgVal)
}
attrVal := GetAttr(*attr)
if *attr != "" && attrVal != nil {
colors = append(colors, *attrVal)
}
return colors
}