-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.go
154 lines (127 loc) · 4.12 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
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
// Copyright 2021-2024 Takashi Takizawa. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:generate go run gen_tables.go
//go:generate gofmt -w tables.go
package color
import (
"fmt"
"os"
"strings"
"github.com/mattn/go-isatty"
)
const Version = "0.4.1"
const (
// csi is CONTROL SEQUENCE INTRODUCER (CSI), "ESC [".
csi = "\x1b["
// sgr_end_char is the end character of SGR (SELECT GRAPHIC RENDITION), which is "m".
sgr_end_char = "m"
// sgr_separeter is the separator of SGR (SELECT GRAPHIC RENDITION), which is ";".
sgr_separator = ";"
)
var terminalDetection = true
// Color is a color that handles ANSI escape sequences.
type Color int
// NewColor creates a Color.
func NewColor(name string) (Color, error) {
for i, v := range colorNames {
if strings.EqualFold(v, name) {
return Color(i), nil
}
}
return 0, fmt.Errorf("unknown color name: %s", name)
}
// String returns the name of the color.
func (c Color) String() string {
return colorNames[c]
}
func (c Color) parameter() string {
var parameter = colorParameters[0]
if 0 <= c && int(c) < len(colorParameters) {
parameter = colorParameters[c]
}
return parameter
}
func (c Color) escapeSequence() string {
return strings.Join([]string{csi, c.parameter(), sgr_end_char}, "")
}
// Colorize returns the string with the foreground color.
func (c Color) Colorize(str string) string {
if terminalDetection && !isatty.IsTerminal(os.Stdout.Fd()) {
return str
}
return fmt.Sprint(strings.Join([]string{c.escapeSequence(), str, Default.escapeSequence()}, ""))
}
// BackgroundColor is a background color for output.
type BackgroundColor int
// NewBackgroundColor creates a BackgroundColor.
func NewBackgroundColor(name string) (BackgroundColor, error) {
for i, v := range backgroundColorNames {
if strings.EqualFold(v, name) {
return BackgroundColor(i), nil
}
}
return 0, fmt.Errorf("unknown color name: %s", name)
}
// String returns the name of the color.
func (c BackgroundColor) String() string {
return backgroundColorNames[c]
}
func (c BackgroundColor) parameter() string {
var parameter = backgroundColorParameters[0]
if 0 <= c && int(c) < len(backgroundColorParameters) {
parameter = backgroundColorParameters[c]
}
return parameter
}
func (c BackgroundColor) escapeSequence() string {
return strings.Join([]string{csi, c.parameter(), sgr_end_char}, "")
}
// Colorize returns the string with the background color.
func (c BackgroundColor) Colorize(str string) string {
if terminalDetection && !isatty.IsTerminal(os.Stdout.Fd()) {
return str
}
return fmt.Sprint(strings.Join([]string{c.escapeSequence(), str, DefaultBackground.escapeSequence()}, ""))
}
func SupportedColors() []Color {
var colors []Color
for _, name := range colorNames {
c, _ := NewColor(name)
if c == Default {
continue
}
colors = append(colors, c)
}
return colors
}
// ColorizeForeground returns the string with the foreground color.
func ColorizeForeground(str string, foreground Color) string {
return foreground.Colorize(str)
}
// ColorizeBackground returns the string with the background color.
func ColorizeBackground(str string, background BackgroundColor) string {
return background.Colorize(str)
}
// Colorize returns the string with foreground and background colors.
func Colorize(str string, foreground Color, background BackgroundColor) string {
if terminalDetection && !isatty.IsTerminal(os.Stdout.Fd()) {
return str
}
return strings.Join([]string{
csi, foreground.parameter(), sgr_separator, background.parameter(), sgr_end_char,
str,
csi, Default.parameter(), sgr_separator, DefaultBackground.parameter(), sgr_end_char}, "")
}
// EnableTerminalDetection enables terminal detection.
// If no terminal is detected, colorization is suppressed.
// By default, the terminal detection is enabled.
func EnableTerminalDetection() {
terminalDetection = true
}
// DisableTerminalDetection disables terminal detection.
// Even if the terminal is not detected, colorization is not suppressed.
// By default, the terminal detection is enabled.
func DisableTerminalDetection() {
terminalDetection = false
}