2022-06-28 20:28:35 +03:00
|
|
|
package lipgloss
|
|
|
|
|
|
|
|
import (
|
2022-10-06 19:37:18 +03:00
|
|
|
"io"
|
|
|
|
|
2022-06-28 20:28:35 +03:00
|
|
|
"github.com/muesli/termenv"
|
|
|
|
)
|
|
|
|
|
|
|
|
var renderer = NewRenderer()
|
|
|
|
|
|
|
|
// Renderer is a lipgloss terminal renderer.
|
|
|
|
type Renderer struct {
|
|
|
|
output *termenv.Output
|
2022-10-03 20:11:44 +03:00
|
|
|
hasDarkBackground *bool
|
2022-06-28 20:28:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// RendererOption is a function that can be used to configure a Renderer.
|
|
|
|
type RendererOption func(r *Renderer)
|
|
|
|
|
|
|
|
// DefaultRenderer returns the default renderer.
|
|
|
|
func DefaultRenderer() *Renderer {
|
|
|
|
return renderer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRenderer creates a new Renderer.
|
2022-10-06 19:37:18 +03:00
|
|
|
func NewRenderer(options ...RendererOption) *Renderer {
|
2022-10-03 20:11:44 +03:00
|
|
|
r := &Renderer{
|
|
|
|
output: termenv.DefaultOutput(),
|
|
|
|
}
|
2022-06-28 20:28:35 +03:00
|
|
|
for _, option := range options {
|
|
|
|
option(r)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2022-10-06 19:37:18 +03:00
|
|
|
// 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 {
|
2022-06-28 20:28:35 +03:00
|
|
|
return func(r *Renderer) {
|
|
|
|
r.output = output
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 20:11:44 +03:00
|
|
|
// WithDarkBackground can force the renderer to use a light/dark background.
|
|
|
|
func WithDarkBackground(dark bool) RendererOption {
|
2022-06-28 20:28:35 +03:00
|
|
|
return func(r *Renderer) {
|
2022-10-03 20:11:44 +03:00
|
|
|
r.SetHasDarkBackground(dark)
|
2022-06-28 20:28:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithColorProfile 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 {
|
|
|
|
return func(r *Renderer) {
|
|
|
|
r.SetColorProfile(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ColorProfile returns the detected termenv color profile.
|
|
|
|
func (r *Renderer) ColorProfile() termenv.Profile {
|
|
|
|
return r.output.Profile
|
|
|
|
}
|
|
|
|
|
|
|
|
// ColorProfile returns the detected termenv color profile.
|
|
|
|
func ColorProfile() termenv.Profile {
|
|
|
|
return renderer.ColorProfile()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetColorProfile sets the color profile on the renderer. This function exists
|
|
|
|
// mostly for testing purposes so that you can assure you're testing against
|
|
|
|
// a specific profile.
|
|
|
|
//
|
|
|
|
// Outside of testing you likely won't want to use this function as the color
|
|
|
|
// profile will detect and cache the terminal's color capabilities and choose
|
|
|
|
// the best available profile.
|
|
|
|
//
|
|
|
|
// Available color profiles are:
|
|
|
|
//
|
|
|
|
// termenv.Ascii (no color, 1-bit)
|
|
|
|
// termenv.ANSI (16 colors, 4-bit)
|
|
|
|
// termenv.ANSI256 (256 colors, 8-bit)
|
|
|
|
// termenv.TrueColor (16,777,216 colors, 24-bit)
|
|
|
|
//
|
|
|
|
// This function is thread-safe.
|
|
|
|
func (r *Renderer) SetColorProfile(p termenv.Profile) {
|
|
|
|
r.output.Profile = p
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetColorProfile sets the color profile on the default renderer. This
|
|
|
|
// function exists mostly for testing purposes so that you can assure you're
|
|
|
|
// testing against a specific profile.
|
|
|
|
//
|
|
|
|
// Outside of testing you likely won't want to use this function as the color
|
|
|
|
// profile will detect and cache the terminal's color capabilities and choose
|
|
|
|
// the best available profile.
|
|
|
|
//
|
|
|
|
// Available color profiles are:
|
|
|
|
//
|
|
|
|
// termenv.Ascii (no color, 1-bit)
|
|
|
|
// termenv.ANSI (16 colors, 4-bit)
|
|
|
|
// termenv.ANSI256 (256 colors, 8-bit)
|
|
|
|
// termenv.TrueColor (16,777,216 colors, 24-bit)
|
|
|
|
//
|
|
|
|
// This function is thread-safe.
|
|
|
|
func SetColorProfile(p termenv.Profile) {
|
|
|
|
renderer.SetColorProfile(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasDarkBackground returns whether or not the terminal has a dark background.
|
|
|
|
func HasDarkBackground() bool {
|
|
|
|
return renderer.HasDarkBackground()
|
|
|
|
}
|
|
|
|
|
2022-10-03 20:11:44 +03:00
|
|
|
// HasDarkBackground returns whether or not the terminal has a dark background.
|
|
|
|
func (r *Renderer) HasDarkBackground() bool {
|
|
|
|
if r.hasDarkBackground != nil {
|
|
|
|
return *r.hasDarkBackground
|
|
|
|
}
|
|
|
|
return r.output.HasDarkBackground()
|
2022-06-28 20:28:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetHasDarkBackground sets the background color detection value for the
|
|
|
|
// default renderer. This function exists mostly for testing purposes so that
|
|
|
|
// you can assure you're testing against a specific background color setting.
|
|
|
|
//
|
|
|
|
// Outside of testing you likely won't want to use this function as the
|
|
|
|
// backgrounds value will be automatically detected and cached against the
|
|
|
|
// terminal's current background color setting.
|
|
|
|
//
|
|
|
|
// This function is thread-safe.
|
|
|
|
func SetHasDarkBackground(b bool) {
|
|
|
|
renderer.SetHasDarkBackground(b)
|
|
|
|
}
|
|
|
|
|
2022-10-03 20:11:44 +03:00
|
|
|
// SetHasDarkBackground sets the background color detection value on the
|
|
|
|
// renderer. This function exists mostly for testing purposes so that you can
|
|
|
|
// assure you're testing against a specific background color setting.
|
|
|
|
//
|
|
|
|
// Outside of testing you likely won't want to use this function as the
|
|
|
|
// backgrounds value will be automatically detected and cached against the
|
|
|
|
// terminal's current background color setting.
|
|
|
|
//
|
|
|
|
// This function is thread-safe.
|
|
|
|
func (r *Renderer) SetHasDarkBackground(b bool) {
|
|
|
|
r.hasDarkBackground = &b
|
2022-06-28 20:28:35 +03:00
|
|
|
}
|