-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththemes.go
More file actions
62 lines (53 loc) · 1.45 KB
/
themes.go
File metadata and controls
62 lines (53 loc) · 1.45 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
package main
import (
"encoding/json"
"os"
"path/filepath"
"sort"
)
type Theme struct {
Name string `json:"name"`
Description string `json:"description"`
Bg string `json:"bg"`
Text string `json:"text"`
GradientColor string `json:"gradient_color"`
Water string `json:"water"`
Parks string `json:"parks"`
RoadMotorway string `json:"road_motorway"`
RoadPrimary string `json:"road_primary"`
RoadSecondary string `json:"road_secondary"`
RoadTertiary string `json:"road_tertiary"`
RoadResidential string `json:"road_residential"`
RoadDefault string `json:"road_default"`
}
const themesDir = "themes"
func getAvailableThemes() ([]string, error) {
files, err := os.ReadDir(themesDir)
if err != nil {
return nil, err
}
var themes []string
for _, file := range files {
if !file.IsDir() && filepath.Ext(file.Name()) == ".json" {
themes = append(themes, file.Name()[:len(file.Name())-5])
}
}
sort.Strings(themes)
return themes, nil
}
func loadTheme(themeName string) (*Theme, error) {
themeFile := filepath.Join(themesDir, themeName+".json")
if _, err := os.Stat(themeFile); os.IsNotExist(err) {
// Fallback to terracotta
themeFile = filepath.Join(themesDir, "terracotta.json")
}
data, err := os.ReadFile(themeFile)
if err != nil {
return nil, err
}
var theme Theme
if err := json.Unmarshal(data, &theme); err != nil {
return nil, err
}
return &theme, nil
}