-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtint.go
More file actions
284 lines (244 loc) · 8.04 KB
/
Copy pathtint.go
File metadata and controls
284 lines (244 loc) · 8.04 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package tint
import (
"fmt"
"io"
"os"
"strconv"
"strings"
"sync"
)
// Color defines a custom color object which is defined by ANSI SGR parameters.
type Color struct {
params []Attribute
noColor *bool
}
// New creates a new Color with the given attributes.
func New(attrs ...Attribute) *Color {
c := &Color{params: make([]Attribute, 0, len(attrs))}
c.Add(attrs...)
return c
}
// Add adds the given attributes to the color.
func (c *Color) Add(attrs ...Attribute) *Color {
c.params = append(c.params, attrs...)
return c
}
// Equals reports whether c and other have the same attributes.
func (c *Color) Equals(other *Color) bool {
if len(c.params) != len(other.params) {
return false
}
for i, a := range c.params {
if a != other.params[i] {
return false
}
}
return true
}
// Sprint formats using the default formats for its operands and returns the
// resulting string wrapped in ANSI color sequences.
func (c *Color) Sprint(a ...interface{}) string {
return c.wrap(fmt.Sprint(a...))
}
// Sprintf formats according to a format specifier and returns the resulting
// string wrapped in ANSI color sequences.
func (c *Color) Sprintf(format string, a ...interface{}) string {
return c.wrap(fmt.Sprintf(format, a...))
}
// Sprintln formats using the default formats for its operands and returns the
// resulting string with a newline, wrapped in ANSI color sequences.
func (c *Color) Sprintln(a ...interface{}) string {
return c.wrap(fmt.Sprintln(a...))
}
// SprintFunc returns a function that wraps Sprint with the current color settings.
func (c *Color) SprintFunc() func(a ...interface{}) string {
return func(a ...interface{}) string {
return c.Sprint(a...)
}
}
// SprintfFunc returns a function that wraps Sprintf with the current color settings.
func (c *Color) SprintfFunc() func(format string, a ...interface{}) string {
return func(format string, a ...interface{}) string {
return c.Sprintf(format, a...)
}
}
// SprintlnFunc returns a function that wraps Sprintln with the current color settings.
func (c *Color) SprintlnFunc() func(a ...interface{}) string {
return func(a ...interface{}) string {
return c.Sprintln(a...)
}
}
// PrintlnFunc returns a function that wraps Println with the current color settings.
// Note: the returned function has no return values, matching fatih/color's API.
func (c *Color) PrintlnFunc() func(a ...interface{}) {
return func(a ...interface{}) {
_, _ = c.Println(a...)
}
}
// PrintFunc returns a function that wraps Print with the current color settings.
func (c *Color) PrintFunc() func(a ...interface{}) {
return func(a ...interface{}) {
_, _ = c.Print(a...)
}
}
// PrintfFunc returns a function that wraps Printf with the current color settings.
func (c *Color) PrintfFunc() func(format string, a ...interface{}) {
return func(format string, a ...interface{}) {
_, _ = c.Printf(format, a...)
}
}
// FprintFunc returns a function that wraps Fprint with the current color settings.
func (c *Color) FprintFunc() func(w io.Writer, a ...interface{}) {
return func(w io.Writer, a ...interface{}) {
_, _ = c.Fprint(w, a...)
}
}
// FprintfFunc returns a function that wraps Fprintf with the current color settings.
func (c *Color) FprintfFunc() func(w io.Writer, format string, a ...interface{}) {
return func(w io.Writer, format string, a ...interface{}) {
_, _ = c.Fprintf(w, format, a...)
}
}
// FprintlnFunc returns a function that wraps Fprintln with the current color settings.
func (c *Color) FprintlnFunc() func(w io.Writer, a ...interface{}) {
return func(w io.Writer, a ...interface{}) {
_, _ = c.Fprintln(w, a...)
}
}
// Print formats using the default formats and writes to standard output.
func (c *Color) Print(a ...interface{}) (int, error) {
return c.Fprint(Output, a...)
}
// Printf formats according to a format specifier and writes to standard output.
func (c *Color) Printf(format string, a ...interface{}) (int, error) {
return c.Fprintf(Output, format, a...)
}
// Println formats using the default formats and writes to standard output,
// followed by a newline.
func (c *Color) Println(a ...interface{}) (int, error) {
return c.Fprintln(Output, a...)
}
// Fprint formats using the default formats and writes to w.
func (c *Color) Fprint(w io.Writer, a ...interface{}) (int, error) {
s := c.wrap(fmt.Sprint(a...))
return fmt.Fprint(w, s)
}
// Fprintf formats according to a format specifier and writes to w.
func (c *Color) Fprintf(w io.Writer, format string, a ...interface{}) (int, error) {
s := c.wrap(fmt.Sprintf(format, a...))
return fmt.Fprint(w, s)
}
// Fprintln formats using the default formats and writes to w, followed by a newline.
func (c *Color) Fprintln(w io.Writer, a ...interface{}) (int, error) {
s := c.wrap(fmt.Sprintln(a...))
return fmt.Fprint(w, s)
}
// Set sets the color attributes on the default output and returns the color
// for chaining. This is useful for stateful color changes (e.g., setting a
// color, printing multiple lines, then unsetting).
func (c *Color) Set() *Color {
if c.isNoColor() {
return c
}
_, _ = fmt.Fprint(Output, c.sequence())
return c
}
// Unset resets the color on the default output.
func (c *Color) Unset() {
if c.isNoColor() {
return
}
_, _ = fmt.Fprint(Output, "\033[0m")
}
// DisableColor disables color output for this Color instance.
func (c *Color) DisableColor() {
v := true
c.noColor = &v
}
// EnableColor enables color output for this Color instance.
func (c *Color) EnableColor() {
v := false
c.noColor = &v
}
// isNoColor returns whether color output is disabled for this Color instance.
func (c *Color) isNoColor() bool {
// Per-instance override takes precedence.
if c.noColor != nil {
return *c.noColor
}
// Global setting.
return NoColor
}
// sequence returns the ANSI escape sequence prefix for the color's attributes.
func (c *Color) sequence() string {
params := make([]string, len(c.params))
for i, p := range c.params {
params[i] = strconv.Itoa(int(p))
}
return "\033[" + strings.Join(params, ";") + "m"
}
// wrap wraps s with ANSI color sequences if color is enabled.
func (c *Color) wrap(s string) string {
if c.isNoColor() {
return s
}
return c.sequence() + s + "\033[0m"
}
// colorPrint is the internal print function used by package-level convenience
// functions. It is safe for concurrent use.
func colorPrint(format string, p Attribute, a ...interface{}) {
c := getCachedColor(p)
if len(a) == 0 {
_, _ = c.Print(format)
} else {
_, _ = c.Printf(format, a...)
}
}
// colorString is the internal string function used by package-level convenience
// functions. It is safe for concurrent use.
func colorString(format string, p Attribute, a ...interface{}) string {
c := getCachedColor(p)
if len(a) == 0 {
return c.Sprint(format)
}
return c.Sprintf(format, a...)
}
// colorCache stores cached Color instances for each attribute, avoiding
// allocations on repeated use of convenience functions.
var (
colorCache = make(map[Attribute]*Color)
colorCacheMu sync.Mutex
)
// getCachedColor returns a cached Color for the given attribute.
func getCachedColor(p Attribute) *Color {
colorCacheMu.Lock()
defer colorCacheMu.Unlock()
c, ok := colorCache[p]
if !ok {
c = New(p)
colorCache[p] = c
}
return c
}
// Global controls.
var (
// NoColor disables all color output when set to true.
// It respects the NO_COLOR environment variable (https://no-color.org/)
// and TERM=dumb.
NoColor = noColorFromEnv()
// Output is the default writer for color output (typically os.Stdout).
Output io.Writer = os.Stdout
// Error is the default writer for error color output (typically os.Stderr).
Error io.Writer = os.Stderr
)
func noColorFromEnv() bool {
return os.Getenv("NO_COLOR") != "" || os.Getenv("TERM") == "dumb"
}
// Unset resets the terminal colors on the default output.
func Unset() {
_, _ = fmt.Fprint(Output, "\033[0m")
}
// Internal fmt wrappers used by hicolor.go to avoid import cycle issues.
func sprint(a ...interface{}) string { return fmt.Sprint(a...) }
func sprintf(f string, a ...interface{}) string { return fmt.Sprintf(f, a...) }
func sprintln(a ...interface{}) string { return fmt.Sprintln(a...) }