-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththemes.go
64 lines (51 loc) · 1.36 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
package main
import (
"embed"
"fmt"
"gopkg.in/yaml.v3"
)
const defaultTheme = "standard"
// Load loads themes/${theme}.yml and returns a map of strings for
// converted themes.
func loadTheme(allThemes embed.FS, theme string) (map[string]string, error) {
if theme == "" {
theme = defaultTheme
}
t := make(map[string]string)
file := fmt.Sprintf("themes/%v.yml", theme)
b, err := allThemes.ReadFile(file)
if err != nil {
return t, fmt.Errorf("failed to load file %v: %w", file, err)
}
err = yaml.Unmarshal(b, &t)
if err != nil {
return t, fmt.Errorf("failed to unmarshal file %v: %w", file, err)
}
return t, nil
}
// Load loads themes/${theme}.yml and returns a map of strings of colors. As a
// fallback, it loads the defaultTheme, so that strings that are not set to a
// color will still show visible text in some sense.
func loadThemes(allThemes embed.FS, theme string) (map[string]string, error) {
t, err := loadTheme(allThemes, defaultTheme)
if err != nil {
return t, fmt.Errorf("failed to load default themes %v: %w", defaultTheme, err)
}
switch theme {
case "":
fallthrough
case defaultTheme:
return t, nil
default:
break
}
u, err := loadTheme(allThemes, theme)
if err != nil {
return t, fmt.Errorf("failed to load specified themes %v: %w", theme, err)
}
// merge the two maps
for k, v := range u {
t[k] = v
}
return t, nil
}