-
Notifications
You must be signed in to change notification settings - Fork 54
/
color.go
155 lines (140 loc) · 3.6 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
155
package zlog
import (
"fmt"
"os"
"strings"
"github.com/sohaha/zlsgo/zstring"
)
// DisableColor DisableColor
var DisableColor = false
// Color Color
type Color int
// Format Format
type Format int
// Op Op
type Op int
const (
// ColorBlack black
ColorBlack Color = iota + 30
// ColorRed gules
ColorRed
// ColorGreen green
ColorGreen
// ColorYellow yellow
ColorYellow
// ColorBlue blue
ColorBlue
// ColorMagenta magenta
ColorMagenta
// ColorCyan cyan
ColorCyan
// ColorWhite white
ColorWhite
)
const (
// ColorLightGrey light grey
ColorLightGrey Color = iota + 90
// ColorLightRed light red
ColorLightRed
// ColorLightGreen light green
ColorLightGreen
// ColorLightYellow light yellow
ColorLightYellow
// ColorLightBlue light blue
ColorLightBlue
// ColorLightMagenta light magenta
ColorLightMagenta
// ColorLightCyan lightcyan
ColorLightCyan
// ColorLightWhite light white
ColorLightWhite
// ColorDefault ColorDefault
ColorDefault = 49
)
const (
// OpReset Reset All Settings
OpReset Op = iota
// OpBold Bold
OpBold
// OpFuzzy Fuzzy (not all terminal emulators support it)
OpFuzzy
// OpItalic Italic (not all terminal emulators support it)
OpItalic
// OpUnderscore Underline
OpUnderscore
// OpBlink Twinkle
OpBlink
// OpFastBlink Fast scintillation (not widely supported)
OpFastBlink
// OpReverse Reversed Exchange Background and Foreground Colors
OpReverse
// OpConcealed Concealed
OpConcealed
// OpStrikethrough Deleted lines (not widely supported)
OpStrikethrough
)
// OpTextWrap OpTextWrap
func OpTextWrap(op Op, text string) string {
if !IsSupportColor() {
return text
}
return fmt.Sprintf("\x1b[%dm%s", op, text)
}
// ColorBackgroundWrap ColorBackgroundWrap
func ColorBackgroundWrap(color Color, backgroundColor Color, text string) string {
if !IsSupportColor() {
return text
}
return fmt.Sprintf("\x1b[%d;%dm%s\x1b[0m", color, backgroundColor+10, text)
}
// OutAllColor OutAllColor
func OutAllColor() {
all := zstring.Buffer()
colors := GetAllColorText()
for k, v := range colors {
all.WriteString("\n\nBackground " + k + "\n")
for ck, cv := range colors {
if cv == v {
continue
}
all.WriteString(ColorBackgroundWrap(cv, v, ck+" => "))
all.WriteString(ColorBackgroundWrap(cv, v, OpTextWrap(OpBold, "Bold ")))
all.WriteString(ColorBackgroundWrap(cv, v, OpTextWrap(OpUnderscore, "Under")))
all.WriteString(ColorBackgroundWrap(cv, v, " | "))
}
all.WriteString("\n")
}
fmt.Println(all.String())
}
// GetAllColorText GetAllColorText
func GetAllColorText() map[string]Color {
return map[string]Color{
"ColorBlack": ColorBlack,
"ColorRed": ColorRed,
"ColorGreen": ColorGreen,
"ColorYellow": ColorYellow,
"ColorBlue": ColorBlue,
"ColorMagenta": ColorMagenta,
"ColorCyan": ColorCyan,
"ColorWhite": ColorWhite,
"ColorLightGrey": ColorLightGrey,
"ColorLightRed": ColorLightRed,
"ColorLightGreen": ColorLightGreen,
"ColorLightYellow": ColorLightYellow,
"ColorLightBlue": ColorLightBlue,
"ColorLightMagenta": ColorLightMagenta,
"ColorLightCyan": ColorLightCyan,
"ColorLightWhite": ColorLightWhite,
"ColorDefault": ColorDefault,
}
}
// ColorTextWrap ColorTextWrap
func ColorTextWrap(color Color, text string) string {
if !IsSupportColor() {
return text
}
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color, text)
}
func isSupportColor() bool {
return !DisableColor && (strings.Contains(os.Getenv("TERM"), "xterm") || os.Getenv("ConEmuANSI") == "ON" || os.Getenv("ANSICON") != "" || os.Getenv("ANSICON") != "" || strings.Contains(os.Getenv("TERM"), "256color"))
}