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
+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() {