-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
135 lines (110 loc) · 2.26 KB
/
config.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
package main
import (
"io/ioutil"
"os"
"path"
"gopkg.in/yaml.v2"
)
type ProjektorConfig struct {
KeyBind string
ForceCacheInterval int64
EnabledCategories struct {
Calc bool
History bool
Apps bool
URL bool
Commands bool
Files bool
WebSearch bool
}
History struct {
Capacity int
}
URL struct {
Icon string
}
WebSearch struct {
Engine string
Icon string
}
UI struct {
Position string
Width string
Height string
}
}
var (
ConfigFilePath = path.Join(AppDir, "config.yaml")
Config *ProjektorConfig = MustLoadConfig()
)
func DefaultConfig() *ProjektorConfig {
c := &ProjektorConfig{}
c.KeyBind = "Mod4-q"
c.ForceCacheInterval = 60
c.EnabledCategories.Calc = true
c.EnabledCategories.History = true
c.EnabledCategories.Apps = true
c.EnabledCategories.URL = true
c.EnabledCategories.Commands = true
c.EnabledCategories.Files = true
c.EnabledCategories.WebSearch = true
c.History.Capacity = 40
c.URL.Icon = "web-browser"
c.WebSearch.Engine = "https://google.com/search?q=%s"
c.WebSearch.Icon = "web-browser"
c.UI.Position = "center"
c.UI.Width = "400"
c.UI.Height = "480"
return c
}
func SaveConfig(cfg *ProjektorConfig) error {
err := os.MkdirAll(AppDir, 0700)
if err != nil {
return err
}
f, err := os.Create(ConfigFilePath)
if err != nil {
return err
}
defer f.Close()
buf, err := yaml.Marshal(cfg)
if err != nil {
return err
}
_, err = f.Write(buf)
if err != nil {
return err
}
return nil
}
func OpenConfig() (*ProjektorConfig, error) {
f, err := os.Open(ConfigFilePath)
if err != nil {
return nil, err
}
defer f.Close()
contents, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
config := DefaultConfig()
err = yaml.Unmarshal(contents, config)
if err != nil {
return nil, err
}
return config, nil
}
func MustLoadConfig() *ProjektorConfig {
var config *ProjektorConfig
var err error
config, err = OpenConfig()
if err != nil {
errduring("opening config file at %q", err, "Attempting to create one", ConfigFilePath)
config = DefaultConfig()
}
if err := SaveConfig(config); err != nil {
errduring("creating config file at %q", err, "Using default options", ConfigFilePath)
return config
}
return config
}