Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com>
This commit is contained in:
Hadi
2026-05-26 19:59:34 +02:00
commit f3dce1f4ab
29 changed files with 2218 additions and 0 deletions
+70
View File
@@ -0,0 +1,70 @@
package config
import (
_ "embed"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)
//go:embed default_config.yaml
var defaultConfig []byte
type Config struct {
Keybindings Keybindings `mapstructure:"keybindings"`
}
var Global *Config
func Load(path string) error {
var defaults map[string]any
if err := yaml.Unmarshal(defaultConfig, &defaults); err != nil {
return fmt.Errorf("default config: %w", err)
}
for k, v := range flatten("", defaults) {
viper.SetDefault(k, v)
}
viper.SetConfigType("yaml")
viper.SetConfigFile(path)
if err := viper.ReadInConfig(); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
}
Global = &Config{}
return viper.Unmarshal(Global)
}
func WriteDefaultConfig(path string) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return fmt.Errorf("create config dir: %w", err)
}
if err := os.WriteFile(path, defaultConfig, 0o600); err != nil {
return fmt.Errorf("write config: %w", err)
}
return nil
}
func flatten(prefix string, m map[string]any) map[string]any {
out := make(map[string]any)
for k, v := range m {
key := k
if prefix != "" {
key = prefix + "." + k
}
if nested, ok := v.(map[string]any); ok {
for nk, nv := range flatten(key, nested) {
out[nk] = nv
}
} else {
out[key] = v
}
}
return out
}
+11
View File
@@ -0,0 +1,11 @@
keybindings:
quit: "ctrl+c,q"
cycle_focus: "tab"
edit: "e,enter"
edit_external: "E"
docs: "d"
help_toggle: "?"
clear: "x"
reset: "r"
copy: "y,ctrl+shift+c"
paste: "p,ctrl+shift+v"
+14
View File
@@ -0,0 +1,14 @@
package config
type Keybindings struct {
Quit string `mapstructure:"quit"`
CycleFocus string `mapstructure:"cycle_focus"`
Edit string `mapstructure:"edit"`
EditExternal string `mapstructure:"edit_external"`
Docs string `mapstructure:"docs"`
HelpToggle string `mapstructure:"help_toggle"`
Clear string `mapstructure:"clear"`
Reset string `mapstructure:"reset"`
Copy string `mapstructure:"copy"`
Paste string `mapstructure:"paste"`
}