summaryrefslogtreecommitdiffstats
path: root/pkg/gui/style/style_test.go
blob: cadd3c397117d1724b9a383e47e2f583e48361ab (plain)
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
package style

import (
	"bytes"
	"testing"
	"text/template"

	"github.com/gookit/color"
	"github.com/stretchr/testify/assert"
	"github.com/xo/terminfo"
)

func TestMerge(t *testing.T) {
	type scenario struct {
		name          string
		toMerge       []TextStyle
		expectedStyle TextStyle
		expectedStr   string
	}

	// on CI we've got no color capability so we're forcing it here
	color.ForceSetColorLevel(terminfo.ColorLevelMillions)

	fgRed := color.FgRed
	bgRed := color.BgRed
	fgBlue := color.FgBlue

	rgbPinkLib := color.Rgb(0xFF, 0x00, 0xFF)
	rgbPink := NewRGBColor(rgbPinkLib)

	rgbYellowLib := color.Rgb(0xFF, 0xFF, 0x00)
	rgbYellow := NewRGBColor(rgbYellowLib)

	strToPrint := "foo"

	scenarios := []scenario{
		{
			"no color",
			nil,
			TextStyle{style: color.Style{}},
			"foo",
		},
		{
			"only fg color",
			[]TextStyle{FgRed},
			TextStyle{fg: &Color{basic: &fgRed}, style: color.Style{fgRed}},
			"\x1b[31mfoo\x1b[0m",
		},
		{
			"only bg color",
			[]TextStyle{BgRed},
			TextStyle{bg: &Color{basic: &bgRed}, style: color.Style{bgRed}},
			"\x1b[41mfoo\x1b[0m",
		},
		{
			"fg and bg color",
			[]TextStyle{FgBlue, BgRed},
			TextStyle{
				fg:    &Color{basic: &fgBlue},
				bg:    &Color{basic: &bgRed},
				style: color.Style{fgBlue, bgRed},
			},
			"\x1b[34;41mfoo\x1b[0m",
		},
		{
			"single attribute",
			[]TextStyle{AttrBold},
			TextStyle{
				decoration: Decoration{bold: true},
				style:      color.Style{color.OpBold},
			},
			"\x1b[1mfoo\x1b[0m",
		},
		{
			"multiple attributes",
			[]TextStyle{AttrBold, AttrUnderline},
			TextStyle{
				decoration: Decoration{
					bold:      true,
					underline: true,
				},
				style: color.Style{color.OpBold, color.OpUnderscore},
			},
			"\x1b[1;4mfoo\x1b[0m",
		},
		{
			"multiple attributes and colors",
			[]TextStyle{AttrBold, FgBlue, AttrUnderline, BgRed},
			TextStyle{
				fg: &Color{basic: &fgBlue},
				bg: &Color{basic: &bgRed},
				decoration: Decoration{
					bold:      true,
					underline: true,
				},
				style: color.Style{fgBlue, bgRed, color.OpBold, color.OpUnderscore},
			},
			"\x1b[34;41;1;4mfoo\x1b[0m",
		},
		{
			"rgb fg color",
			[]TextStyle{New().SetFg(rgbPink)},
			TextStyle{
				fg:    &rgbPink,
				style: color.NewRGBStyle(rgbPinkLib).SetOpts(color.Opts{}),
			},
			// '38;2' qualifies an RGB foreground color
			"\x1b[38;2;255;0;255mfoo\x1b[0m",
		},
		{
			"rgb fg and bg color",
			[]TextStyle{New().SetFg(rgbPink).SetBg(rgbYellow)},
			TextStyle{
				fg:    &rgbPink,
				bg:    &rgbYellow,
				style: color.NewRGBStyle(rgbPinkLib, rgbYellowLib).SetOpts(color.Opts{}),
			},
			// '48;2' qualifies an RGB background color
			"\x1b[38;2;255;0;255;48;2;255;255;0mfoo\x1b[0m",
		},
		{
			"rgb fg and bg color with opts",
			[]TextStyle{AttrBold, New().SetFg(rgbPink).SetBg(rgbYellow), AttrUnderline},
			TextStyle{
				fg: &rgbPink,
				bg: &rgbYellow,
				decoration: Decoration{
					bold:      true,
					underline: true,
				},
				style: color.NewRGBStyle(rgbPinkLib, rgbYellowLib).SetOpts(color.Opts{color.OpBold, color.OpUnderscore}),
			},
			"\x1b[38;2;255;0;255;48;2;255;255;0;1;4mfoo\x1b[0m",
		},
		{
			"mix color-16 with rgb colors",
			[]TextStyle{New().SetFg(rgbYellow), BgRed},
			TextStyle{
				fg: &rgbYellow,
				bg: &Color{basic: &bgRed},
				style: color.NewRGBStyle(
					rgbYellowLib,
					fgRed.RGB(), // We need to use FG here,  https://github.com/gookit/color/issues/39
				).SetOpts(color.Opts{}),
			},
			"\x1b[38;2;255;255;0;48;2;197;30;20mfoo\x1b[0m",
		},
	}

	for _, s := range scenarios {
		s := s
		t.Run(s.name, func(t *testing.T) {
			style := New()
			for _, other := range s.toMerge {
				style = style.MergeStyle(other)
			}
			assert.Equal(t, s.expectedStyle, style)
			assert.Equal(t, s.expectedStr, style.Sprint(strToPrint))
		})
	}
}

func TestTemplateFuncMapAddColors(t *testing.T) {
	type scenario struct {
		name   string
		tmpl   string
		expect string
	}

	scenarios := []scenario{
		{
			"normal template",
			"{{ .Foo }}",
			"bar",
		},
		{
			"colored string",
			"{{ .Foo | red }}",
			"\x1b[31mbar\x1b[0m",
		},
		{
			"string with decorator",
			"{{ .Foo | bold }}",
			"\x1b[1mbar\x1b[0m",
		},
		{
			"string with color and decorator",
			"{{ .Foo | bold | red }}",
			"\x1b[31m\x1b[1mbar\x1b[0m\x1b[0m",
		},
		{
			"multiple string with different colors",
			"{{ .Foo | red }} - {{ .Foo | blue }}",
			"\x1b[31mbar\x1b[0m - \x1b[34mbar\x1b[0m",
		},
	}

	for _, s := range scenarios {
		s := s
		t.Run(s.name, func(t *testing.T) {
			tmpl, err := template.New("test template").Funcs(TemplateFuncMapAddColors(template.FuncMap{})).Parse(s.tmpl)
			assert.NoError(t, err)

			buff := bytes.NewBuffer(nil)
			err = tmpl.Execute(buff, struct{ Foo string }{"bar"})
			assert.NoError(t, err)

			assert.Equal(t, s.expect, buff.String())
		})
	}
}