-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
166 lines (145 loc) Β· 4.5 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
165
166
/**
* This file is part of tz.
*
* tz is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* tz is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with tz. If not, see <https://www.gnu.org/licenses/>.
**/
package main
import (
"fmt"
"slices"
"strings"
)
// Keymaps represents the key mappings in the TOML file
type Keymaps struct {
PrevHour []string
NextHour []string
PrevDay []string
NextDay []string
PrevWeek []string
NextWeek []string
ToggleDate []string
OpenWeb []string
Now []string
Help []string
Quit []string
}
// Config stores app configuration
type Config struct {
Zones []*Zone
Keymaps Keymaps
}
// Function to provide default values for the Config struct
var DefaultKeymaps = Keymaps{
PrevHour: []string{"h", "left"},
NextHour: []string{"l", "right"},
PrevDay: []string{"H", "shift+left", "pgup", "shift+up", "ctrl+u"},
NextDay: []string{"L", "shift+right", "pgdown", "shift+down", "ctrl+d"},
PrevWeek: []string{"p", "ctrl+left", "shift+pgup", "ctrl+b"},
NextWeek: []string{"n", "ctrl+right", "shift+pgdown", "ctrl+f"},
ToggleDate: []string{"d"},
OpenWeb: []string{"o"},
Now: []string{"t"},
Help: []string{"?"},
Quit: []string{"q", "ctrl+c", "esc"},
}
func LoadConfig(tzConfigs []string) (*Config, error) {
// Apply config file first
fileConfig, fileError := LoadConfigFile()
if fileError != nil {
return nil, fmt.Errorf("File error: %w", fileError)
}
// Override with env var config
envConfig, envErr := LoadConfigEnv(tzConfigs)
if envErr != nil {
return nil, fmt.Errorf("Env error: %w", envErr)
}
// Merge configs, with envConfig taking precedence
mergedConfig := Config{
Zones: []*Zone{DefaultZones[0]},
Keymaps: DefaultKeymaps,
}
// Merge Zones
var configZones []*Zone
if len(envConfig.Zones) > 0 {
configZones = envConfig.Zones
} else if len(fileConfig.Zones) > 0 {
configZones = fileConfig.Zones
} else {
configZones = DefaultZones[1:]
}
mergedConfig.Zones = append(mergedConfig.Zones, configZones...)
logger.Printf("File zones: %s", fileConfig.Zones)
logger.Printf("Env zones: %s", envConfig.Zones)
logger.Printf("Merged zones: %s", mergedConfig.Zones)
// Merge Keymaps
if len(fileConfig.Keymaps.PrevHour) > 0 {
mergedConfig.Keymaps.PrevHour = fileConfig.Keymaps.PrevHour
}
if len(fileConfig.Keymaps.NextHour) > 0 {
mergedConfig.Keymaps.NextHour = fileConfig.Keymaps.NextHour
}
if len(fileConfig.Keymaps.PrevDay) > 0 {
mergedConfig.Keymaps.PrevDay = fileConfig.Keymaps.PrevDay
}
if len(fileConfig.Keymaps.NextDay) > 0 {
mergedConfig.Keymaps.NextDay = fileConfig.Keymaps.NextDay
}
if len(fileConfig.Keymaps.PrevWeek) > 0 {
mergedConfig.Keymaps.PrevWeek = fileConfig.Keymaps.PrevWeek
}
if len(fileConfig.Keymaps.NextWeek) > 0 {
mergedConfig.Keymaps.NextWeek = fileConfig.Keymaps.NextWeek
}
if len(fileConfig.Keymaps.ToggleDate) > 0 {
mergedConfig.Keymaps.ToggleDate = fileConfig.Keymaps.ToggleDate
}
if len(fileConfig.Keymaps.OpenWeb) > 0 {
mergedConfig.Keymaps.OpenWeb = fileConfig.Keymaps.OpenWeb
}
if len(fileConfig.Keymaps.Now) > 0 {
mergedConfig.Keymaps.Now = fileConfig.Keymaps.Now
}
if len(fileConfig.Keymaps.Quit) > 0 {
mergedConfig.Keymaps.Quit = fileConfig.Keymaps.Quit
}
allKeymaps := [][]string {
mergedConfig.Keymaps.PrevHour,
mergedConfig.Keymaps.NextHour,
mergedConfig.Keymaps.PrevDay,
mergedConfig.Keymaps.NextDay,
mergedConfig.Keymaps.PrevWeek,
mergedConfig.Keymaps.NextWeek,
mergedConfig.Keymaps.ToggleDate,
mergedConfig.Keymaps.OpenWeb,
mergedConfig.Keymaps.Now,
mergedConfig.Keymaps.Help,
mergedConfig.Keymaps.Quit,
}
var keysUsed = make(map[string]bool)
var keysDuplicated []string
for _, keys := range allKeymaps {
for _, key := range keys {
if _, used := keysUsed[key]; used == false {
keysUsed[key] = true
} else {
keysDuplicated = append(keysDuplicated, key)
}
}
}
if len(keysDuplicated) > 0 {
slices.Sort(keysDuplicated)
return nil, fmt.Errorf("Key(s) mapped multiple times in config: %v", strings.Join(keysDuplicated, " "))
}
return &mergedConfig, nil
}