From e6a28b4fba1d0928c1e37fa5a489efc3fc9bf762 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Wed, 8 Apr 2020 10:08:03 +0200 Subject: [PATCH] Support 'auto' style to make it accessible by the Render(Bytes) helper function --- glamour.go | 17 ++++++++--------- glamour_test.go | 7 +++++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/glamour.go b/glamour.go index 386ff3b..b1c921e 100644 --- a/glamour.go +++ b/glamour.go @@ -114,15 +114,7 @@ func WithStandardStyle(style string) TermRendererOption { // WithAutoStyle sets a TermRenderer's styles with either the standard dark // or light style, depending on the terminal's background color at run-time. func WithAutoStyle() TermRendererOption { - return func(tr *TermRenderer) error { - style := LightStyleConfig - if termenv.HasDarkBackground() { - style = DarkStyleConfig - } - - tr.ansiOptions.Styles = style - return nil - } + return WithStandardStyle("auto") } // WithStylePath sets a TermRenderer's style from stylePath. stylePath is first @@ -216,6 +208,13 @@ func (tr *TermRenderer) RenderBytes(in []byte) ([]byte, error) { } func getDefaultStyle(style string) (*ansi.StyleConfig, error) { + if style == "auto" { + if termenv.HasDarkBackground() { + return &DarkStyleConfig, nil + } + return &LightStyleConfig, nil + } + styles, ok := DefaultStyles[style] if !ok { return nil, fmt.Errorf("%s: style not found", style) diff --git a/glamour_test.go b/glamour_test.go index 9e49c5a..c6ea503 100644 --- a/glamour_test.go +++ b/glamour_test.go @@ -97,6 +97,13 @@ func TestStyles(t *testing.T) { if err != nil { t.Fatal(err) } + + _, err = NewTermRenderer( + WithStandardStyle("auto"), + ) + if err != nil { + t.Fatal(err) + } } func TestRenderHelpers(t *testing.T) {