-
Notifications
You must be signed in to change notification settings - Fork 21
/
chalk.go
162 lines (135 loc) · 4.06 KB
/
chalk.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
package chalk
import "fmt"
// Color represents one of the ANSI color escape codes.
// http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
type Color struct {
value int
}
// Value returns the individual value for this color
// (Actually it's really just its index in the list
// of color escape codes with the list being
// [black, red, green, yellow, blue, magenta, cyan, white].
func (c Color) Value() int {
return c.value
}
// Color colors the foreground of the given string
// (whatever the previous background color was, it is
// left alone).
func (c Color) Color(val string) string {
return fmt.Sprintf("%s%s%s", c, val, ResetColor)
}
func (c Color) String() string {
return fmt.Sprintf("\u001b[%dm", 30+c.value)
}
// NewStyle creates a style with a foreground of the
// color we're creating the style from.
func (c Color) NewStyle() Style {
return &style{foreground: c}
}
type textStyleDemarcation int
func (t textStyleDemarcation) String() string {
return fmt.Sprintf("\u001b[%dm", t)
}
// A TextStyle represents the ways we can style the text:
// bold, dim, italic, underline, inverse, hidden or strikethrough.
type TextStyle struct {
start, stop textStyleDemarcation
}
// TextStyle styles the given string using the desired text style.
func (t TextStyle) TextStyle(val string) string {
if t == emptyTextStyle {
return val
}
return fmt.Sprintf("%s%s%s", t.start, val, t.stop)
}
// NOTE: this function specifically does not work as desired because
// text styles must be wrapped around the text they are meant to style.
// As such, use TextStyle() or Style.Style() instead.
func (t TextStyle) String() string {
return fmt.Sprintf("%s%s", t.start, t.stop)
}
// NewStyle creates a style starting with the current TextStyle
// as its text style.
func (t TextStyle) NewStyle() Style {
return &style{textStyle: t}
}
// A Style is how we want our text to look in the console.
// Consequently, we can set the foreground and background
// to specific colors, we can style specific strings and
// can also use this style in a builder pattern should we
// wish (these will be more useful once styles such as
// italics are supported).
type Style interface {
// Foreground sets the foreground of the style to the specific color.
Foreground(Color)
// Background sets the background of the style to the specific color.
Background(Color)
// Style styles the given string with the current style.
Style(string) string
// WithBackground allows us to set the background in a builder
// pattern style.
WithBackground(Color) Style
// WithForeground allows us to set the foreground in a builder
// pattern style.
WithForeground(Color) Style
// WithStyle allows us to set the text style in a builder pattern
// style.
WithTextStyle(TextStyle) Style
String() string
}
type style struct {
foreground Color
background Color
textStyle TextStyle
}
func (s *style) WithBackground(col Color) Style {
s.Background(col)
return s
}
func (s *style) WithForeground(col Color) Style {
s.Foreground(col)
return s
}
func (s *style) String() string {
var toReturn string
toReturn = fmt.Sprintf("\u001b[%dm", 40+s.background.Value())
return toReturn + fmt.Sprintf("\u001b[%dm", 30+s.foreground.Value())
}
func (s *style) Style(val string) string {
return fmt.Sprintf("%s%s%s", s, s.textStyle.TextStyle(val), Reset)
}
func (s *style) Foreground(col Color) {
s.foreground = col
}
func (s *style) Background(col Color) {
s.background = col
}
func (s *style) WithTextStyle(textStyle TextStyle) Style {
s.textStyle = textStyle
return s
}
var (
// Colors
Black = Color{0}
Red = Color{1}
Green = Color{2}
Yellow = Color{3}
Blue = Color{4}
Magenta = Color{5}
Cyan = Color{6}
White = Color{7}
ResetColor = Color{9}
// Text Styles
Bold = TextStyle{1, 22}
Dim = TextStyle{2, 22}
Italic = TextStyle{3, 23}
Underline = TextStyle{4, 24}
Inverse = TextStyle{7, 27}
Hidden = TextStyle{8, 28}
Strikethrough = TextStyle{9, 29}
Reset = &style{
foreground: ResetColor,
background: ResetColor,
}
emptyTextStyle = TextStyle{}
)