-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
105 lines (82 loc) · 1.93 KB
/
util.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/charmbracelet/bubbles/list"
"github.com/pelletier/go-toml/v2"
)
var configPath string
func getThemes() ([]list.Item, error) {
items := []list.Item{}
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Println("error:", err)
return nil, err
}
folderPath := filepath.Join(homeDir, ".config", "alacritty", "themes")
files, err := os.ReadDir(folderPath)
if err != nil {
println(err.Error())
return nil, err
}
for _, file := range files {
if file.IsDir() {
continue
}
fileName := file.Name()
fileName = strings.TrimSuffix(fileName, filepath.Ext(fileName))
absolutePath := filepath.Join(folderPath, file.Name())
items = append(items, theme{name: fileName, location: absolutePath})
}
return items, nil
}
func LoadConfig() (map[string]interface{}, error) {
var cfg map[string]interface{}
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Println("error: ", err)
return nil, err
}
configPath = filepath.Join(homeDir, ".config", "alacritty", "alacritty.toml")
content, err := os.ReadFile(configPath)
if err != nil {
fmt.Println("error: ", err)
return nil, err
}
err = toml.Unmarshal(content, &cfg)
if err != nil {
log.Panic(err)
}
_, ok := cfg["import"]
if !ok {
cfg["import"] = []interface{}{""}
}
return cfg, nil
}
func saveConfig(config []byte) error {
err := os.WriteFile(configPath, config, 0644)
return err
}
func applyTheme(th theme) {
config, err := LoadConfig()
if err != nil {
log.Panic(err)
}
import_array, ok := config["import"].([]interface{})
if ok {
data := fmt.Sprintf("~/.config/alacritty/themes/%s.toml", th.name)
import_array = import_array[:len(import_array)-1]
import_array = append(import_array, data)
config["import"] = import_array
} else {
log.Fatalf("couldnt parse config file")
}
b, err := toml.Marshal(config)
if err != nil {
log.Panic(err)
}
saveConfig(b)
}