Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: renderer race condition #210

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lipgloss

import (
"io"
"sync"

"github.com/muesli/termenv"
)
Expand All @@ -16,6 +17,7 @@ var renderer = &Renderer{
type Renderer struct {
output *termenv.Output
hasDarkBackground *bool
mtx sync.RWMutex
}

// RendererOption is a function that can be used to configure a [Renderer].
Expand Down Expand Up @@ -43,11 +45,15 @@ func NewRenderer(w io.Writer, opts ...termenv.OutputOption) *Renderer {

// Output returns the termenv output.
func (r *Renderer) Output() *termenv.Output {
r.mtx.RLock()
defer r.mtx.RUnlock()
return r.output
}

// SetOutput sets the termenv output.
func (r *Renderer) SetOutput(o *termenv.Output) {
r.mtx.Lock()
defer r.mtx.Unlock()
r.output = o
}

Expand Down Expand Up @@ -78,6 +84,8 @@ func ColorProfile() termenv.Profile {
//
// This function is thread-safe.
func (r *Renderer) SetColorProfile(p termenv.Profile) {
r.mtx.Lock()
defer r.mtx.Unlock()
r.output.Profile = p
}

Expand Down Expand Up @@ -110,6 +118,8 @@ func HasDarkBackground() bool {
// background. A dark background can either be auto-detected, or set explicitly
// on the renderer.
func (r *Renderer) HasDarkBackground() bool {
r.mtx.RLock()
aymanbagabas marked this conversation as resolved.
Show resolved Hide resolved
defer r.mtx.RUnlock()
if r.hasDarkBackground != nil {
return *r.hasDarkBackground
}
Expand Down Expand Up @@ -139,5 +149,7 @@ func SetHasDarkBackground(b bool) {
//
// This function is thread-safe.
func (r *Renderer) SetHasDarkBackground(b bool) {
r.mtx.Lock()
defer r.mtx.Unlock()
r.hasDarkBackground = &b
}
20 changes: 19 additions & 1 deletion renderer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lipgloss

import (
"io"
"os"
"testing"

Expand Down Expand Up @@ -29,7 +30,24 @@ func TestRendererWithOutput(t *testing.T) {
defer os.Remove(f.Name())
r := NewRenderer(f)
r.SetColorProfile(termenv.TrueColor)
if r.output.Profile != termenv.TrueColor {
if r.ColorProfile() != termenv.TrueColor {
t.Error("Expected renderer to use true color")
}
}

func TestRace(t *testing.T) {
r := NewRenderer(io.Discard)
o := r.Output()

for i := 0; i < 100; i++ {
t.Run("SetColorProfile", func(t *testing.T) {
t.Parallel()
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
r.SetHasDarkBackground(false)
r.HasDarkBackground()
r.SetOutput(o)
r.SetColorProfile(termenv.ANSI256)
r.SetHasDarkBackground(true)
r.Output()
})
}
}
7 changes: 4 additions & 3 deletions style.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@
var (
str = joinString(strs...)

te = s.r.ColorProfile().String()
teSpace = s.r.ColorProfile().String()
teWhitespace = s.r.ColorProfile().String()
p = s.r.ColorProfile()
te = p.String()
teSpace = p.String()
teWhitespace = p.String()

bold = s.getAsBool(boldKey, false)
italic = s.getAsBool(italicKey, false)
Expand Down Expand Up @@ -332,7 +333,7 @@
}

// Padding
if !inline {

Check failure on line 336 in style.go

View workflow job for this annotation

GitHub Actions / lint-soft

`if !inline` has complex nested blocks (complexity: 8) (nestif)

Check failure on line 336 in style.go

View workflow job for this annotation

GitHub Actions / lint-soft

`if !inline` has complex nested blocks (complexity: 8) (nestif)
if leftPadding > 0 {
var st *termenv.Style
if colorWhitespace || styleWhitespace {
Expand Down
19 changes: 10 additions & 9 deletions style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,41 @@ import (
)

func TestStyleRender(t *testing.T) {
renderer.SetColorProfile(termenv.TrueColor)
renderer.SetHasDarkBackground(true)
r := NewRenderer(io.Discard)
r.SetColorProfile(termenv.TrueColor)
r.SetHasDarkBackground(true)
t.Parallel()

tt := []struct {
style Style
expected string
}{
{
NewStyle().Foreground(Color("#5A56E0")),
r.NewStyle().Foreground(Color("#5A56E0")),
"\x1b[38;2;89;86;224mhello\x1b[0m",
},
{
NewStyle().Foreground(AdaptiveColor{Light: "#fffe12", Dark: "#5A56E0"}),
r.NewStyle().Foreground(AdaptiveColor{Light: "#fffe12", Dark: "#5A56E0"}),
"\x1b[38;2;89;86;224mhello\x1b[0m",
},
{
NewStyle().Bold(true),
r.NewStyle().Bold(true),
"\x1b[1mhello\x1b[0m",
},
{
NewStyle().Italic(true),
r.NewStyle().Italic(true),
"\x1b[3mhello\x1b[0m",
},
{
NewStyle().Underline(true),
r.NewStyle().Underline(true),
"\x1b[4;4mh\x1b[0m\x1b[4;4me\x1b[0m\x1b[4;4ml\x1b[0m\x1b[4;4ml\x1b[0m\x1b[4;4mo\x1b[0m",
},
{
NewStyle().Blink(true),
r.NewStyle().Blink(true),
"\x1b[5mhello\x1b[0m",
},
{
NewStyle().Faint(true),
r.NewStyle().Faint(true),
"\x1b[2mhello\x1b[0m",
},
}
Expand Down
Loading