From e5401eb8b85cc12b18b1479737f57b039de608e6 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 6 Oct 2022 12:37:18 -0400 Subject: [PATCH] feat(renderer): add WithTermenvOutput option --- examples/ssh/main.go | 2 +- renderer.go | 13 ++++++++++--- renderer_test.go | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/examples/ssh/main.go b/examples/ssh/main.go index ad00690..b5e543f 100644 --- a/examples/ssh/main.go +++ b/examples/ssh/main.go @@ -80,7 +80,7 @@ func main() { } w, _ := pty.Window.Width, pty.Window.Height - renderer := lipgloss.NewRenderer(lipgloss.WithOutput(output)) + renderer := lipgloss.NewRenderer(lipgloss.WithTermenvOutput(output)) str := strings.Builder{} fmt.Fprintf(&str, "\n%s %s %s %s %s", renderer.NewStyle("bold").Bold(true), diff --git a/renderer.go b/renderer.go index 04f3118..28ca477 100644 --- a/renderer.go +++ b/renderer.go @@ -1,6 +1,8 @@ package lipgloss import ( + "io" + "github.com/muesli/termenv" ) @@ -21,7 +23,7 @@ func DefaultRenderer() *Renderer { } // NewRenderer creates a new Renderer. -func NewRenderer(options ...func(r *Renderer)) *Renderer { +func NewRenderer(options ...RendererOption) *Renderer { r := &Renderer{ output: termenv.DefaultOutput(), } @@ -31,8 +33,13 @@ func NewRenderer(options ...func(r *Renderer)) *Renderer { return r } -// WithOutput sets the termenv Output to use for rendering. -func WithOutput(output *termenv.Output) RendererOption { +// WithOutput sets the io.Writer to be used for rendering. +func WithOutput(w io.Writer) RendererOption { + return WithTermenvOutput(termenv.NewOutput(w)) +} + +// WithTermenvOutput sets the termenv Output to use for rendering. +func WithTermenvOutput(output *termenv.Output) RendererOption { return func(r *Renderer) { r.output = output } diff --git a/renderer_test.go b/renderer_test.go index fed38fc..2be6430 100644 --- a/renderer_test.go +++ b/renderer_test.go @@ -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(WithOutput(output)) + r := NewRenderer(WithTermenvOutput(output)) if r.output.Profile != termenv.TrueColor { t.Error("Expected renderer to use true color") }