GLAMOUR_STYLE environment variable support (#69)

This commit is contained in:
Jesse Millar
2020-05-27 17:07:25 +02:00
committed by GitHub
parent e859bb067c
commit d3564681b9
4 changed files with 35 additions and 0 deletions
+5
View File
@@ -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
+22
View File
@@ -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() {
+7
View File
@@ -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) {
+1
View File
@@ -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=