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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
package tui
import (
"testing"
)
func TestEscBuilder_SetStyle_ANSIForeground(t *testing.T) {
type tc struct {
color Color
caps Capabilities
expected string
}
tests := map[string]tc{
"basic red": {
color: Red,
caps: Capabilities{Colors: Color16},
expected: "\x1b[0;31m",
},
"basic green": {
color: Green,
caps: Capabilities{Colors: Color16},
expected: "\x1b[0;32m",
},
"bright red": {
color: BrightRed,
caps: Capabilities{Colors: Color16},
expected: "\x1b[0;91m",
},
"bright white": {
color: BrightWhite,
caps: Capabilities{Colors: Color16},
expected: "\x1b[0;97m",
},
"color 256 index 200": {
color: ANSIColor(200),
caps: Capabilities{Colors: Color256},
expected: "\x1b[0;38;5;200m",
},
"color 256 index 42": {
color: ANSIColor(42),
caps: Capabilities{Colors: Color256},
expected: "\x1b[0;38;5;42m",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
e := newEscBuilder(64)
style := NewStyle().Foreground(tt.color)
e.SetStyle(style, tt.caps)
if string(e.Bytes()) != tt.expected {
t.Errorf("SetStyle(fg=%v) = %q, want %q", tt.color, e.Bytes(), tt.expected)
}
})
}
}
func TestEscBuilder_SetStyle_RGBForeground(t *testing.T) {
type tc struct {
r, g, b uint8
caps Capabilities
expected string
}
tests := map[string]tc{
"true color red": {
r: 255, g: 0, b: 0,
caps: Capabilities{Colors: ColorTrue, TrueColor: true},
expected: "\x1b[0;38;2;255;0;0m",
},
"true color green": {
r: 0, g: 255, b: 0,
caps: Capabilities{Colors: ColorTrue, TrueColor: true},
expected: "\x1b[0;38;2;0;255;0m",
},
"true color mixed": {
r: 128, g: 64, b: 192,
caps: Capabilities{Colors: ColorTrue, TrueColor: true},
expected: "\x1b[0;38;2;128;64;192m",
},
"fallback to 256 when no true color": {
r: 255, g: 0, b: 0,
caps: Capabilities{Colors: Color256, TrueColor: false},
expected: "\x1b[0;38;5;196m",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
e := newEscBuilder(64)
style := NewStyle().Foreground(RGBColor(tt.r, tt.g, tt.b))
e.SetStyle(style, tt.caps)
if string(e.Bytes()) != tt.expected {
t.Errorf("SetStyle(fg=RGB(%d,%d,%d)) = %q, want %q", tt.r, tt.g, tt.b, e.Bytes(), tt.expected)
}
})
}
}
func TestEscBuilder_SetStyle_Background(t *testing.T) {
type tc struct {
color Color
caps Capabilities
expected string
}
tests := map[string]tc{
"basic blue background": {
color: Blue,
caps: Capabilities{Colors: Color16},
expected: "\x1b[0;44m",
},
"bright cyan background": {
color: BrightCyan,
caps: Capabilities{Colors: Color16},
expected: "\x1b[0;106m",
},
"256 color background": {
color: ANSIColor(128),
caps: Capabilities{Colors: Color256},
expected: "\x1b[0;48;5;128m",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
e := newEscBuilder(64)
style := NewStyle().Background(tt.color)
e.SetStyle(style, tt.caps)
if string(e.Bytes()) != tt.expected {
t.Errorf("SetStyle(bg=%v) = %q, want %q", tt.color, e.Bytes(), tt.expected)
}
})
}
}
func TestEscBuilder_SetStyle_RGBBackground(t *testing.T) {
e := newEscBuilder(64)
caps := Capabilities{Colors: ColorTrue, TrueColor: true}
style := NewStyle().Background(RGBColor(100, 150, 200))
e.SetStyle(style, caps)
expected := "\x1b[0;48;2;100;150;200m"
if string(e.Bytes()) != expected {
t.Errorf("SetStyle(bg=RGB) = %q, want %q", e.Bytes(), expected)
}
}
func TestEscBuilder_SetStyle_Combined(t *testing.T) {
type tc struct {
style Style
caps Capabilities
expected string
}
tests := map[string]tc{
"bold red on blue": {
style: NewStyle().Bold().Foreground(Red).Background(Blue),
caps: Capabilities{Colors: Color16},
expected: "\x1b[0;1;31;44m",
},
"italic with 256 colors": {
style: NewStyle().Italic().Foreground(ANSIColor(196)).Background(ANSIColor(21)),
caps: Capabilities{Colors: Color256},
expected: "\x1b[0;3;38;5;196;48;5;21m",
},
"full style with true color": {
style: NewStyle().Bold().Underline().Foreground(RGBColor(255, 100, 50)).Background(RGBColor(0, 0, 128)),
caps: Capabilities{Colors: ColorTrue, TrueColor: true},
expected: "\x1b[0;1;4;38;2;255;100;50;48;2;0;0;128m",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
e := newEscBuilder(128)
e.SetStyle(tt.style, tt.caps)
if string(e.Bytes()) != tt.expected {
t.Errorf("SetStyle() = %q, want %q", e.Bytes(), tt.expected)
}
})
}
}
func TestEscBuilder_SetStyle_CapabilityFallback(t *testing.T) {
type tc struct {
style Style
caps Capabilities
desc string
contains string
}
tests := map[string]tc{
"RGB falls back to 256 color": {
style: NewStyle().Foreground(RGBColor(255, 0, 0)),
caps: Capabilities{Colors: Color256, TrueColor: false},
desc: "should use 38;5 instead of 38;2",
contains: "38;5;",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
e := newEscBuilder(64)
e.SetStyle(tt.style, tt.caps)
result := string(e.Bytes())
if !containsSubstring(result, tt.contains) {
t.Errorf("SetStyle() = %q, should contain %q (%s)", result, tt.contains, tt.desc)
}
})
}
}
func TestEscBuilder_WriteRune(t *testing.T) {
type tc struct {
r rune
expected string
}
tests := map[string]tc{
"ASCII letter": {
r: 'A',
expected: "A",
},
"space": {
r: ' ',
expected: " ",
},
"CJK character": {
r: '中',
expected: "中",
},
"emoji": {
r: '😀',
expected: "😀",
},
"box drawing": {
r: '┌',
expected: "┌",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
e := newEscBuilder(64)
e.WriteRune(tt.r)
if string(e.Bytes()) != tt.expected {
t.Errorf("WriteRune(%q) = %q, want %q", tt.r, e.Bytes(), tt.expected)
}
})
}
}
func TestEscBuilder_Reset(t *testing.T) {
e := newEscBuilder(64)
e.WriteString("hello")
if e.Len() != 5 {
t.Errorf("Len() = %d, want 5", e.Len())
}
e.Reset()
if e.Len() != 0 {
t.Errorf("after Reset(), Len() = %d, want 0", e.Len())
}
e.WriteString("world")
if string(e.Bytes()) != "world" {
t.Errorf("after Reset() and write, got %q, want %q", e.Bytes(), "world")
}
}
func TestEscBuilder_WriteString(t *testing.T) {
e := newEscBuilder(64)
e.WriteString("Hello, World!")
if string(e.Bytes()) != "Hello, World!" {
t.Errorf("WriteString() = %q, want %q", e.Bytes(), "Hello, World!")
}
}
func containsSubstring(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
|