From d3564681b966ac71f86b684fadc51cac1d818344 Mon Sep 17 00:00:00 2001 From: Jesse Millar Date: Wed, 27 May 2020 10:07:25 -0500 Subject: [PATCH] GLAMOUR_STYLE environment variable support (#69) --- README.md | 5 +++++ glamour.go | 22 ++++++++++++++++++++++ glamour_test.go | 7 +++++++ go.sum | 1 + 4 files changed, 35 insertions(+) diff --git a/README.md b/README.md index 64e6e69..694c19e 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,11 @@ fmt.Print(out) You can find all available default styles in our [gallery](https://github.com/charmbracelet/glamour/tree/master/styles/gallery). Want to create your own style? [Learn how!](https://github.com/charmbracelet/glamour/tree/master/styles) +There are a few options for using a custom style: +1. Call `glamour.Render(inputText, "desiredStyle")` +1. Set the `GLAMOUR_STYLE` environment variable to your desired default style or a file location for a style and call `glamour.RenderWithEnvironmentConfig(inputText)` +1. Set the `GLAMOUR_STYLE` environment variable and pass `glamour.WithEnvironmentConfig()` to your custom renderer + ## Glamourous Projects diff --git a/glamour.go b/glamour.go index b1c921e..90e49ec 100644 --- a/glamour.go +++ b/glamour.go @@ -36,6 +36,13 @@ func Render(in string, stylePath string) (string, error) { return string(b), err } +// RenderWithEnvironmentConfig initializes a new TermRenderer and renders a +// markdown with a specific style defined by the GLAMOUR_STYLE environment variable. +func RenderWithEnvironmentConfig(in string) (string, error) { + b, err := RenderBytes([]byte(in), getEnvironmentStyle()) + return string(b), err +} + // RenderBytes initializes a new TermRenderer and renders a markdown with a // specific style. func RenderBytes(in []byte, stylePath string) ([]byte, error) { @@ -117,6 +124,12 @@ func WithAutoStyle() TermRendererOption { return WithStandardStyle("auto") } +// WithEnvironmentConfig sets a TermRenderer's styles based on the +// GLAMOUR_STYLE environment variable. +func WithEnvironmentConfig() TermRendererOption { + return WithStylePath(getEnvironmentStyle()) +} + // WithStylePath sets a TermRenderer's style from stylePath. stylePath is first // interpreted as a filename. If no such file exists, it is re-interpreted as a // standard style. @@ -207,6 +220,15 @@ func (tr *TermRenderer) RenderBytes(in []byte) ([]byte, error) { return buf.Bytes(), err } +func getEnvironmentStyle() string { + glamourStyle := os.Getenv("GLAMOUR_STYLE") + if len(glamourStyle) == 0 { + glamourStyle = "auto" + } + + return glamourStyle +} + func getDefaultStyle(style string) (*ansi.StyleConfig, error) { if style == "auto" { if termenv.HasDarkBackground() { diff --git a/glamour_test.go b/glamour_test.go index c6ea503..29296db 100644 --- a/glamour_test.go +++ b/glamour_test.go @@ -104,6 +104,13 @@ func TestStyles(t *testing.T) { if err != nil { t.Fatal(err) } + + _, err = NewTermRenderer( + WithEnvironmentConfig(), + ) + if err != nil { + t.Fatal(err) + } } func TestRenderHelpers(t *testing.T) { diff --git a/go.sum b/go.sum index 2be38f7..93b3dfe 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,7 @@ github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk= github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4= github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4=