summaryrefslogtreecommitdiffstats
path: root/pkg/gui/style
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2021-11-02 16:39:15 +1100
committerJesse Duffield <jessedduffield@gmail.com>2021-11-05 07:58:21 +1100
commit802cfb1a0436568c72fc998249f10f8150b352a3 (patch)
tree599f8a8bd52b786312a11f3b3cac2a2d5b7c597e /pkg/gui/style
parent2fc1498517523a20a3080816ec50ee9e7fbe533d (diff)
render commit graph
Diffstat (limited to 'pkg/gui/style')
-rw-r--r--pkg/gui/style/style_test.go22
-rw-r--r--pkg/gui/style/text_style.go22
2 files changed, 23 insertions, 21 deletions
diff --git a/pkg/gui/style/style_test.go b/pkg/gui/style/style_test.go
index ecf316705..360ad00e6 100644
--- a/pkg/gui/style/style_test.go
+++ b/pkg/gui/style/style_test.go
@@ -39,19 +39,19 @@ func TestMerge(t *testing.T) {
{
"no color",
nil,
- TextStyle{style: color.Style{}},
+ TextStyle{Style: color.Style{}},
"foo",
},
{
"only fg color",
[]TextStyle{FgRed},
- TextStyle{fg: &Color{basic: &fgRed}, style: color.Style{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}},
+ TextStyle{bg: &Color{basic: &bgRed}, Style: color.Style{bgRed}},
"\x1b[41mfoo\x1b[0m",
},
{
@@ -60,7 +60,7 @@ func TestMerge(t *testing.T) {
TextStyle{
fg: &Color{basic: &fgBlue},
bg: &Color{basic: &bgRed},
- style: color.Style{fgBlue, bgRed},
+ Style: color.Style{fgBlue, bgRed},
},
"\x1b[34;41mfoo\x1b[0m",
},
@@ -69,7 +69,7 @@ func TestMerge(t *testing.T) {
[]TextStyle{AttrBold},
TextStyle{
decoration: Decoration{bold: true},
- style: color.Style{color.OpBold},
+ Style: color.Style{color.OpBold},
},
"\x1b[1mfoo\x1b[0m",
},
@@ -81,7 +81,7 @@ func TestMerge(t *testing.T) {
bold: true,
underline: true,
},
- style: color.Style{color.OpBold, color.OpUnderscore},
+ Style: color.Style{color.OpBold, color.OpUnderscore},
},
"\x1b[1;4mfoo\x1b[0m",
},
@@ -95,7 +95,7 @@ func TestMerge(t *testing.T) {
bold: true,
underline: true,
},
- style: color.Style{fgBlue, bgRed, color.OpBold, color.OpUnderscore},
+ Style: color.Style{fgBlue, bgRed, color.OpBold, color.OpUnderscore},
},
"\x1b[34;41;1;4mfoo\x1b[0m",
},
@@ -104,7 +104,7 @@ func TestMerge(t *testing.T) {
[]TextStyle{New().SetFg(rgbPink)},
TextStyle{
fg: &rgbPink,
- style: color.NewRGBStyle(rgbPinkLib).SetOpts(color.Opts{}),
+ Style: color.NewRGBStyle(rgbPinkLib).SetOpts(color.Opts{}),
},
// '38;2' qualifies an RGB foreground color
"\x1b[38;2;255;0;255mfoo\x1b[0m",
@@ -115,7 +115,7 @@ func TestMerge(t *testing.T) {
TextStyle{
fg: &rgbPink,
bg: &rgbYellow,
- style: color.NewRGBStyle(rgbPinkLib, rgbYellowLib).SetOpts(color.Opts{}),
+ 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",
@@ -130,7 +130,7 @@ func TestMerge(t *testing.T) {
bold: true,
underline: true,
},
- style: color.NewRGBStyle(rgbPinkLib, rgbYellowLib).SetOpts(color.Opts{color.OpBold, color.OpUnderscore}),
+ 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",
},
@@ -140,7 +140,7 @@ func TestMerge(t *testing.T) {
TextStyle{
fg: &rgbYellow,
bg: &Color{basic: &bgRed},
- style: color.NewRGBStyle(
+ Style: color.NewRGBStyle(
rgbYellowLib,
fgRed.RGB(), // We need to use FG here, https://github.com/gookit/color/issues/39
).SetOpts(color.Opts{}),
diff --git a/pkg/gui/style/text_style.go b/pkg/gui/style/text_style.go
index 4fbb7f1f4..30fa74035 100644
--- a/pkg/gui/style/text_style.go
+++ b/pkg/gui/style/text_style.go
@@ -30,7 +30,9 @@ type TextStyle struct {
bg *Color
decoration Decoration
- style Sprinter
+ // making this public so that we can use a type switch to get to the underlying
+ // value so we can cache styles. This is very much a hack.
+ Style Sprinter
}
type Sprinter interface {
@@ -40,16 +42,16 @@ type Sprinter interface {
func New() TextStyle {
s := TextStyle{}
- s.style = s.deriveStyle()
+ s.Style = s.deriveStyle()
return s
}
func (b TextStyle) Sprint(a ...interface{}) string {
- return b.style.Sprint(a...)
+ return b.Style.Sprint(a...)
}
func (b TextStyle) Sprintf(format string, a ...interface{}) string {
- return b.style.Sprintf(format, a...)
+ return b.Style.Sprintf(format, a...)
}
// note that our receiver here is not a pointer which means we're receiving a
@@ -57,31 +59,31 @@ func (b TextStyle) Sprintf(format string, a ...interface{}) string {
// TextStyle receiver without actually modifying the original.
func (b TextStyle) SetBold() TextStyle {
b.decoration.SetBold()
- b.style = b.deriveStyle()
+ b.Style = b.deriveStyle()
return b
}
func (b TextStyle) SetUnderline() TextStyle {
b.decoration.SetUnderline()
- b.style = b.deriveStyle()
+ b.Style = b.deriveStyle()
return b
}
func (b TextStyle) SetReverse() TextStyle {
b.decoration.SetReverse()
- b.style = b.deriveStyle()
+ b.Style = b.deriveStyle()
return b
}
func (b TextStyle) SetBg(color Color) TextStyle {
b.bg = &color
- b.style = b.deriveStyle()
+ b.Style = b.deriveStyle()
return b
}
func (b TextStyle) SetFg(color Color) TextStyle {
b.fg = &color
- b.style = b.deriveStyle()
+ b.Style = b.deriveStyle()
return b
}
@@ -96,7 +98,7 @@ func (b TextStyle) MergeStyle(other TextStyle) TextStyle {
b.bg = other.bg
}
- b.style = b.deriveStyle()
+ b.Style = b.deriveStyle()
return b
}