-
Notifications
You must be signed in to change notification settings - Fork 16
/
theme.go
365 lines (338 loc) · 10.8 KB
/
theme.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package theme
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/glamour/ansi"
"github.com/charmbracelet/lipgloss"
)
// Default is the default colorscheme
var Default = Colors{
BgDark: "#161622",
BgDarker: "#11111a",
Text: "#FFFFFF",
TextDark: "#676985",
Color1: "#c29fec",
Color2: "#ddbec0",
Color3: "#89b4fa",
Color4: "#e06c75",
Color5: "#98c379",
Color6: "#fab387",
Color7: "#f1c1e4",
MarkdownStyle: glamour.DraculaStyleConfig,
}
// Colors is a struct that contains all the colors for the application
type Colors struct {
MarkdownStyle ansi.StyleConfig `json:"-"` // Just generate this at runtime
Color2 lipgloss.Color `json:"color2"`
BgDarker lipgloss.Color `json:"bg_darker"`
Text lipgloss.Color `json:"text"`
TextDark lipgloss.Color `json:"text_dark"`
Color1 lipgloss.Color `json:"color1"`
FilePath string `json:"-"`
Color3 lipgloss.Color `json:"color3"`
Color4 lipgloss.Color `json:"color4"`
Color5 lipgloss.Color `json:"color5"`
Color6 lipgloss.Color `json:"color6"`
Color7 lipgloss.Color `json:"color7"`
BgDark lipgloss.Color `json:"bg_dark"`
}
// New will create a new colorscheme and try to load it
func New(path string) (*Colors, error) {
log.Println("Initializing colorscheme")
if path == "" {
defaultPath, err := getDefaultPath()
if err != nil {
return nil, fmt.Errorf("theme.New: %w", err)
}
path = defaultPath
}
colors := Default
colors.FilePath = path
colors.genMarkdownStyle()
return &colors, nil
}
// Load will load the colorscheme from a JSON file
func (c *Colors) Load() error {
fileContent, err := os.ReadFile(c.FilePath)
if err != nil {
return fmt.Errorf("theme.Load: %w", err)
}
if err = json.Unmarshal(fileContent, c); err != nil {
return fmt.Errorf("theme.Load: %w", err)
}
c.genMarkdownStyle()
return nil
}
// Save saves the colorscheme to a JSON file
func (c Colors) Save() error {
jsonData, err := json.MarshalIndent(c, "", " ")
if err != nil {
return fmt.Errorf("theme.Save: %w", err)
}
if err = os.WriteFile(c.FilePath, jsonData, 0600); err != nil {
if err = os.MkdirAll(filepath.Dir(c.FilePath), 0755); err != nil {
return fmt.Errorf("theme.Save: %w", err)
}
if err = os.WriteFile(c.FilePath, jsonData, 0600); err != nil {
return fmt.Errorf("theme.Save: %w", err)
}
}
return nil
}
// Convert takes the information from a pywal file and converts it to a colorscheme
func (c *Colors) Convert(pywalFilePath string) error {
if pywalFilePath == "" {
cacheDir, err := os.UserCacheDir()
if err != nil {
return fmt.Errorf("theme.Convert: %w", err)
}
pywalFilePath = filepath.Join(cacheDir, "wal", "colors.json")
}
fileContent, err := os.ReadFile(pywalFilePath)
if err != nil {
return fmt.Errorf("theme.Convert: %w", err)
}
var walColorscheme map[string]interface{}
if err = json.Unmarshal(fileContent, &walColorscheme); err != nil {
return fmt.Errorf("theme.Convert: %w", err)
}
// Set the colors
c.BgDark = lipgloss.Color(walColorscheme["special"].(map[string]interface{})["background"].(string))
c.BgDarker = lipgloss.Color(walColorscheme["special"].(map[string]interface{})["background"].(string))
c.Text = lipgloss.Color(walColorscheme["special"].(map[string]interface{})["foreground"].(string))
c.TextDark = lipgloss.Color(walColorscheme["special"].(map[string]interface{})["foreground"].(string))
c.Color1 = lipgloss.Color(walColorscheme["colors"].(map[string]interface{})["color1"].(string))
c.Color2 = lipgloss.Color(walColorscheme["colors"].(map[string]interface{})["color2"].(string))
c.Color3 = lipgloss.Color(walColorscheme["colors"].(map[string]interface{})["color3"].(string))
c.Color4 = lipgloss.Color(walColorscheme["colors"].(map[string]interface{})["color4"].(string))
c.Color5 = lipgloss.Color(walColorscheme["colors"].(map[string]interface{})["color5"].(string))
c.Color6 = lipgloss.Color(walColorscheme["colors"].(map[string]interface{})["color6"].(string))
c.Color7 = lipgloss.Color(walColorscheme["colors"].(map[string]interface{})["color7"].(string))
return nil
}
// PrettyPrint displays the colorscheme in the terminal
func (c Colors) PrettyPrint() string {
result := []string{"A table of all the colors:"}
for _, color := range []lipgloss.Color{c.BgDark, c.BgDarker, c.Text, c.TextDark, c.Color1, c.Color2, c.Color3, c.Color4, c.Color5, c.Color6, c.Color7} {
foreground := lipgloss.NewStyle().Foreground(color)
background := lipgloss.NewStyle().Background(color)
result = append(result, fmt.Sprintf(
"%s %s %s",
foreground.Render("foreground"),
background.Render("background"),
color,
))
}
return strings.Join(result, "\n")
}
// getDefaultPath returns the default path for the colorscheme file
func getDefaultPath() (string, error) {
// Get the default config path
configDir, err := os.UserConfigDir()
if err != nil {
return "", fmt.Errorf("theme.getDefaultPath: %w", err)
}
// Create the config path
return filepath.Join(configDir, "goread", "colorscheme.json"), nil
}
// generateMarkDownStyle generates the markdown style from the colorscheme
func (c *Colors) genMarkdownStyle() {
c.MarkdownStyle = ansi.StyleConfig{
Document: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
BlockPrefix: "\n",
BlockSuffix: "\n",
Color: stringPtr(string(c.Text)),
},
Margin: uintPtr(2),
},
BlockQuote: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Color: stringPtr(string(c.Color7)),
Italic: boolPtr(true),
},
Indent: uintPtr(1),
IndentToken: stringPtr("│ "),
},
List: ansi.StyleList{
LevelIndent: 2,
StyleBlock: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{Color: stringPtr(string(c.Text))},
},
},
Heading: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
BlockSuffix: "\n",
Color: stringPtr(string(c.Color3)),
Bold: boolPtr(true),
},
},
H1: ansi.StyleBlock{StylePrimitive: ansi.StylePrimitive{Prefix: "# "}},
H2: ansi.StyleBlock{StylePrimitive: ansi.StylePrimitive{Prefix: "## "}},
H3: ansi.StyleBlock{StylePrimitive: ansi.StylePrimitive{Prefix: "### "}},
H4: ansi.StyleBlock{StylePrimitive: ansi.StylePrimitive{Prefix: "#### "}},
H5: ansi.StyleBlock{StylePrimitive: ansi.StylePrimitive{Prefix: "##### "}},
H6: ansi.StyleBlock{StylePrimitive: ansi.StylePrimitive{Prefix: "###### "}},
Strikethrough: ansi.StylePrimitive{CrossedOut: boolPtr(true)},
Emph: ansi.StylePrimitive{
Italic: boolPtr(true),
Color: stringPtr(string(c.Color1)),
},
Strong: ansi.StylePrimitive{
Bold: boolPtr(true),
Color: stringPtr(string(c.Color1)),
},
HorizontalRule: ansi.StylePrimitive{
Color: stringPtr(string(c.TextDark)),
Format: "\n--------\n",
},
Item: ansi.StylePrimitive{
BlockPrefix: "• ",
},
Enumeration: ansi.StylePrimitive{
BlockPrefix: ". ",
Color: stringPtr("#8be9fd"),
},
Task: ansi.StyleTask{
StylePrimitive: ansi.StylePrimitive{},
Ticked: "[✓] ",
Unticked: "[ ] ",
},
Link: ansi.StylePrimitive{
Color: stringPtr(string(c.Color6)),
Underline: boolPtr(true),
},
LinkText: ansi.StylePrimitive{
Color: stringPtr(string(c.Color5)),
Bold: boolPtr(true),
},
Image: ansi.StylePrimitive{
Color: stringPtr(string(c.Color6)),
Underline: boolPtr(true),
},
ImageText: ansi.StylePrimitive{
Color: stringPtr(string(c.TextDark)),
Format: "Image: {{.text}} →",
},
Code: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Prefix: " ",
Suffix: " ",
Color: stringPtr(string(c.Text)),
BackgroundColor: stringPtr(string(c.BgDark)),
},
},
CodeBlock: ansi.StyleCodeBlock{
StyleBlock: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Color: stringPtr(string(c.Text)),
},
Margin: uintPtr(2),
},
Chroma: &ansi.Chroma{
Text: ansi.StylePrimitive{
Color: stringPtr(string(c.Text)),
},
Error: ansi.StylePrimitive{
Color: stringPtr(string(c.Color4)),
},
Comment: ansi.StylePrimitive{
Color: stringPtr(string(c.TextDark)),
},
CommentPreproc: ansi.StylePrimitive{
Color: stringPtr(string(c.TextDark)),
},
Keyword: ansi.StylePrimitive{
Color: stringPtr(string(c.Color1)),
},
KeywordReserved: ansi.StylePrimitive{
Color: stringPtr(string(c.Color1)),
},
KeywordNamespace: ansi.StylePrimitive{
Color: stringPtr(string(c.Color1)),
},
KeywordType: ansi.StylePrimitive{
Color: stringPtr(string(c.Color2)),
},
Operator: ansi.StylePrimitive{
Color: stringPtr(string(c.Color3)),
},
Punctuation: ansi.StylePrimitive{
Color: stringPtr(string(c.Text)),
},
Name: ansi.StylePrimitive{
Color: stringPtr(string(c.Text)),
},
NameBuiltin: ansi.StylePrimitive{
Color: stringPtr(string(c.Color6)),
},
NameTag: ansi.StylePrimitive{
Color: stringPtr(string(c.Color6)),
},
NameAttribute: ansi.StylePrimitive{
Color: stringPtr(string(c.Color6)),
},
NameClass: ansi.StylePrimitive{
Color: stringPtr(string(c.Color2)),
},
NameConstant: ansi.StylePrimitive{
Color: stringPtr(string(c.Text)),
},
NameDecorator: ansi.StylePrimitive{
Color: stringPtr(string(c.Color3)),
},
NameFunction: ansi.StylePrimitive{
Color: stringPtr(string(c.Color3)),
},
LiteralNumber: ansi.StylePrimitive{
Color: stringPtr(string(c.Color5)),
},
LiteralString: ansi.StylePrimitive{
Color: stringPtr(string(c.Color5)),
},
LiteralStringEscape: ansi.StylePrimitive{
Color: stringPtr(string(c.Color5)),
},
GenericDeleted: ansi.StylePrimitive{
Color: stringPtr(string(c.Color4)),
},
GenericEmph: ansi.StylePrimitive{
Color: stringPtr(string(c.Color1)),
Italic: boolPtr(true),
},
GenericInserted: ansi.StylePrimitive{
Color: stringPtr(string(c.Color1)),
},
GenericStrong: ansi.StylePrimitive{
Color: stringPtr(string(c.Color1)),
Bold: boolPtr(true),
},
GenericSubheading: ansi.StylePrimitive{
Color: stringPtr(string(c.Color2)),
},
Background: ansi.StylePrimitive{
BackgroundColor: stringPtr(string(c.BgDark)),
},
},
},
Table: ansi.StyleTable{
StyleBlock: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{},
},
CenterSeparator: stringPtr("┼"),
ColumnSeparator: stringPtr("│"),
RowSeparator: stringPtr("─"),
},
DefinitionDescription: ansi.StylePrimitive{
BlockPrefix: "\n🠶 ",
},
}
}
func boolPtr(b bool) *bool { return &b }
func stringPtr(s string) *string { return &s }
func uintPtr(u uint) *uint { return &u }