-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgocolor.go
50 lines (42 loc) · 1.6 KB
/
gocolor.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
package gocolor
// Prepare is a convenience method that calls EnableConsole
// and returns an enabled color if there's no error and startEnabled is True,
// and a disabled color and the error otherwise.
func Prepare(startEnabled bool) (Color, error) {
err := EnableConsole()
if err != nil || !startEnabled {
return Color{}, err
}
//nolint:exhaustruct // This is too big to reasonably initialize
col := Color{}
col.EnableAll()
return col, nil
}
// NewEmpty returns an empty Color struct. Useful to satisfy exhaustruct linter
func NewEmpty() Color {
//nolint:exhaustruct // This is too big to reasonably initialize
return Color{}
}
// EnableConsole enables color printing on Windows and is a no-op
// on non-Windows platforms (they enable color printing by default).
func EnableConsole() error {
return enableConsole()
}
// Add colors a message and resets the console color afterwards
func (c *Color) Add(color Code, message string) string {
return string(color) + message + string(c.Default)
}
// ColorFunc colors a message and resets the console color afterward.
// Use Func() to generate a ColorFunc with a specific color
type ColorFunc = func(message string) string
// Func generates a ColorFunc from a color code. It uses the address of the code instead
// of the value because the code's value might change (for example to "" when Disable() is called)
func (c *Color) Func(codePtr *Code) ColorFunc {
return func(message string) string {
return c.Add(*codePtr, message)
}
}
// Code is a color code string
type Code string
// empty represents the absence of a color code. Used for Disable
const empty = Code("")