summaryrefslogtreecommitdiffstats
path: root/pkg/gui/style
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2021-08-01 12:54:17 +1000
committerJesse Duffield <jessedduffield@gmail.com>2021-08-01 13:21:06 +1000
commite798aa4b15a48b582db3debaab36446c0cbf86a8 (patch)
tree618f070ab7e0cc0e37e888e1e8e17abd5a00b19a /pkg/gui/style
parent04e474aa6610ca942a03b090ad4cdc29c646a049 (diff)
more color tests
Diffstat (limited to 'pkg/gui/style')
-rw-r--r--pkg/gui/style/style_test.go22
-rw-r--r--pkg/gui/style/text_style.go2
2 files changed, 23 insertions, 1 deletions
diff --git a/pkg/gui/style/style_test.go b/pkg/gui/style/style_test.go
index 3b301d160..298dae843 100644
--- a/pkg/gui/style/style_test.go
+++ b/pkg/gui/style/style_test.go
@@ -5,6 +5,7 @@ import (
"github.com/gookit/color"
"github.com/stretchr/testify/assert"
+ "github.com/xo/terminfo"
)
func TestMerge(t *testing.T) {
@@ -12,8 +13,12 @@ func TestMerge(t *testing.T) {
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
@@ -24,21 +29,26 @@ func TestMerge(t *testing.T) {
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",
@@ -48,6 +58,7 @@ func TestMerge(t *testing.T) {
bg: &Color{basic: &bgRed},
style: color.Style{fgBlue, bgRed},
},
+ "\x1b[34;41mfoo\x1b[0m",
},
{
"single attribute",
@@ -56,6 +67,7 @@ func TestMerge(t *testing.T) {
decoration: Decoration{bold: true},
style: color.Style{color.OpBold},
},
+ "\x1b[1mfoo\x1b[0m",
},
{
"multiple attributes",
@@ -67,6 +79,7 @@ func TestMerge(t *testing.T) {
},
style: color.Style{color.OpBold, color.OpUnderscore},
},
+ "\x1b[1;4mfoo\x1b[0m",
},
{
"multiple attributes and colors",
@@ -80,6 +93,7 @@ func TestMerge(t *testing.T) {
},
style: color.Style{fgBlue, bgRed, color.OpBold, color.OpUnderscore},
},
+ "\x1b[34;41;1;4mfoo\x1b[0m",
},
{
"rgb fg color",
@@ -88,6 +102,8 @@ func TestMerge(t *testing.T) {
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",
@@ -97,6 +113,8 @@ func TestMerge(t *testing.T) {
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",
@@ -110,6 +128,7 @@ func TestMerge(t *testing.T) {
},
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",
@@ -122,16 +141,19 @@ func TestMerge(t *testing.T) {
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))
})
}
}
diff --git a/pkg/gui/style/text_style.go b/pkg/gui/style/text_style.go
index fd893a2be..4fbb7f1f4 100644
--- a/pkg/gui/style/text_style.go
+++ b/pkg/gui/style/text_style.go
@@ -7,7 +7,7 @@ import (
// A TextStyle contains a foreground color, background color, and
// decorations (bold/underline/reverse).
//
-// Colors may each be either 16-bit or 256-bit RGB colors. When
+// Colors may each be either 16-bit or 24-bit RGB colors. When
// we need to produce a string with a TextStyle, if either foreground or
// background color is RGB, we'll promote the other color component to RGB as well.
// We could simplify this code by forcing everything to be RGB, but we're not