feat: use (exported) consts for default styles (#232)

This commit is contained in:
Christian Rocha
2023-05-19 09:14:05 -04:00
committed by GitHub
parent cd217b9c0f
commit 7528eaad66
4 changed files with 23 additions and 12 deletions
+14 -3
View File
@@ -18,6 +18,17 @@ import (
"github.com/charmbracelet/glamour/ansi"
)
// Default styles.
const (
AsciiStyle = "ascii"
AutoStyle = "auto"
DarkStyle = "dark"
DraculaStyle = "dracula"
LightStyle = "light"
NoTTYStyle = "notty"
PinkStyle = "pink"
)
// A TermRendererOption sets an option on a TermRenderer.
type TermRendererOption func(*TermRenderer) error
@@ -122,7 +133,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 WithStandardStyle("auto")
return WithStandardStyle(AutoStyle)
}
// WithEnvironmentConfig sets a TermRenderer's styles based on the
@@ -237,14 +248,14 @@ func (tr *TermRenderer) RenderBytes(in []byte) ([]byte, error) {
func getEnvironmentStyle() string {
glamourStyle := os.Getenv("GLAMOUR_STYLE")
if len(glamourStyle) == 0 {
glamourStyle = "auto"
glamourStyle = AutoStyle
}
return glamourStyle
}
func getDefaultStyle(style string) (*ansi.StyleConfig, error) {
if style == "auto" {
if style == AutoStyle {
if termenv.HasDarkBackground() {
return &DarkStyleConfig, nil
}
+2 -2
View File
@@ -15,7 +15,7 @@ const (
func TestTermRendererWriter(t *testing.T) {
r, err := NewTermRenderer(
WithStandardStyle("dark"),
WithStandardStyle(DarkStyle),
)
if err != nil {
t.Fatal(err)
@@ -152,7 +152,7 @@ func TestStyles(t *testing.T) {
}
_, err = NewTermRenderer(
WithStandardStyle("auto"),
WithStandardStyle(AutoStyle),
)
if err != nil {
t.Fatal(err)
+6 -6
View File
@@ -759,12 +759,12 @@ var (
// DefaultStyles are the default styles.
DefaultStyles = map[string]*ansi.StyleConfig{
"ascii": &ASCIIStyleConfig,
"dark": &DarkStyleConfig,
"light": &LightStyleConfig,
"pink": &PinkStyleConfig,
"notty": &NoTTYStyleConfig,
"dracula": &DraculaStyleConfig,
AsciiStyle: &ASCIIStyleConfig,
DarkStyle: &DarkStyleConfig,
DraculaStyle: &DraculaStyleConfig,
LightStyle: &LightStyleConfig,
NoTTYStyle: &NoTTYStyleConfig,
PinkStyle: &PinkStyleConfig,
}
)
+1 -1
View File
@@ -3,7 +3,7 @@ package fuzzing
import "github.com/charmbracelet/glamour"
func Fuzz(data []byte) int {
_, err := glamour.RenderBytes(data, "dark")
_, err := glamour.RenderBytes(data, glamour.DarkStyle)
if err != nil {
return 0
}