/
color.go
125 lines (104 loc) · 2.64 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
package ui
import (
"bytes"
"github.com/juju/ansiterm"
)
// Color represents 1 of 16 ANSI color sequences
type Color int
const (
_ Color = iota
// Default sets the temrinal text to its default color
Default
// Black text
Black
// Red text
Red
//Green text
Green
// Yellow text
Yellow
// Blue text
Blue
// Magenta text
Magenta
// Cyan text
Cyan
// Gray text
Gray
// DarkGray text
DarkGray
// BrightRed text
BrightRed
// BrightGreen text
BrightGreen
// BrightYellow text
BrightYellow
// BrightBlue text
BrightBlue
// BrightMagenta text
BrightMagenta
// BrightCyan text
BrightCyan
// White text
White
)
// BoldString returns a bolded copy of the string (ANSI escape sequenced)
func BoldString(s string) string { return defUI.BoldString(s) }
// BoldString checks the EnableColors pref and returns a bolded copy
// of the string (ANSI escape sequenced)
func (u *UI) BoldString(s string) string {
if !u.EnableColors {
return s
}
ctx := ansiterm.Context{
Styles: []ansiterm.Style{ansiterm.Bold},
}
return createStyledString(ctx, s)
}
// FaintString returns a faint (ANSI DarkGray) copy of the string (ANSI escape sequenced)
func FaintString(s string) string { return defUI.FaintString(s) }
// FaintString checks the EnableColors pref and returns a faint (ANSI DarkGrey)
// copy of the string (ANSI escape sequenced)
func (u *UI) FaintString(s string) string {
if !u.EnableColors {
return s
}
return ColorString(DarkGray, s)
}
// ColorString returns the original string, converted to the provided ANSI color
func ColorString(c Color, s string) string { return defUI.ColorString(c, s) }
// ColorString checks the EnableColors pref and returns a colored
// copy of the string (ANSI escape sequenced)
func (u *UI) ColorString(c Color, s string) string {
if !u.EnableColors {
return s
}
ctx := ansiterm.Context{
Foreground: ansiterm.Color(c),
}
return createStyledString(ctx, s)
}
// BoldColorString returns a bolded, colored copy of the string (ANSI escape sequenced)
func BoldColorString(c Color, s string) string { return defUI.BoldColorString(c, s) }
// BoldColorString checks the EnableColors pref and returns a bolded, colored
// copy of the string (ANSI escape sequenced)
func (u *UI) BoldColorString(c Color, s string) string {
if !u.EnableColors {
return s
}
ctx := ansiterm.Context{
Foreground: ansiterm.Color(c),
Styles: []ansiterm.Style{ansiterm.Bold},
}
return createStyledString(ctx, s)
}
func createStyledString(ctx ansiterm.Context, s string) string {
if !Attached() {
return s
}
buf := bytes.Buffer{}
w := ansiterm.NewWriter(&buf)
w.SetColorCapable(true)
ctx.Fprint(w, s)
return buf.String()
}