/
brush.go
178 lines (158 loc) · 3.19 KB
/
brush.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
// Copyright (c) 2017 Fadhli Dzil Ikram. All rights reserved.
// Source code is governed under MIT license, see LICENSE for more information.
// Brush wrapper implementation
package brush
import "io"
// Internal formatting state bit
const (
// Color state
colorOff = iota
colorRed
colorGreen
colorOrange
colorBlue
colorPurple
colorCyan
colorGray
colorMask = ^((1 << 3) - 1)
// Format state
formatBold = 1 << 3
)
// Internal formatting data bytes
var formatBytes = [][]byte{
[]byte("\033[0m"),
[]byte("\033[0;31m"),
[]byte("\033[0;32m"),
[]byte("\033[0;33m"),
[]byte("\033[0;34m"),
[]byte("\033[0;35m"),
[]byte("\033[0;36m"),
[]byte("\033[0;37m"),
[]byte("\033[1m"),
[]byte("\033[1;31m"),
[]byte("\033[1;32m"),
[]byte("\033[1;33m"),
[]byte("\033[1;34m"),
[]byte("\033[1;35m"),
[]byte("\033[1;36m"),
[]byte("\033[1;37m"),
}
// Writer function wraps io.Writer and store related formatting state. User
// should use Wrap function to create new Writer struct instead of manual
// instantiation.
type Writer struct {
io.Writer
format int
color bool
}
// Color function get underlying writer color support status.
func (w Writer) Color() bool {
return w.color
}
// Bold method set bold formatting (should be followed by a color command).
func (w *Writer) Bold() *Writer {
if w.color {
w.format |= formatBold
}
return w
}
// NoBold method unset bold formatting (should be followed by a color command).
func (w *Writer) NoBold() *Writer {
if w.color {
w.format &= ^formatBold
}
return w
}
// Off method unset all formatting.
func (w *Writer) Off() {
if !w.color {
return
}
w.format = colorOff
w.update()
}
// NoColor method unset all color.
func (w *Writer) NoColor() {
if !w.color {
return
}
w.format &= colorMask
w.update()
}
// Red method set to red color.
func (w *Writer) Red() {
if !w.color {
return
}
w.format &= colorMask
w.format |= colorRed
w.update()
}
// Green method set to green color.
func (w *Writer) Green() {
if !w.color {
return
}
w.format &= colorMask
w.format |= colorGreen
w.update()
}
// Orange method set to orange color.
func (w *Writer) Orange() {
if !w.color {
return
}
w.format &= colorMask
w.format |= colorOrange
w.update()
}
// Blue method set to blue color.
func (w *Writer) Blue() {
if !w.color {
return
}
w.format &= colorMask
w.format |= colorBlue
w.update()
}
// Purple method set to purple color.
func (w *Writer) Purple() {
if !w.color {
return
}
w.format &= colorMask
w.format |= colorPurple
w.update()
}
// Cyan method set to cyan color.
func (w *Writer) Cyan() {
if !w.color {
return
}
w.format &= colorMask
w.format |= colorCyan
w.update()
}
// Gray method set to gray color.
func (w *Writer) Gray() {
if !w.color {
return
}
w.format &= colorMask
w.format |= colorGray
w.update()
}
// Wrap function create new Writer object by wrapping existing io.Writer
// interface and detect its ability to display color.
func Wrap(w io.Writer) *Writer {
return &Writer{
Writer: w,
color: isColorEnabled(w),
}
}
// Update function write current formatting state to underlying writer.
func (w Writer) update() error {
// Write format bytes to underlying writer
_, err := w.Write(formatBytes[w.format])
return err
}