Skip to content

Commit c5382b3

Browse files
committed
ref(renderer): change renderer impl
1 parent d09532a commit c5382b3

File tree

5 files changed

+51
-52
lines changed

5 files changed

+51
-52
lines changed

renderer.go

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package lipgloss
22

33
import (
44
"io"
5+
"os"
56

67
"github.com/muesli/termenv"
78
)
89

9-
var renderer = NewRenderer()
10+
var renderer = NewRenderer(os.Stdout)
1011

1112
// Renderer is a lipgloss terminal renderer.
1213
type Renderer struct {
@@ -22,43 +23,29 @@ func DefaultRenderer() *Renderer {
2223
return renderer
2324
}
2425

26+
// SetDefaultRenderer sets the default global renderer.
27+
func SetDefaultRenderer(r *Renderer) {
28+
renderer = r
29+
}
30+
2531
// NewRenderer creates a new Renderer.
26-
func NewRenderer(options ...RendererOption) *Renderer {
32+
//
33+
// w will be used to determine the terminal's color capabilities.
34+
func NewRenderer(w io.Writer, opts ...termenv.OutputOption) *Renderer {
2735
r := &Renderer{
28-
output: termenv.DefaultOutput(),
29-
}
30-
for _, option := range options {
31-
option(r)
36+
output: termenv.NewOutput(w, opts...),
3237
}
3338
return r
3439
}
3540

36-
// WithOutput sets the io.Writer to be used for rendering.
37-
func WithOutput(w io.Writer) RendererOption {
38-
return WithTermenvOutput(termenv.NewOutput(w))
41+
// Output returns the termenv output.
42+
func (r *Renderer) Output() *termenv.Output {
43+
return r.output
3944
}
4045

41-
// WithTermenvOutput sets the termenv Output to use for rendering.
42-
func WithTermenvOutput(output *termenv.Output) RendererOption {
43-
return func(r *Renderer) {
44-
r.output = output
45-
}
46-
}
47-
48-
// WithDarkBackground can force the renderer to use a light/dark background.
49-
func WithDarkBackground(dark bool) RendererOption {
50-
return func(r *Renderer) {
51-
r.SetHasDarkBackground(dark)
52-
}
53-
}
54-
55-
// WithColorProfile sets the color profile on the renderer. This function is
56-
// primarily intended for testing. For details, see the note on
57-
// [Renderer.SetColorProfile].
58-
func WithColorProfile(p termenv.Profile) RendererOption {
59-
return func(r *Renderer) {
60-
r.SetColorProfile(p)
61-
}
46+
// SetOutput sets the termenv output.
47+
func (r *Renderer) SetOutput(o *termenv.Output) {
48+
r.output = o
6249
}
6350

6451
// ColorProfile returns the detected termenv color profile.

renderer_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import (
88
)
99

1010
func TestRendererHasDarkBackground(t *testing.T) {
11-
r1 := NewRenderer(WithDarkBackground(false))
11+
r1 := NewRenderer(os.Stdout)
12+
r1.SetHasDarkBackground(false)
1213
if r1.HasDarkBackground() {
1314
t.Error("Expected renderer to have light background")
1415
}
15-
r2 := NewRenderer(WithDarkBackground(true))
16+
r2 := NewRenderer(os.Stdout)
17+
r2.SetHasDarkBackground(true)
1618
if !r2.HasDarkBackground() {
1719
t.Error("Expected renderer to have dark background")
1820
}
@@ -25,8 +27,8 @@ func TestRendererWithOutput(t *testing.T) {
2527
}
2628
defer f.Close()
2729
defer os.Remove(f.Name())
28-
output := termenv.NewOutput(f, termenv.WithProfile(termenv.TrueColor))
29-
r := NewRenderer(WithTermenvOutput(output))
30+
r := NewRenderer(f)
31+
r.SetColorProfile(termenv.TrueColor)
3032
if r.output.Profile != termenv.TrueColor {
3133
t.Error("Expected renderer to use true color")
3234
}

set.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,13 @@ func (s Style) StrikethroughSpaces(v bool) Style {
520520
return s
521521
}
522522

523+
// Renderer sets the renderer for the style. This is useful for changing the
524+
// renderer for a style that is being used in a different context.
525+
func (s Style) Renderer(r *Renderer) Style {
526+
s.r = r
527+
return s
528+
}
529+
523530
// whichSidesInt is a helper method for setting values on sides of a block based
524531
// on the number of arguments. It follows the CSS shorthand rules for blocks
525532
// like margin, padding. and borders. Here are how the rules work:

style.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,33 +75,20 @@ const (
7575
// A set of properties.
7676
type rules map[propKey]interface{}
7777

78-
// StyleOption is a function that applies a style option to a Style.
79-
type StyleOption func(*Style)
80-
81-
// WithString sets the underlying string value for this style.
82-
func WithString(strs ...string) StyleOption {
83-
return func(s *Style) {
84-
s.value = joinString(strs...)
85-
}
86-
}
87-
8878
// NewStyle returns a new, empty Style. While it's syntactic sugar for the
8979
// Style{} primitive, it's recommended to use this function for creating styles
9080
// in case the underlying implementation changes. It takes an optional string
9181
// value to be set as the underlying string value for this style.
92-
func NewStyle(opts ...StyleOption) Style {
93-
return renderer.NewStyle(opts...)
82+
func NewStyle() Style {
83+
return renderer.NewStyle()
9484
}
9585

9686
// NewStyle returns a new, empty Style. While it's syntactic sugar for the
9787
// Style{} primitive, it's recommended to use this function for creating styles
9888
// in case the underlying implementation changes. It takes an optional string
9989
// value to be set as the underlying string value for this style.
100-
func (r *Renderer) NewStyle(opts ...StyleOption) Style {
90+
func (r *Renderer) NewStyle() Style {
10191
s := Style{r: r}
102-
for _, opt := range opts {
103-
opt(&s)
104-
}
10592
return s
10693
}
10794

style_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package lipgloss
22

33
import (
4+
"io/ioutil"
45
"reflect"
56
"testing"
67

@@ -58,7 +59,9 @@ func TestStyleRender(t *testing.T) {
5859
}
5960

6061
func TestStyleCustomRender(t *testing.T) {
61-
r := NewRenderer(WithColorProfile(termenv.TrueColor), WithDarkBackground(false))
62+
r := NewRenderer(ioutil.Discard)
63+
r.SetHasDarkBackground(false)
64+
r.SetColorProfile(termenv.TrueColor)
6265
tt := []struct {
6366
style Style
6467
expected string
@@ -91,6 +94,10 @@ func TestStyleCustomRender(t *testing.T) {
9194
r.NewStyle().Faint(true),
9295
"\x1b[2mhello\x1b[0m",
9396
},
97+
{
98+
NewStyle().Faint(true).Renderer(r),
99+
"\x1b[2mhello\x1b[0m",
100+
},
94101
}
95102

96103
for i, tc := range tt {
@@ -104,6 +111,15 @@ func TestStyleCustomRender(t *testing.T) {
104111
}
105112
}
106113

114+
func TestStyleRenderer(t *testing.T) {
115+
r := NewRenderer(ioutil.Discard)
116+
s1 := NewStyle().Bold(true)
117+
s2 := s1.Renderer(r)
118+
if s1.r == s2.r {
119+
t.Fatalf("expected different renderers")
120+
}
121+
}
122+
107123
func TestValueCopy(t *testing.T) {
108124
t.Parallel()
109125

@@ -323,7 +339,7 @@ func TestStyleValue(t *testing.T) {
323339
},
324340
{
325341
name: "new style with string",
326-
style: NewStyle(WithString("bar", "foobar")),
342+
style: NewStyle().SetString("bar", "foobar"),
327343
expected: "bar foobar foo",
328344
},
329345
}

0 commit comments

Comments
 (0)