-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig.go
243 lines (212 loc) · 5.01 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"fmt"
"os"
"path/filepath"
yaml "gopkg.in/yaml.v3"
)
type Layout struct {
SidebarWidth int `yaml:"sidebar_width"`
Padding struct {
Horizontal int `yaml:"horizontal"`
Vertical int `yaml:"vertical"`
} `yaml:"padding"`
Heights struct {
Header int `yaml:"header"`
Footer int `yaml:"footer"`
Status int `yaml:"status"`
Help int `yaml:"help"`
} `yaml:"heights"`
HeaderGap int `yaml:"header_gap"`
}
type Config struct {
ConfigDir string `yaml:"config_dir"`
NotesDir string `yaml:"notes_dir"`
ArchiveDir string `yaml:"archive_dir"`
Editor string `yaml:"editor"`
Layout Layout `yaml:"layout"`
Theme struct {
Light string `yaml:"light"`
Dark string `yaml:"dark"`
} `yaml:"theme"`
}
type Dimensions struct {
Heights struct {
Header int
Footer int
Status int
}
Spacing struct {
HeaderGap int
}
}
func getConfigDir() (string, error) {
return filepath.Join(getConfigHome(), "note"), nil
}
func getConfigHome() string {
if xdgConfig := os.Getenv("XDG_CONFIG_HOME"); xdgConfig != "" && filepath.IsAbs(xdgConfig) {
return xdgConfig
}
homeDir, _ := os.UserHomeDir()
return filepath.Join(homeDir, ".config")
}
func getDataHome() string {
if xdgData := os.Getenv("XDG_DATA_HOME"); xdgData != "" && filepath.IsAbs(xdgData) {
return xdgData
}
homeDir, _ := os.UserHomeDir()
return filepath.Join(homeDir, ".local", "share")
}
func DefaultConfig() *Config {
configDir, _ := getConfigDir()
return &Config{
ConfigDir: configDir,
NotesDir: filepath.Join(getDataHome(), "note"),
ArchiveDir: filepath.Join(getDataHome(), "note", "archive"),
Editor: "",
Layout: Layout{
SidebarWidth: 30,
Padding: struct {
Horizontal int `yaml:"horizontal"`
Vertical int `yaml:"vertical"`
}{
Horizontal: 2,
Vertical: 1,
},
Heights: struct {
Header int `yaml:"header"`
Footer int `yaml:"footer"`
Status int `yaml:"status"`
Help int `yaml:"help"`
}{
Header: 1,
Footer: 1,
Status: 1,
Help: 1,
},
HeaderGap: 1,
},
Theme: struct {
Light string `yaml:"light"`
Dark string `yaml:"dark"`
}{
Light: "default",
Dark: "default",
},
}
}
func LoadConfig() (*Config, error) {
cfg := DefaultConfig()
configDir := cfg.ConfigDir
// Create directories if they don't exist
dirs := []string{
configDir,
cfg.NotesDir,
cfg.ArchiveDir,
}
for _, dir := range dirs {
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, fmt.Errorf("failed to create directory %s: %v", dir, err)
}
}
configPath := filepath.Join(configDir, "config.yaml")
// Check if config file exists
if _, err := os.Stat(configPath); os.IsNotExist(err) {
// Set the editor before creating the config file
cfg.Editor = cfg.GetEditor()
// Create default config file
data, err := yaml.Marshal(cfg)
if err != nil {
return nil, fmt.Errorf("failed to marshal default config: %v", err)
}
if err := os.WriteFile(configPath, data, 0644); err != nil {
return nil, fmt.Errorf("failed to write default config: %v", err)
}
return cfg, nil
}
// Read existing config
data, err := os.ReadFile(configPath)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(data, cfg); err != nil {
return nil, err
}
return cfg, nil
}
func SaveConfig(cfg *Config) error {
data, err := yaml.Marshal(cfg)
if err != nil {
return err
}
configPath := filepath.Join(cfg.ConfigDir, "config.yaml")
return os.WriteFile(configPath, data, 0644)
}
func (c *Config) CalculateHeights(totalHeight int) struct {
Content int
Header int
Footer int
} {
header := c.Layout.Heights.Header
status := c.Layout.Heights.Status
help := c.Layout.Heights.Help
footer := status + help
content := totalHeight - header - footer - 2 - c.Layout.HeaderGap // 2 is border size
if content < 0 {
content = 0
}
return struct {
Content int
Header int
Footer int
}{
Content: content,
Header: header,
Footer: footer,
}
}
func (c *Config) GetPadding() (horizontal, vertical int) {
return c.Layout.Padding.Horizontal, c.Layout.Padding.Vertical
}
func (c *Config) DefaultDimensions() Dimensions {
return Dimensions{
Heights: struct {
Header int
Footer int
Status int
}{
Header: c.Layout.Heights.Header,
Footer: c.Layout.Heights.Footer,
Status: c.Layout.Heights.Status,
},
Spacing: struct {
HeaderGap int
}{
HeaderGap: c.Layout.HeaderGap,
},
}
}
func (c *Config) GetEditor() string {
// 1. Check config file setting
if c.Editor != "" {
return c.Editor
}
// 2. Check NOTE_EDITOR environment variable
if noteEditor := os.Getenv("NOTE_EDITOR"); noteEditor != "" {
return noteEditor
}
// 3. Check VISUAL environment variable
if visual := os.Getenv("VISUAL"); visual != "" {
return visual
}
// 4. Check EDITOR environment variable
if editor := os.Getenv("EDITOR"); editor != "" {
return editor
}
// 5. Check if /usr/bin/vi exists
if _, err := os.Stat("/usr/bin/vi"); err == nil {
return "/usr/bin/vi"
}
// 6. Final fallback to ed
return "/bin/ed"
}