Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions glamour.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions glamour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (

func TestTermRendererWriter(t *testing.T) {
r, err := NewTermRenderer(
WithStandardStyle("dark"),
WithStandardStyle(DarkStyle),
)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestStyles(t *testing.T) {
}

_, err = NewTermRenderer(
WithStandardStyle("auto"),
WithStandardStyle(AutoStyle),
)
if err != nil {
t.Fatal(err)
Expand Down
12 changes: 6 additions & 6 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
)

Expand Down
2 changes: 1 addition & 1 deletion testdata/fuzz/fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading