-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
164 lines (142 loc) · 4.29 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"flag"
"io/ioutil"
"os"
"path"
"strings"
"gopkg.in/yaml.v3"
)
var (
configLocation = flag.String("config", "", "config file")
)
// config type definitions {{{
type Module struct {
Name string `json:"name" yaml:"Name"`
Text string `json:"full_text" yaml:"-"`
ForegroundColor string `json:"color" yaml:"ForegroundColor"`
BackgroundColor string `json:"background" yaml:"BackgroundColor"`
Pre string `json:"-" yaml:"Pre"`
Post string `json:"-" yaml:"Post"`
Command string `json:"-" yaml:"Command"`
Args []string `json:"-" yaml:"Args"`
Interval int `json:"-" yaml:"Interval"`
Markup string `json:"markup" yaml:"Markup"`
Separator bool `json:"separator" yaml:"-"`
SeparatorWidth int `json:"separator_block_width" yaml:"-"`
Signal int `json:"-" yaml:"Signal"`
}
type ConfigOptions struct {
PowerlineTheme bool `yaml:"PowerlineTheme"`
PowerlineSeparator string `yaml:"PowerlineSeparator"`
}
type Config struct {
Modules []Module `yaml:"Modules"`
Colors map[string]string `yaml:"Colors"`
Options ConfigOptions `yaml:"Options"`
}
// }}}
func DefaultConfig(msg string) Config {
config := Config{}
config.Modules = []Module{
{"msg", "", "#0000ff", "#ffff00", " ", "",
"*echo", []string{msg}, 60, "none", true, 9, 0},
{"time", "", "#ffffff", "#000000", " ", "",
"*time", []string{}, 1, "none", true, 9, 0},
{"kernel", "", "#880088", "#ccccee", " ", "",
"uname", []string{"-r"}, 60, "none", true, 9, 0}}
return config
}
func LoadColors() map[string]string {
// default (gruvbox)
colors := map[string]string{
"BLACK": "#282828",
"BLUE": "#458588",
"CYAN": "#689d6a",
"DARK_GREY": "#6f6357",
"GREEN": "#98971a",
"LIGHT_GREY": "#a89984",
"MAGENTA": "#b16286",
"RED": "#cc241d",
"WHITE": "#ebdbb2",
"YELLOW": "#d79921"}
// load from env
for _, v := range os.Environ() {
keyValue := strings.Split(v, "=")
if len(keyValue[0]) < 7 {
continue
}
if keyValue[0][:6] == "COLOR_" {
colors[keyValue[0][6:]] = keyValue[1]
}
}
return colors
}
func LoadConfig() Config {
flag.Parse()
var configFile string
// return default config in certain cases {{{
if *configLocation != "" {
configFile = *configLocation
} else {
configDir, err := os.UserConfigDir()
if err != nil {
return DefaultConfig("cannot get config dir")
}
configFile = path.Join(configDir, "i3gocks", "config.yml")
}
_, err := os.Stat(configFile)
if err != nil {
if os.IsNotExist(err) {
return DefaultConfig("i3gocks (default config)")
}
return DefaultConfig("cannot stat config")
}
data, err := ioutil.ReadFile(configFile)
if err != nil {
return DefaultConfig("error reading config")
}
config := Config{}
config.Colors = LoadColors()
err = yaml.Unmarshal(data, &config)
if err != nil {
return DefaultConfig("error parsing config")
}
// }}}
// default powerline separator
if config.Options.PowerlineTheme && config.Options.PowerlineSeparator == "" {
config.Options.PowerlineSeparator = "\uE0B2"
}
// default values and initiallizing {{{
for i := 0; i < len(config.Modules); i++ {
// default interval
if config.Modules[i].Interval == 0 {
config.Modules[i].Interval = 1
}
// default foreground color
if config.Modules[i].ForegroundColor == "" {
config.Modules[i].ForegroundColor = config.Colors["WHITE"]
}
// default background color
if config.Modules[i].BackgroundColor == "" {
config.Modules[i].BackgroundColor = config.Colors["BLACK"]
}
// enable pango in powerline theme / separator in non-powerline
if config.Options.PowerlineTheme {
config.Modules[i].Markup = "pango"
} else {
config.Modules[i].Separator = true
config.Modules[i].SeparatorWidth = 9
}
// resolve foreground color reference
if config.Modules[i].ForegroundColor[0] == '*' {
config.Modules[i].ForegroundColor = config.Colors[strings.ToUpper(config.Modules[i].ForegroundColor[1:])]
}
// resolve background color reference
if config.Modules[i].BackgroundColor != "" && config.Modules[i].BackgroundColor[0] == '*' {
config.Modules[i].BackgroundColor = config.Colors[strings.ToUpper(config.Modules[i].BackgroundColor[1:])]
}
}
// }}}
return config
}