-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
142 lines (108 loc) · 2.66 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
136
137
138
139
140
141
142
package main
import (
"github.com/BurntSushi/wingo/wini"
"github.com/AmandaCameron/gobar/utils"
)
type Config struct {
CommandAccel string
BarSize int
BarWidth int
Position string
ClockFormat string
Clock Section
StatusBar Section
Command Section
Tracker Section
}
type Section struct {
Width int
Position int
Font FontInfo
}
type FontInfo struct {
Name string
Size float64
}
func loadConfig(fileName string) *Config {
dat, err := wini.Parse(fileName)
utils.FailMeMaybe(err)
cfg := &Config{}
// Main thing.
loadInt(dat, "Main", "Size", &cfg.BarSize)
loadInt(dat, "Main", "Width", &cfg.BarWidth)
loadString(dat, "Main", "Position", "", &cfg.Position)
// Clock.
loadSection(dat, "Clock", true, &cfg.Clock)
loadString(dat, "Clock", "Format", "2006-01-02 15:04:05", &cfg.ClockFormat)
// Tracker
loadSection(dat, "App Tracker", false, &cfg.Tracker)
// Command Tray
loadSection(dat, "CommandTray", true, &cfg.Command)
loadString(dat, "CommandTray", "Accel", "", &cfg.CommandAccel)
// Status Bar
loadSection(dat, "StatusBar", false, &cfg.StatusBar)
return cfg
}
func loadSection(dat *wini.Data, name string, fontMeMaybe bool, target *Section) {
sec := Section{}
loadInt(dat, name, "Width", &sec.Width)
loadInt(dat, name, "Position", &sec.Position)
if fontMeMaybe {
loadFont(dat, name, &sec.Font)
}
*target = sec
}
func loadFont(dat *wini.Data, name string, target *FontInfo) {
font := FontInfo{}
loadString(dat, "Fonts/"+name, "Name", "", &font.Name)
loadFloat(dat, "Fonts/"+name, "Size", &font.Size)
*target = font
}
func loadString(dat *wini.Data, name, key, def string, target *string) {
k := dat.GetKey(name, key)
if k == nil {
if def == "" {
utils.Fail("Missing key " + key + " in section " + name)
} else {
*target = def
return
}
}
tmp := k.Strings()
if len(tmp) > 0 {
*target = tmp[0]
} else {
if def == "" {
utils.Fail("Missing key " + key + " in section " + name)
} else {
*target = def
return
}
}
}
func loadInt(dat *wini.Data, name, key string, target *int) {
k := dat.GetKey(name, key)
if k == nil {
utils.Fail("Missing key " + key + " in section " + name)
}
tmp, err := k.Ints()
utils.FailMeMaybe(err)
if len(tmp) > 0 {
*target = tmp[0]
} else {
utils.Fail("Missing key " + key + " in section " + name)
}
}
func loadFloat(dat *wini.Data, name, key string, target *float64) {
k := dat.GetKey(name, key)
if k == nil {
utils.Fail("Missing key " + key + " in section " + name)
}
tmp, err := k.Floats()
utils.FailMeMaybe(err)
if len(tmp) > 0 {
*target = tmp[0]
} else {
utils.Fail("Missing key " + key + " in section " + name)
}
}