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

fix: tokyo night #320

Merged
merged 10 commits into from
Aug 5, 2024
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
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ Please submit a pull request for minor changes and submit issues for major chang
## Testing

When providing a new feature or bug fix, please provide tests that demonstrate the issue along with your fix.

## Themes

New styles need to be implemented in `styles/<style-name>.go`, and then `go generate
./...` will create the JSON files from it.

You can look up all references of another theme (e.g. Dracula), and add your
theme accordingly.
46 changes: 46 additions & 0 deletions examples/stdin-stdout-custom-styles/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

// A simple example that renders input through a pipe.
//
// Usage:
// echo "# Hello, world!" | go run main.go
//
// cat README.md | go run main.go
//
// go run main.go < README.md

import (
"fmt"
"io"
"os"

"github.com/charmbracelet/glamour"
)

const defaultWidth = 80

func main() {
// Read from stdin.
in, err := io.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading stdin: %s\n", err)
}

// Create a new renderer.
r, err := glamour.NewTermRenderer(
glamour.WithEnvironmentConfig(),
glamour.WithWordWrap(defaultWidth),
)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating renderer: %s\n", err)
}

// Render markdown.
md, err := r.RenderBytes(in)
if err != nil {
fmt.Fprintf(os.Stderr, "Error rendering markdown: %s\n", err)
}

// Write markdown to stdout.
fmt.Fprintf(os.Stdout, "%s\n", md)
}
26 changes: 8 additions & 18 deletions glamour.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,7 @@ import (
"golang.org/x/term"

"github.com/charmbracelet/glamour/ansi"
)

// Default styles.
const (
AsciiStyle = "ascii"
AutoStyle = "auto"
DarkStyle = "dark"
DraculaStyle = "dracula"
LightStyle = "light"
NoTTYStyle = "notty"
PinkStyle = "pink"
styles "github.com/charmbracelet/glamour/styles"
)

const (
Expand Down Expand Up @@ -138,7 +128,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(AutoStyle)
return WithStandardStyle(styles.AutoStyle)
}

// WithEnvironmentConfig sets a TermRenderer's styles based on the
Expand Down Expand Up @@ -253,24 +243,24 @@ func (tr *TermRenderer) RenderBytes(in []byte) ([]byte, error) {
func getEnvironmentStyle() string {
glamourStyle := os.Getenv("GLAMOUR_STYLE")
if len(glamourStyle) == 0 {
glamourStyle = AutoStyle
glamourStyle = styles.AutoStyle
}

return glamourStyle
}

func getDefaultStyle(style string) (*ansi.StyleConfig, error) {
if style == AutoStyle {
if style == styles.AutoStyle {
if !term.IsTerminal(int(os.Stdout.Fd())) {
return &NoTTYStyleConfig, nil
return &styles.NoTTYStyleConfig, nil
}
if termenv.HasDarkBackground() {
return &DarkStyleConfig, nil
return &styles.DarkStyleConfig, nil
}
return &LightStyleConfig, nil
return &styles.LightStyleConfig, nil
}

styles, ok := DefaultStyles[style]
styles, ok := styles.DefaultStyles[style]
if !ok {
return nil, fmt.Errorf("%s: style not found", style)
}
Expand Down
13 changes: 7 additions & 6 deletions glamour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"strings"
"testing"

styles "github.com/charmbracelet/glamour/styles"
"github.com/charmbracelet/x/exp/golden"
)

const markdown = "testdata/readme.markdown.in"

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

_, err = NewTermRenderer(
WithStandardStyle(AutoStyle),
WithStandardStyle(styles.AutoStyle),
)
if err != nil {
t.Fatal(err)
Expand All @@ -141,8 +142,8 @@ func TestCustomStyle(t *testing.T) {
expected string
}{
{name: "style exists", stylePath: "testdata/custom.style", err: nil, expected: "testdata/custom.style"},
{name: "style doesn't exist", stylePath: "testdata/notfound.style", err: os.ErrNotExist, expected: AutoStyle},
{name: "style is empty", stylePath: "", err: nil, expected: AutoStyle},
{name: "style doesn't exist", stylePath: "testdata/notfound.style", err: os.ErrNotExist, expected: styles.AutoStyle},
{name: "style is empty", stylePath: "", err: nil, expected: styles.AutoStyle},
}

for _, tc := range tests {
Expand Down Expand Up @@ -186,7 +187,7 @@ func TestRenderHelpers(t *testing.T) {

func TestCapitalization(t *testing.T) {
p := true
style := DarkStyleConfig
style := styles.DarkStyleConfig
style.H1.Upper = &p
style.H2.Title = &p
style.H3.Lower = &p
Expand All @@ -209,7 +210,7 @@ func TestCapitalization(t *testing.T) {
func FuzzData(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
func() int {
_, err := RenderBytes(data, DarkStyle)
_, err := RenderBytes(data, styles.DarkStyle)
if err != nil {
return 0
}
Expand Down
4 changes: 2 additions & 2 deletions internal/generate-style-json/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"
"path/filepath"

"github.com/charmbracelet/glamour"
"github.com/charmbracelet/glamour/ansi"
styles "github.com/charmbracelet/glamour/styles"
)

func writeStyleJSON(filename string, styleConfig *ansi.StyleConfig) error {
Expand All @@ -22,7 +22,7 @@ func writeStyleJSON(filename string, styleConfig *ansi.StyleConfig) error {
}

func run() error {
for style, styleConfig := range glamour.DefaultStyles {
for style, styleConfig := range styles.DefaultStyles {
if err := writeStyleJSON(filepath.Join("styles", style+".json"), styleConfig); err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion dracula.go → styles/dracula.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package glamour
package styles

import "github.com/charmbracelet/glamour/ansi"

// DraculaStyleConfig is the dracula style.
var DraculaStyleConfig = ansi.StyleConfig{
Document: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Expand Down
12 changes: 8 additions & 4 deletions styles/gallery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

## Dark

![Glamour Dark Style](dark.png)
![Glamour Dark Style](./dark.png)

## Light

![Glamour Light Style](light.png)
![Glamour Light Style](./light.png)

## NoTTY

Pronounced _naughty_.

![Glamour NoTTY Style](notty.png)
![Glamour NoTTY Style](./notty.png)

## Dracula

![Dracula Style](dracula.png)
![Dracula Style](./dracula.png)

## Tokyo Night

![Tokyo Night Style](./tokyo-night.png)
Binary file added styles/gallery/tokyo-night.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 4 additions & 8 deletions styles/notty.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"block_quote": {
"indent": 1,
"indent_token": " "
"indent_token": "| "
},
"paragraph": {},
"list": {
Expand Down Expand Up @@ -56,7 +56,7 @@
"block_prefix": ". "
},
"task": {
"ticked": "[] ",
"ticked": "[x] ",
"unticked": "[ ] "
},
"link": {},
Expand All @@ -72,15 +72,11 @@
"code_block": {
"margin": 2
},
"table": {
"center_separator": "┼",
"column_separator": "│",
"row_separator": "─"
},
"table": {},
"definition_list": {},
"definition_term": {},
"definition_description": {
"block_prefix": "\n🠶 "
"block_prefix": "\n* "
},
"html_block": {},
"html_span": {}
Expand Down
31 changes: 23 additions & 8 deletions styles.go → styles/styles.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package glamour
package styles

//go:generate go run ./internal/generate-style-json
//go:generate go run ../internal/generate-style-json

import (
"github.com/charmbracelet/glamour/ansi"
Expand All @@ -12,6 +12,18 @@ const (
defaultMargin = 2
)

// Default styles.
const (
AsciiStyle = "ascii"
AutoStyle = "auto"
DarkStyle = "dark"
DraculaStyle = "dracula"
TokyoNightStyle = "tokyo-night"
LightStyle = "light"
NoTTYStyle = "notty"
PinkStyle = "pink"
)

var (
// ASCIIStyleConfig uses only ASCII characters.
ASCIIStyleConfig = ansi.StyleConfig{
Expand Down Expand Up @@ -647,12 +659,15 @@ var (

// DefaultStyles are the default styles.
DefaultStyles = map[string]*ansi.StyleConfig{
AsciiStyle: &ASCIIStyleConfig,
DarkStyle: &DarkStyleConfig,
DraculaStyle: &DraculaStyleConfig,
LightStyle: &LightStyleConfig,
NoTTYStyle: &NoTTYStyleConfig,
PinkStyle: &PinkStyleConfig,
AsciiStyle: &ASCIIStyleConfig,
DarkStyle: &DarkStyleConfig,
LightStyle: &LightStyleConfig,
NoTTYStyle: &NoTTYStyleConfig,
PinkStyle: &PinkStyleConfig,

// Popular themes
DraculaStyle: &DraculaStyleConfig,
TokyoNightStyle: &TokyoNightStyleConfig,
}
)

Expand Down
Loading