-
Notifications
You must be signed in to change notification settings - Fork 255
/
themes.go
204 lines (182 loc) · 5.35 KB
/
themes.go
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Package main theme.go contains the information about a terminal theme.
// It stores the 16 base colors as well as the background and foreground colors
// of the terminal theme.
//
// It can be changed through the Set command.
//
// Set Theme {"background": "#171717"}
// Set Theme "Catppuccin Mocha"
//
//go:generate make all
package main
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/charmbracelet/glamour/ansi"
_ "embed"
"github.com/agnivade/levenshtein"
)
// Theme is a terminal theme for xterm.js
// It is used for marshalling between the xterm.js readable json format and a
// valid go struct.
// https://xtermjs.org/docs/api/terminal/interfaces/itheme/
type Theme struct {
Name string `json:"name"`
Background string `json:"background"`
Foreground string `json:"foreground"`
Selection string `json:"selection"`
Cursor string `json:"cursor"`
CursorAccent string `json:"cursorAccent"`
Black string `json:"black"`
BrightBlack string `json:"brightBlack"`
Red string `json:"red"`
BrightRed string `json:"brightRed"`
Green string `json:"green"`
BrightGreen string `json:"brightGreen"`
Yellow string `json:"yellow"`
BrightYellow string `json:"brightYellow"`
Blue string `json:"blue"`
BrightBlue string `json:"brightBlue"`
Magenta string `json:"magenta"`
BrightMagenta string `json:"brightMagenta"`
Cyan string `json:"cyan"`
BrightCyan string `json:"brightCyan"`
White string `json:"white"`
BrightWhite string `json:"brightWhite"`
}
func (t Theme) String() string {
ts, err := json.Marshal(t)
if err != nil {
dts, _ := json.Marshal(DefaultTheme)
return string(dts)
}
return string(ts)
}
// DefaultTheme is the default theme to use for recording demos and
// screenshots.
//
// Taken from https://github.com/meowgorithm/dotfiles.
var DefaultTheme = Theme{
Background: Background,
Foreground: Foreground,
Cursor: Foreground,
CursorAccent: Background,
Black: Black,
BrightBlack: BrightBlack,
Red: Red,
BrightRed: BrightRed,
Green: Green,
BrightGreen: BrightGreen,
Yellow: Yellow,
BrightYellow: BrightYellow,
Blue: Blue,
BrightBlue: BrightBlue,
Magenta: Magenta,
BrightMagenta: BrightMagenta,
Cyan: Cyan,
BrightCyan: BrightCyan,
White: White,
BrightWhite: BrightWhite,
}
const margin = 2
// GlamourTheme is the theme for printing out the manual page.
// $ vhs man
var GlamourTheme = ansi.StyleConfig{
Document: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
BlockPrefix: "\n",
BlockSuffix: "\n",
},
Margin: uintPtr(margin),
},
Heading: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
BlockSuffix: "\n",
Color: stringPtr("99"),
Bold: boolPtr(true),
},
},
Item: ansi.StylePrimitive{Prefix: "· "},
Emph: ansi.StylePrimitive{Color: stringPtr(BrightBlack)},
Strong: ansi.StylePrimitive{Bold: boolPtr(true)},
Link: ansi.StylePrimitive{Color: stringPtr("42"), Underline: boolPtr(true)},
LinkText: ansi.StylePrimitive{Color: stringPtr("207")},
Code: ansi.StyleBlock{StylePrimitive: ansi.StylePrimitive{Color: stringPtr("204")}},
}
func boolPtr(b bool) *bool { return &b }
func stringPtr(s string) *string { return &s }
func uintPtr(u uint) *uint { return &u }
//go:embed themes.json
var themesBts []byte
// ThemeNotFoundError is returned when a requested theme is not found.
type ThemeNotFoundError struct {
Theme string
Suggestions []string
}
func (e ThemeNotFoundError) Error() string {
if len(e.Suggestions) == 0 {
return fmt.Sprintf("invalid `Set Theme %q`: theme does not exist", e.Theme)
}
return fmt.Sprintf("invalid `Set Theme %q`: did you mean %q",
e.Theme,
strings.Join(e.Suggestions, ", "),
)
}
// sortedThemeNames returns the names of the themes, sorted.
func sortedThemeNames() ([]string, error) {
var keys []string
for _, bts := range [][]byte{themesBts} {
themes, err := parseThemes(bts)
if err != nil {
return nil, err
}
for _, theme := range themes {
keys = append(keys, theme.Name)
}
}
sort.Slice(keys, func(i, j int) bool {
return strings.ToLower(keys[i]) < strings.ToLower(keys[j])
})
return keys, nil
}
const distance = 2
// findTheme return the given theme, if it exists.
func findTheme(name string) (Theme, error) {
for _, bts := range [][]byte{themesBts} {
themes, err := parseThemes(bts)
if err != nil {
return DefaultTheme, err
}
for _, theme := range themes {
if theme.Name == name {
return theme, nil
}
}
}
// not found, lets find similar themes!
keys, err := sortedThemeNames()
if err != nil {
return DefaultTheme, err
}
suggestions := []string{}
lname := strings.ToLower(name)
for _, theme := range keys {
ltheme := strings.ToLower(theme)
levenshteinDistance := levenshtein.ComputeDistance(lname, ltheme)
suggestByLevenshtein := levenshteinDistance <= distance
suggestByPrefix := strings.HasPrefix(lname, ltheme)
if suggestByLevenshtein || suggestByPrefix {
suggestions = append(suggestions, theme)
}
}
return DefaultTheme, ThemeNotFoundError{name, suggestions}
}
func parseThemes(bts []byte) ([]Theme, error) {
var themes []Theme
if err := json.Unmarshal(bts, &themes); err != nil {
return nil, fmt.Errorf("could not load themes.json: %w", err)
}
return themes, nil
}