-
Notifications
You must be signed in to change notification settings - Fork 80
/
termthemes.go
83 lines (69 loc) · 2.14 KB
/
termthemes.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
package configstore
import (
"encoding/json"
"errors"
"log"
"os"
"path"
"path/filepath"
"github.com/fsnotify/fsnotify"
"github.com/wavetermdev/waveterm/wavesrv/pkg/scbase"
)
const ConfigReturnTypeStr = "termthemes"
const configDir = "config/terminal-themes/"
var configDirAbsPath = path.Join(scbase.GetWaveHomeDir(), configDir)
type ConfigReturn map[string]map[string]string
func (tt ConfigReturn) GetType() string {
return ConfigReturnTypeStr
}
func getNameAndPath(event fsnotify.Event) (string, string) {
filePath := event.Name
fileName := filepath.Base(filePath)
// Normalize the file path for consistency across platforms
normalizedPath := filepath.ToSlash(filePath)
return fileName, normalizedPath
}
// readFileContents reads and unmarshals the JSON content from a file.
func readFileContents(filePath string) (map[string]string, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
var content map[string]string
if err := json.Unmarshal(data, &content); err != nil {
return nil, err
}
return content, nil
}
// ScanConfigs reads all JSON files in the specified directory and its subdirectories.
func ScanConfigs() (ConfigReturn, error) {
config := make(ConfigReturn)
if _, err := os.Stat(configDirAbsPath); errors.Is(err, os.ErrNotExist) {
log.Printf("directory does not exist: %s", configDirAbsPath)
return ConfigReturn{}, nil
}
err := filepath.Walk(configDirAbsPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && filepath.Ext(info.Name()) == ".json" {
content, err := readFileContents(path)
if err != nil {
log.Printf("error reading file %s: %v", path, err)
return nil // continue walking despite error in reading file
}
// Use the relative path from the directory as the key to store themes
relPath, err := filepath.Rel(configDirAbsPath, path)
if err != nil {
log.Printf("error getting relative file path %s: %v", path, err)
return nil // continue walking despite error in getting relative path
}
config[relPath] = content
}
return nil
})
if err != nil {
return nil, err
}
return config, nil
}