-
Notifications
You must be signed in to change notification settings - Fork 3
/
tmpl_colors.go
201 lines (170 loc) · 4.42 KB
/
tmpl_colors.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
// Copyright 2023 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package internal
import (
"fmt"
"golang.org/x/term"
"io"
"text/template"
)
// color code generation and terminal check is adopted from github.com/go-kit/log/term
// ColorNames names can be used for generic color function.
// quick foreground color function with same name is also availalbe in template
var ColorNames = map[string]Color{
"black": Black,
"red": Red,
"green": Green,
"yellow": Yellow,
"blue": Blue,
"magenta": Magenta,
"cyan": Cyan,
"gray": Gray,
"black_b": BoldBlack,
"red_b": BoldRed,
"green_b": BoldGreen,
"yellow_b": BoldYellow,
"blue_b": BoldBlue,
"magenta_b": BoldMagenta,
"cyan_b": BoldCyan,
"gray_b": BoldGray,
}
var (
TmplColorFuncMap template.FuncMap
TmplColorFuncMapNonTerm template.FuncMap
)
type Color uint8
const (
Default = Color(iota)
Black
Red
Green
Yellow
Blue
Magenta
Cyan
Gray
BoldBlack
BoldRed
BoldGreen
BoldYellow
BoldBlue
BoldMagenta
BoldCyan
BoldGray
numColors
)
var (
FgColors []string
BgColors []string
ResetColor = "\x1b[39;49;22m"
)
// Implementations adopted from github.com/go-kit/log/term
func init() {
// Default
//FgColors = append(FgColors, "\x1b[39m")
//BgColors = append(BgColors, "\x1b[49m")
FgColors = append(FgColors, "")
BgColors = append(BgColors, "")
// dark colors
for color := Black; color < BoldBlack; color++ {
FgColors = append(FgColors, fmt.Sprintf("\x1b[%dm", 30+color-Black))
BgColors = append(BgColors, fmt.Sprintf("\x1b[%dm", 40+color-Black))
}
// bright colors
for color := BoldBlack; color < numColors; color++ {
FgColors = append(FgColors, fmt.Sprintf("\x1b[%d;1m", 30+color-BoldBlack))
BgColors = append(BgColors, fmt.Sprintf("\x1b[%d;1m", 40+color-BoldBlack))
}
// prepare quick color function Map
TmplColorFuncMap = template.FuncMap{"color": Colored}
TmplColorFuncMapNonTerm = template.FuncMap{"color":NoopColored}
for k, v := range ColorNames {
TmplColorFuncMap[k] = MakeQuickColorFunc(v)
TmplColorFuncMapNonTerm[k] = NoopQuickColor
}
}
// fder matches os.File.Fd()
type fder interface {
Fd() uintptr
}
// IsTerminal returns true if w writes to a terminal.
// Implementations adopted from github.com/go-kit/log/term
func IsTerminal(w io.Writer) bool {
if v, ok := w.(fder); ok {
return term.IsTerminal(int(v.Fd()))
}
return false
}
func ColoredWithCode(s interface{}, fg, bg Color) string {
var fgStr, bgStr string
if fg < numColors {
fgStr = FgColors[fg]
}
if bg < numColors {
bgStr = BgColors[bg]
}
return fgStr + bgStr + Sprint(s) + ResetColor
}
func ColoredWithName(s interface{}, fgName, bgName string) string {
fg, _ := ColorNames[fgName]
bg, _ := ColorNames[bgName]
return ColoredWithCode(s, fg, bg)
}
// Colored takes 0, 1, or 2 color names
// when present, they should be in order of fgName, bgName
func Colored(s interface{}, colorNames...string) string {
switch len(colorNames) {
case 0:
return Sprint(s)
case 1:
return ColoredWithName(s, colorNames[0], "")
default:
return ColoredWithName(s, colorNames[0], colorNames[1])
}
}
func MakeQuickColorFunc(fg Color) func(s interface{}) string {
return func(s interface{}) string {
return ColoredWithCode(s, fg, Default)
}
}
func NoopColored(v interface{}, _...string) interface{} {
return v
}
func NoopQuickColor(v interface{}) interface{} {
return v
}
func DebugShowcase() {
loop := func(colorFunc func(s string, c Color) string) {
count := 0
for k, v := range ColorNames {
name := fmt.Sprintf("%-12s", k)
fmt.Printf("%s ", colorFunc(name, v))
count ++
if count % 4 == 0 {
fmt.Println()
}
}
}
fgColorFunc := func(s string, c Color) string {
return ColoredWithCode(s, c, Default)
}
bgColorFunc := func(s string, c Color) string {
return ColoredWithCode(s, Default, c)
}
// run
loop(fgColorFunc)
loop(bgColorFunc)
}