-
Notifications
You must be signed in to change notification settings - Fork 59
/
color.go
192 lines (168 loc) · 3.16 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
// Copyright © 2019 Alibaba Co. Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package color implements color output on console.
package color
import (
"fmt"
"github.com/alibaba/git-repo-go/cap"
)
type mapping map[string]int
func (v mapping) get(s string) int {
num, ok := v[s]
if !ok {
return -1
}
return num
}
func (v mapping) has(s string) bool {
_, ok := v[s]
return ok
}
const (
colorPrefix = "\033["
colorReset = "\033[m"
)
var colorMap = mapping{
"normal": -1,
"black": 0,
"red": 1,
"green": 2,
"yellow": 3,
"blue": 4,
"magenta": 5,
"cyan": 6,
"white": 7,
}
var attrMap = mapping{
"bold": 1,
"dim": 2,
"ul": 4,
"blink": 5,
"reverse": 7,
}
func colorEnabled() bool {
if !cap.Isatty() || cap.IsWindows() {
return false
}
return true
}
// Color returns color code for terminal display
//
// Available colors:
// normal
// black
// red
// green
// yellow
// blue
// magenta
// cyan
// white
//
// Available attributes:
// bold
// dim
// ul
// blink
// reverse
func Color(fgColor, bgColor, attrVal string) string {
var (
code string
)
if !colorEnabled() {
return ""
}
fg := colorMap.get(fgColor)
bg := colorMap.get(bgColor)
attr := attrMap.get(attrVal)
if attr >= 0 || fg >= 0 || bg >= 0 {
if attr >= 0 {
code = string('0' + rune(attr))
}
if fg >= 0 {
if len(code) > 0 {
code += ";"
}
if fg < 8 {
code += "3" + string('0'+rune(fg))
} else {
code += "38;5;" + string('0'+rune(fg))
}
}
if bg >= 0 {
if len(code) > 0 {
code += ";"
}
if bg < 8 {
code += "4" + string('0'+rune(bg))
} else {
code += "48;5;" + string('0'+rune(bg))
}
}
code += "m"
}
if len(code) > 0 {
return colorPrefix + code
}
return ""
}
// Reset returns ResetColor to reset color output.
func Reset() string {
if colorEnabled() {
return colorReset
}
return ""
}
// Hilight shows hightlight message
func Hilight(msg string) {
fmt.Printf("%s%s%s",
Color("", "", "bold"),
msg,
Reset(),
)
}
// Hilightln shows hightlight message with LF
func Hilightln(msg string) {
fmt.Printf("%s%s%s\n",
Color("", "", "bold"),
msg,
Reset(),
)
}
// Hilightf is sprintf version of Hilight
func Hilightf(f string, args ...interface{}) {
msg := fmt.Sprintf(f, args...)
Hilight(msg)
}
// Dim shows message in dim style
func Dim(msg string) {
fmt.Printf("%s%s%s",
Color("", "", "dim"),
msg,
Reset(),
)
}
// Dimln shows message in dim style with LF
func Dimln(msg string) {
fmt.Printf("%s%s%s\n",
Color("", "", "dim"),
msg,
Reset(),
)
}
// Dimf is sprintf version of Dim
func Dimf(f string, args ...interface{}) {
msg := fmt.Sprintf(f, args...)
Dim(msg)
}