feat(renderer): add WithTermenvOutput option

This commit is contained in:
Ayman Bagabas 2022-10-06 12:37:18 -04:00
parent 4aa1f38785
commit e5401eb8b8
3 changed files with 12 additions and 5 deletions

View File

@ -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),

View File

@ -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
}

View File

@ -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")
}