77 "github.com/charmbracelet/glamour"
88)
99
10- type RenderOpts []glamour.TermRendererOption
11-
1210func WithoutIndentation () glamour.TermRendererOption {
1311 overrides := []byte (`
1412 {
@@ -23,75 +21,42 @@ func WithoutIndentation() glamour.TermRendererOption {
2321 return glamour .WithStylesFromJSONBytes (overrides )
2422}
2523
26- func WithoutWrap () glamour.TermRendererOption {
27- return glamour .WithWordWrap (0 )
28- }
29-
30- func render (text string , opts RenderOpts ) (string , error ) {
31- // Glamour rendering preserves carriage return characters in code blocks, but
32- // we need to ensure that no such characters are present in the output.
33- text = strings .ReplaceAll (text , "\r \n " , "\n " )
34-
35- tr , err := glamour .NewTermRenderer (opts ... )
36- if err != nil {
37- return "" , err
38- }
39-
40- return tr .Render (text )
41- }
42-
43- func Render (text , style string ) (string , error ) {
44- opts := RenderOpts {
45- glamour .WithStylePath (style ),
46- glamour .WithEmoji (),
47- }
48-
49- return render (text , opts )
24+ func WithWrap (w int ) glamour.TermRendererOption {
25+ return glamour .WithWordWrap (w )
5026}
5127
52- func RenderWithOpts (text , style string , opts RenderOpts ) (string , error ) {
53- defaultOpts := RenderOpts {
54- glamour .WithStylePath (style ),
55- glamour .WithEmoji (),
56- }
57- opts = append (defaultOpts , opts ... )
58-
59- return render (text , opts )
28+ type IOStreams interface {
29+ TerminalTheme () string
6030}
6131
62- func RenderWithBaseURL (text , style , baseURL string ) (string , error ) {
63- opts := RenderOpts {
64- glamour .WithStylePath (style ),
65- glamour .WithEmoji (),
66- glamour .WithBaseURL (baseURL ),
32+ func WithIO (io IOStreams ) glamour.TermRendererOption {
33+ style := os .Getenv ("GLAMOUR_STYLE" )
34+ if style == "" || style == "auto" {
35+ theme := io .TerminalTheme ()
36+ switch theme {
37+ case "light" , "dark" :
38+ style = theme
39+ default :
40+ style = "notty"
41+ }
6742 }
68-
69- return render (text , opts )
43+ return glamour .WithStylePath (style )
7044}
7145
72- func RenderWithWrap (text , style string , wrap int ) (string , error ) {
73- opts := RenderOpts {
74- glamour .WithStylePath (style ),
75- glamour .WithEmoji (),
76- glamour .WithWordWrap (wrap ),
77- }
78-
79- return render (text , opts )
46+ func WithBaseURL (u string ) glamour.TermRendererOption {
47+ return glamour .WithBaseURL (u )
8048}
8149
82- func GetStyle (defaultStyle string ) string {
83- style := fromEnv ()
84- if style != "" && style != "auto" {
85- return style
86- }
50+ func Render (text string , opts ... glamour.TermRendererOption ) (string , error ) {
51+ // Glamour rendering preserves carriage return characters in code blocks, but
52+ // we need to ensure that no such characters are present in the output.
53+ text = strings .ReplaceAll (text , "\r \n " , "\n " )
8754
88- if defaultStyle == "light" || defaultStyle == "dark" {
89- return defaultStyle
55+ opts = append (opts , glamour .WithEmoji ())
56+ tr , err := glamour .NewTermRenderer (opts ... )
57+ if err != nil {
58+ return "" , err
9059 }
9160
92- return "notty"
93- }
94-
95- var fromEnv = func () string {
96- return os .Getenv ("GLAMOUR_STYLE" )
61+ return tr .Render (text )
9762}
0 commit comments