@@ -5,9 +5,12 @@ import (
5
5
"fmt"
6
6
"os"
7
7
"strings"
8
+ "sync"
8
9
10
+ "github.com/alecthomas/chroma/v2"
11
+ chromaStyles "github.com/alecthomas/chroma/v2/styles"
9
12
"github.com/charmbracelet/glamour/ansi"
10
- "github.com/charmbracelet/glamour/styles"
13
+ glamourStyles "github.com/charmbracelet/glamour/styles"
11
14
"github.com/charmbracelet/lipgloss"
12
15
"github.com/go-viper/mapstructure/v2"
13
16
"github.com/goccy/go-yaml"
@@ -17,8 +20,11 @@ import (
17
20
18
21
const (
19
22
DefaultBorderColor = "#9999CC"
23
+ chromaStyleTheme = "kyma"
20
24
)
21
25
26
+ var chromaMutex = sync.Mutex {}
27
+
22
28
type Properties struct {
23
29
Title string `yaml:"title"`
24
30
Style StyleConfig `yaml:"style"`
@@ -173,16 +179,16 @@ func getLayout(layout string) (lipgloss.Style, error) {
173
179
}
174
180
175
181
func getTheme (theme string ) GlamourTheme {
176
- style , ok := styles .DefaultStyles [theme ]
182
+ style , ok := glamourStyles .DefaultStyles [theme ]
177
183
if ! ok {
178
184
jsonBytes , err := os .ReadFile (theme )
179
185
if err != nil {
180
- return GlamourTheme {Style : styles .DarkStyleConfig , Name : "dark" }
186
+ return GlamourTheme {Style : glamourStyles .DarkStyleConfig , Name : "dark" }
181
187
}
182
188
183
189
var customStyle ansi.StyleConfig
184
190
if err := json .Unmarshal (jsonBytes , & customStyle ); err != nil {
185
- return GlamourTheme {Style : styles .DarkStyleConfig , Name : "dark" }
191
+ return GlamourTheme {Style : glamourStyles .DarkStyleConfig , Name : "dark" }
186
192
}
187
193
188
194
return GlamourTheme {Style : customStyle , Name : theme }
@@ -269,3 +275,102 @@ func NewProperties(properties string) (Properties, error) {
269
275
270
276
return p , nil
271
277
}
278
+
279
+ func ChromaStyle (style ansi.StylePrimitive ) string {
280
+ var s string
281
+
282
+ if style .Color != nil {
283
+ s = * style .Color
284
+ }
285
+ if style .BackgroundColor != nil {
286
+ if s != "" {
287
+ s += " "
288
+ }
289
+ s += "bg:" + * style .BackgroundColor
290
+ }
291
+ if style .Italic != nil && * style .Italic {
292
+ if s != "" {
293
+ s += " "
294
+ }
295
+ s += "italic"
296
+ }
297
+ if style .Bold != nil && * style .Bold {
298
+ if s != "" {
299
+ s += " "
300
+ }
301
+ s += "bold"
302
+ }
303
+ if style .Underline != nil && * style .Underline {
304
+ if s != "" {
305
+ s += " "
306
+ }
307
+ s += "underline"
308
+ }
309
+
310
+ return s
311
+ }
312
+
313
+ func GetChromaStyle (themeName string ) * chroma.Style {
314
+ customThemeName := chromaStyleTheme + "-" + themeName
315
+
316
+ chromaMutex .Lock ()
317
+ defer chromaMutex .Unlock ()
318
+
319
+ if style , ok := chromaStyles .Registry [customThemeName ]; ok {
320
+ return style
321
+ }
322
+
323
+ styleConfig := getTheme (themeName )
324
+ style := styleConfig .Style
325
+ if style .CodeBlock .Chroma != nil {
326
+ style := chroma .MustNewStyle (customThemeName ,
327
+ chroma.StyleEntries {
328
+ chroma .Text : ChromaStyle (style .CodeBlock .Chroma .Text ),
329
+ chroma .Error : ChromaStyle (style .CodeBlock .Chroma .Error ),
330
+ chroma .Comment : ChromaStyle (style .CodeBlock .Chroma .Comment ),
331
+ chroma .CommentPreproc : ChromaStyle (style .CodeBlock .Chroma .CommentPreproc ),
332
+ chroma .Keyword : ChromaStyle (style .CodeBlock .Chroma .Keyword ),
333
+ chroma .KeywordReserved : ChromaStyle (style .CodeBlock .Chroma .KeywordReserved ),
334
+ chroma .KeywordNamespace : ChromaStyle (style .CodeBlock .Chroma .KeywordNamespace ),
335
+ chroma .KeywordType : ChromaStyle (style .CodeBlock .Chroma .KeywordType ),
336
+ chroma .Operator : ChromaStyle (style .CodeBlock .Chroma .Operator ),
337
+ chroma .Punctuation : ChromaStyle (style .CodeBlock .Chroma .Punctuation ),
338
+ chroma .Name : ChromaStyle (style .CodeBlock .Chroma .Name ),
339
+ chroma .NameBuiltin : ChromaStyle (style .CodeBlock .Chroma .NameBuiltin ),
340
+ chroma .NameTag : ChromaStyle (style .CodeBlock .Chroma .NameTag ),
341
+ chroma .NameAttribute : ChromaStyle (style .CodeBlock .Chroma .NameAttribute ),
342
+ chroma .NameClass : ChromaStyle (style .CodeBlock .Chroma .NameClass ),
343
+ chroma .NameConstant : ChromaStyle (style .CodeBlock .Chroma .NameConstant ),
344
+ chroma .NameDecorator : ChromaStyle (style .CodeBlock .Chroma .NameDecorator ),
345
+ chroma .NameException : ChromaStyle (style .CodeBlock .Chroma .NameException ),
346
+ chroma .NameFunction : ChromaStyle (style .CodeBlock .Chroma .NameFunction ),
347
+ chroma .NameOther : ChromaStyle (style .CodeBlock .Chroma .NameOther ),
348
+ chroma .Literal : ChromaStyle (style .CodeBlock .Chroma .Literal ),
349
+ chroma .LiteralNumber : ChromaStyle (style .CodeBlock .Chroma .LiteralNumber ),
350
+ chroma .LiteralDate : ChromaStyle (style .CodeBlock .Chroma .LiteralDate ),
351
+ chroma .LiteralString : ChromaStyle (style .CodeBlock .Chroma .LiteralString ),
352
+ chroma .LiteralStringEscape : ChromaStyle (style .CodeBlock .Chroma .LiteralStringEscape ),
353
+ chroma .GenericDeleted : ChromaStyle (style .CodeBlock .Chroma .GenericDeleted ),
354
+ chroma .GenericEmph : ChromaStyle (style .CodeBlock .Chroma .GenericEmph ),
355
+ chroma .GenericInserted : ChromaStyle (style .CodeBlock .Chroma .GenericInserted ),
356
+ chroma .GenericStrong : ChromaStyle (style .CodeBlock .Chroma .GenericStrong ),
357
+ chroma .GenericSubheading : ChromaStyle (style .CodeBlock .Chroma .GenericSubheading ),
358
+ chroma .Background : ChromaStyle (style .CodeBlock .Chroma .Background ),
359
+ })
360
+ chromaStyles .Register (style )
361
+ return style
362
+ }
363
+
364
+ switch themeName {
365
+ case "dracula" :
366
+ return chromaStyles .Get ("dracula" )
367
+ case "dark" :
368
+ return chromaStyles .Get ("github-dark" )
369
+ case "light" :
370
+ return chromaStyles .Get ("github" )
371
+ case "tokyo-night" , "tokyonight" :
372
+ return chromaStyles .Get ("tokyo-night" )
373
+ default :
374
+ return chromaStyles .Fallback
375
+ }
376
+ }
0 commit comments