forked from cucumber/godog
/
color_tag_test.go
250 lines (221 loc) · 5.23 KB
/
color_tag_test.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package godog
import (
"bytes"
"fmt"
"io"
"strings"
"testing"
"github.com/cbush06/godog/colors"
)
type csiState int
const (
outsideCsiCode csiState = iota
firstCsiCode
secondCsiCode
)
type tagColorWriter struct {
w io.Writer
state csiState
paramStartBuf bytes.Buffer
paramBuf bytes.Buffer
tag string
}
const (
firstCsiChar byte = '\x1b'
secondeCsiChar byte = '['
separatorChar byte = ';'
sgrCode byte = 'm'
)
const (
ansiReset = "0"
ansiIntensityOn = "1"
ansiIntensityOff = "21"
ansiUnderlineOn = "4"
ansiUnderlineOff = "24"
ansiBlinkOn = "5"
ansiBlinkOff = "25"
ansiForegroundBlack = "30"
ansiForegroundRed = "31"
ansiForegroundGreen = "32"
ansiForegroundYellow = "33"
ansiForegroundBlue = "34"
ansiForegroundMagenta = "35"
ansiForegroundCyan = "36"
ansiForegroundWhite = "37"
ansiForegroundDefault = "39"
ansiBackgroundBlack = "40"
ansiBackgroundRed = "41"
ansiBackgroundGreen = "42"
ansiBackgroundYellow = "43"
ansiBackgroundBlue = "44"
ansiBackgroundMagenta = "45"
ansiBackgroundCyan = "46"
ansiBackgroundWhite = "47"
ansiBackgroundDefault = "49"
ansiLightForegroundGray = "90"
ansiLightForegroundRed = "91"
ansiLightForegroundGreen = "92"
ansiLightForegroundYellow = "93"
ansiLightForegroundBlue = "94"
ansiLightForegroundMagenta = "95"
ansiLightForegroundCyan = "96"
ansiLightForegroundWhite = "97"
ansiLightBackgroundGray = "100"
ansiLightBackgroundRed = "101"
ansiLightBackgroundGreen = "102"
ansiLightBackgroundYellow = "103"
ansiLightBackgroundBlue = "104"
ansiLightBackgroundMagenta = "105"
ansiLightBackgroundCyan = "106"
ansiLightBackgroundWhite = "107"
)
var colorMap = map[string]string{
ansiForegroundBlack: "black",
ansiForegroundRed: "red",
ansiForegroundGreen: "green",
ansiForegroundYellow: "yellow",
ansiForegroundBlue: "blue",
ansiForegroundMagenta: "magenta",
ansiForegroundCyan: "cyan",
ansiForegroundWhite: "white",
ansiForegroundDefault: "",
}
func (cw *tagColorWriter) flushBuffer() (int, error) {
return cw.flushTo(cw.w)
}
func (cw *tagColorWriter) resetBuffer() (int, error) {
return cw.flushTo(nil)
}
func (cw *tagColorWriter) flushTo(w io.Writer) (int, error) {
var n1, n2 int
var err error
startBytes := cw.paramStartBuf.Bytes()
cw.paramStartBuf.Reset()
if w != nil {
n1, err = cw.w.Write(startBytes)
if err != nil {
return n1, err
}
} else {
n1 = len(startBytes)
}
paramBytes := cw.paramBuf.Bytes()
cw.paramBuf.Reset()
if w != nil {
n2, err = cw.w.Write(paramBytes)
if err != nil {
return n1 + n2, err
}
} else {
n2 = len(paramBytes)
}
return n1 + n2, nil
}
func isParameterChar(b byte) bool {
return ('0' <= b && b <= '9') || b == separatorChar
}
func (cw *tagColorWriter) Write(p []byte) (int, error) {
r, nw, first, last := 0, 0, 0, 0
var err error
for i, ch := range p {
switch cw.state {
case outsideCsiCode:
if ch == firstCsiChar {
cw.paramStartBuf.WriteByte(ch)
cw.state = firstCsiCode
}
case firstCsiCode:
switch ch {
case firstCsiChar:
cw.paramStartBuf.WriteByte(ch)
break
case secondeCsiChar:
cw.paramStartBuf.WriteByte(ch)
cw.state = secondCsiCode
last = i - 1
default:
cw.resetBuffer()
cw.state = outsideCsiCode
}
case secondCsiCode:
if isParameterChar(ch) {
cw.paramBuf.WriteByte(ch)
} else {
nw, err = cw.w.Write(p[first:last])
r += nw
if err != nil {
return r, err
}
first = i + 1
if ch == sgrCode {
cw.changeColor()
}
n, _ := cw.resetBuffer()
// Add one more to the size of the buffer for the last ch
r += n + 1
cw.state = outsideCsiCode
}
default:
cw.state = outsideCsiCode
}
}
if cw.state == outsideCsiCode {
nw, err = cw.w.Write(p[first:len(p)])
r += nw
}
return r, err
}
func (cw *tagColorWriter) changeColor() {
strParam := cw.paramBuf.String()
if len(strParam) <= 0 {
strParam = "0"
}
csiParam := strings.Split(strParam, string(separatorChar))
for _, p := range csiParam {
c, ok := colorMap[p]
switch {
case !ok:
switch p {
case ansiReset:
fmt.Fprint(cw.w, "</"+cw.tag+">")
cw.tag = ""
case ansiIntensityOn:
cw.tag = "bold-" + cw.tag
case ansiIntensityOff:
case ansiUnderlineOn:
case ansiUnderlineOff:
case ansiBlinkOn:
case ansiBlinkOff:
default:
// unknown code
}
default:
cw.tag += c
fmt.Fprint(cw.w, "<"+cw.tag+">")
}
}
}
func TestTagColorWriter(t *testing.T) {
var buf bytes.Buffer
w := &tagColorWriter{w: &buf}
s := fmt.Sprintf("text %s then %s", colors.Red("in red"), colors.Yellow("yel"))
fmt.Fprint(w, s)
expected := "text <red>in red</red> then <yellow>yel</yellow>"
if buf.String() != expected {
t.Fatalf("expected `%s` but got `%s`", expected, buf.String())
}
}
func TestTagBoldColorWriter(t *testing.T) {
var buf bytes.Buffer
w := &tagColorWriter{w: &buf}
s := fmt.Sprintf(
"text %s then %s",
colors.Bold(colors.Red)("in red"),
colors.Bold(colors.Yellow)("yel"),
)
fmt.Fprint(w, s)
expected := "text <bold-red>in red</bold-red> then <bold-yellow>yel</bold-yellow>"
if buf.String() != expected {
t.Fatalf("expected `%s` but got `%s`", expected, buf.String())
}
}