Repository navigation
Expand file tree
/
Copy paththeme.go
More file actions
72 lines (66 loc) · 1.57 KB
/
Copy paththeme.go
File metadata and controls
72 lines (66 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package golor
import (
"github.com/fatih/color"
"strings"
)
type Theme struct {
// Could be interesting to have primary/secondary...and theme
// Dracula...
}
func (theme *Theme) Produce(tokens []Token) string {
var rendered []string
for _, token := range tokens {
rendered = append(rendered, theme.Convert(token))
}
return strings.Join(rendered, "")
}
func (theme *Theme) Color(c *Color) (color.Attribute, bool) {
if c == nil {
return 0, false
}
switch c.value {
case Black:
return color.FgBlack, true
case Red:
return color.FgRed, true
case Green:
return color.FgGreen, true
case Yellow:
return color.FgYellow, true
case Blue:
return color.FgBlue, true
case Magenta:
return color.FgMagenta, true
case Cyan:
return color.FgCyan, true
case White:
return color.FgWhite, true
default:
return 0, false // Return false for unsupported color indices
}
}
func (theme *Theme) Typographies(typographies []Typography) []color.Attribute {
var attributes []color.Attribute
for _, t := range typographies {
switch t.value {
case Bold:
attributes = append(attributes, color.Bold)
case Italic:
attributes = append(attributes, color.Italic)
}
}
return attributes
}
func (theme *Theme) Convert(token Token) string {
// Use fatih/color
if token.Style == nil {
return token.Text
}
var attributes []color.Attribute
if c, ok := theme.Color(token.Style.color); ok {
attributes = append(attributes, c)
}
attributes = append(attributes, theme.Typographies(token.Style.typographies)...)
f := color.New(attributes...).SprintFunc()
return f(token.Text)
}