-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.go
206 lines (169 loc) · 3.67 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
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
package color
import (
"os"
"fmt"
"strconv"
"strings"
"github.com/mattn/go-isatty"
)
/*
字背景:
40:黑
41:深红
42:绿
43:黄色
44:蓝色
45:紫色
46:深绿
47:白色
字颜色:
30:黑
31:红
32:绿
33:黄
34:蓝色
35:紫色
36:深绿
37:白色
ANSI控制码的说明:
\033[0m 关闭所有属性
\033[1m 设置高亮度
\033[4m 下划线
\033[5m 闪烁
\033[7m 反显
\033[8m 消隐
\033[30m -- \33[37m 设置前景色
\033[40m -- \33[47m 设置背景色
\033[nA 光标上移n行
\033[nB 光标下移n行
\033[nC 光标右移n行
\033[nD 光标左移n行
\033[y;xH设置光标位置
\033[2J 清屏
\033[K 清除从光标到行尾的内容
\033[s 保存光标位置
\033[u 恢复光标位置
\033[?25l 隐藏光标
\033[?25h 显示光标
*/
type Attr int
type BgColor = Attr
type WdColor = Attr
const (
BgBlack BgColor = iota + 40 // 黑
BgRed // 深红
BgGreen // 绿
BgYellow // 黄色
BgBlue // 蓝色
BgPurple // 紫色
BgHiGreen // 深绿
BgWhite // 白色
)
const (
WdBlack WdColor = iota + 30 // 黑
WdRed // 深红
WdGreen // 绿
WdYellow // 黄色
WdBlue // 蓝色
WdPurple // 紫色
WdHiGreen // 深绿
WdWhite // 白色
)
var (
IsTerm = isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
Output = os.Stdout
START = "\033["
FORMAT = "%s;m"
END = "\033[0m"
)
type Color struct {
params []Attr
canSet bool
}
func (c *Color) println(format string, wd WdColor, bg BgColor, a ...interface{}) {
fmt.Fprintf(Output, format, a...)
}
func (c *Color) canSetColor() bool {
return c.canSet
}
func (c *Color) start() string {
return START
}
func (c *Color) end() string {
return END
}
func (c *Color) features() string {
feature := make([]string, len(c.params))
for i, v := range c.params {
feature[i] = strconv.Itoa(int(v))
}
return strings.Join(feature, ";")
}
func (c *Color) format() string {
return fmt.Sprintf(FORMAT, c.features())
}
func (c *Color) wrap(format string, a ...interface{}) (string, []interface{}) {
if !c.canSetColor() {
return format, a
}
return c.start() + c.format() + format + c.end(), a
}
func (c *Color) AddAttr(attr ...Attr) {
c.params = append(c.params, attr...)
}
func (c *Color) Println(format string, a ...interface{}) {
format, a = c.wrap(format, a...)
fmt.Fprintf(Output, format+"\n", a...)
}
func New(attr ...Attr) *Color {
c := &Color{
canSet: IsTerm,
params: make([]Attr, len(attr)),
}
c.AddAttr(attr...)
return c
}
func Common(format string, a ...interface{}) {
c := New()
c.Println(format, a...)
}
func Red(format string, a ...interface{}) {
c := New()
c.AddAttr(WdRed)
c.Println(format, a...)
}
func Blue(format string, a ...interface{}) {
c := New()
c.AddAttr(WdBlue)
c.Println(format, a...)
}
func Yellow(format string, a ...interface{}) {
c := New()
c.AddAttr(WdYellow)
c.Println(format, a...)
}
func Green(format string, a ...interface{}) {
c := New()
c.AddAttr(WdGreen)
c.Println(format, a...)
}
func Black(format string, a ...interface{}) {
c := New()
c.AddAttr(WdBlack)
c.Println(format, a...)
}
func Purple(format string, a ...interface{}) {
c := New()
c.AddAttr(WdPurple)
c.Println(format, a...)
}
func HiGreen(format string, a ...interface{}) {
c := New()
c.AddAttr(WdHiGreen)
c.Println(format, a...)
}
func White(format string, a ...interface{}) {
c := New()
c.AddAttr(WdWhite)
c.Println(format, a...)
}