diff --git a/renderer.go b/renderer.go index 28ca477e..72728dcf 100644 --- a/renderer.go +++ b/renderer.go @@ -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{ @@ -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) } @@ -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) +} diff --git a/renderer_test.go b/renderer_test.go index 2be6430e..731e8bd8 100644 --- a/renderer_test.go +++ b/renderer_test.go @@ -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") } @@ -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") } diff --git a/style.go b/style.go index 395529a1..9654069f 100644 --- a/style.go +++ b/style.go @@ -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...) } diff --git a/style_test.go b/style_test.go index d0a0ee0f..3e0ba22d 100644 --- a/style_test.go +++ b/style_test.go @@ -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 @@ -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", }, }