Skip to content

Commit

Permalink
ref(renderer): rename renderer options & add clone
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Mar 2, 2023
1 parent 1eab58f commit 777dcd6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
35 changes: 26 additions & 9 deletions renderer.go
Expand Up @@ -22,6 +22,11 @@ func DefaultRenderer() *Renderer {
return renderer
}

// SetDefaultRenderer sets the default global renderer.
func SetDefaultRenderer(r *Renderer) {
renderer = r
}

// NewRenderer creates a new Renderer.
func NewRenderer(options ...RendererOption) *Renderer {
r := &Renderer{
Expand All @@ -33,29 +38,29 @@ func NewRenderer(options ...RendererOption) *Renderer {
return r
}

// WithOutput sets the io.Writer to be used for rendering.
func WithOutput(w io.Writer) RendererOption {
return WithTermenvOutput(termenv.NewOutput(w))
// WithRendererOutput sets the io.Writer to be used for rendering.
func WithRendererOutput(w io.Writer) RendererOption {
return WithRendererTermenvOutput(termenv.NewOutput(w))
}

// WithTermenvOutput sets the termenv Output to use for rendering.
func WithTermenvOutput(output *termenv.Output) RendererOption {
// WithRendererTermenvOutput sets the termenv Output to use for rendering.
func WithRendererTermenvOutput(output *termenv.Output) RendererOption {
return func(r *Renderer) {
r.output = output
}
}

// WithDarkBackground can force the renderer to use a light/dark background.
func WithDarkBackground(dark bool) RendererOption {
// WithRendererDarkBackground can force the renderer to use a light/dark background.
func WithRendererDarkBackground(dark bool) RendererOption {
return func(r *Renderer) {
r.SetHasDarkBackground(dark)
}
}

// WithColorProfile sets the color profile on the renderer. This function is
// WithRendererColorProfile sets the color profile on the renderer. This function is
// primarily intended for testing. For details, see the note on
// [Renderer.SetColorProfile].
func WithColorProfile(p termenv.Profile) RendererOption {
func WithRendererColorProfile(p termenv.Profile) RendererOption {
return func(r *Renderer) {
r.SetColorProfile(p)
}
Expand Down Expand Up @@ -149,3 +154,15 @@ func SetHasDarkBackground(b bool) {
func (r *Renderer) SetHasDarkBackground(b bool) {
r.hasDarkBackground = &b
}

// Clone returns a new copy of s that uses the renderer.
func (r *Renderer) Clone(s Style) Style {
n := s.Copy()
n.r = r
return n
}

// Clone returns a new copy of s that uses the default renderer.
func Clone(s Style) Style {
return renderer.Clone(s)
}
6 changes: 3 additions & 3 deletions renderer_test.go
Expand Up @@ -8,11 +8,11 @@ import (
)

func TestRendererHasDarkBackground(t *testing.T) {
r1 := NewRenderer(WithDarkBackground(false))
r1 := NewRenderer(WithRendererDarkBackground(false))
if r1.HasDarkBackground() {
t.Error("Expected renderer to have light background")
}
r2 := NewRenderer(WithDarkBackground(true))
r2 := NewRenderer(WithRendererDarkBackground(true))
if !r2.HasDarkBackground() {
t.Error("Expected renderer to have dark background")
}
Expand All @@ -26,7 +26,7 @@ func TestRendererWithOutput(t *testing.T) {
defer f.Close()
defer os.Remove(f.Name())
output := termenv.NewOutput(f, termenv.WithProfile(termenv.TrueColor))
r := NewRenderer(WithTermenvOutput(output))
r := NewRenderer(WithRendererTermenvOutput(output))
if r.output.Profile != termenv.TrueColor {
t.Error("Expected renderer to use true color")
}
Expand Down
4 changes: 2 additions & 2 deletions style.go
Expand Up @@ -78,8 +78,8 @@ type rules map[propKey]interface{}
// StyleOption is a function that applies a style option to a Style.
type StyleOption func(*Style)

// WithString sets the underlying string value for this style.
func WithString(strs ...string) StyleOption {
// WithStyleString sets the underlying string value for this style.
func WithStyleString(strs ...string) StyleOption {
return func(s *Style) {
s.value = joinString(strs...)
}
Expand Down
4 changes: 2 additions & 2 deletions style_test.go
Expand Up @@ -58,7 +58,7 @@ func TestStyleRender(t *testing.T) {
}

func TestStyleCustomRender(t *testing.T) {
r := NewRenderer(WithColorProfile(termenv.TrueColor), WithDarkBackground(false))
r := NewRenderer(WithRendererColorProfile(termenv.TrueColor), WithRendererDarkBackground(false))
tt := []struct {
style Style
expected string
Expand Down Expand Up @@ -323,7 +323,7 @@ func TestStyleValue(t *testing.T) {
},
{
name: "new style with string",
style: NewStyle(WithString("bar", "foobar")),
style: NewStyle(WithStyleString("bar", "foobar")),
expected: "bar foobar foo",
},
}
Expand Down

0 comments on commit 777dcd6

Please sign in to comment.