mirror of
https://github.com/anotherhadi/settuings.git
synced 2026-10-05 16:18:24 +02:00
@@ -0,0 +1,96 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
//go:embed default_config.yaml
|
||||
var defaultConfig []byte
|
||||
|
||||
type Config struct {
|
||||
Version string `mapstructure:"-"`
|
||||
|
||||
App struct {
|
||||
ExternalEditor string `mapstructure:"external_editor"`
|
||||
} `mapstructure:"app"`
|
||||
|
||||
TUI struct {
|
||||
UseNerdfontIcons bool `mapstructure:"use_nerdfont_icons"`
|
||||
DefaultSidebarState string `mapstructure:"default_sidebar_state"`
|
||||
} `mapstructure:"tui"`
|
||||
|
||||
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{}
|
||||
if err := viper.Unmarshal(Global); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func WriteDefaultConfig(path string) error {
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o750); 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 ExpandPath(p string) string {
|
||||
if strings.HasPrefix(p, "~/") {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return p
|
||||
}
|
||||
return filepath.Join(home, p[2:])
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
app:
|
||||
external_editor: "" # override $EDITOR for external editing (e.g. nvim, code --wait)
|
||||
|
||||
tui:
|
||||
use_nerdfont_icons: true
|
||||
default_sidebar_state: "expanded" # hidden, collapsed, expanded
|
||||
|
||||
keybindings:
|
||||
global:
|
||||
quit: "q,ctrl+c"
|
||||
escape: "esc,ctrl+c"
|
||||
help: "?"
|
||||
open_logs: "ctrl+g"
|
||||
toggle_sidebar: "ctrl+b"
|
||||
cycle_focus: "tab"
|
||||
up: "up,k"
|
||||
down: "down,j"
|
||||
left: "left,h"
|
||||
right: "right,l"
|
||||
goto_top: "g"
|
||||
goto_bottom: "G,end"
|
||||
scroll_up: "pgup,ctrl+u"
|
||||
scroll_down: "pgdown,ctrl+d"
|
||||
prev_page: "[,ctrl+i"
|
||||
next_page: "],ctrl+o"
|
||||
|
||||
docs:
|
||||
search: "/"
|
||||
search_reset: "r"
|
||||
search_next: "n"
|
||||
search_prev: "N"
|
||||
|
||||
network:
|
||||
select: "enter"
|
||||
refresh: "r"
|
||||
forget: "d"
|
||||
toggle_auto: "a"
|
||||
reveal_secret: "p"
|
||||
known: "K"
|
||||
info: "i"
|
||||
disconnect: "d"
|
||||
toggle_radio: "w"
|
||||
|
||||
bluetooth:
|
||||
select: "enter"
|
||||
refresh: "r"
|
||||
forget: "d"
|
||||
toggle_trust: "t"
|
||||
info: "i"
|
||||
toggle_power: "w"
|
||||
|
||||
audio:
|
||||
select: "enter"
|
||||
refresh: "r"
|
||||
mute: "m"
|
||||
test: "t"
|
||||
@@ -0,0 +1,63 @@
|
||||
package config
|
||||
|
||||
type GlobalKeys struct {
|
||||
Quit string `mapstructure:"quit"`
|
||||
Escape string `mapstructure:"escape"`
|
||||
OpenLogs string `mapstructure:"open_logs"`
|
||||
ToggleSidebar string `mapstructure:"toggle_sidebar"`
|
||||
Help string `mapstructure:"help"`
|
||||
Up string `mapstructure:"up"`
|
||||
Down string `mapstructure:"down"`
|
||||
Left string `mapstructure:"left"`
|
||||
Right string `mapstructure:"right"`
|
||||
CycleFocus string `mapstructure:"cycle_focus"`
|
||||
ScrollUp string `mapstructure:"scroll_up"`
|
||||
ScrollDown string `mapstructure:"scroll_down"`
|
||||
GotoTop string `mapstructure:"goto_top"`
|
||||
GotoBottom string `mapstructure:"goto_bottom"`
|
||||
PrevPage string `mapstructure:"prev_page"`
|
||||
NextPage string `mapstructure:"next_page"`
|
||||
}
|
||||
|
||||
type DocsKeys struct {
|
||||
Search string `mapstructure:"search"`
|
||||
SearchReset string `mapstructure:"search_reset"`
|
||||
SearchNext string `mapstructure:"search_next"`
|
||||
SearchPrev string `mapstructure:"search_prev"`
|
||||
}
|
||||
|
||||
type NetworkKeys struct {
|
||||
Select string `mapstructure:"select"`
|
||||
Refresh string `mapstructure:"refresh"`
|
||||
Forget string `mapstructure:"forget"`
|
||||
ToggleAuto string `mapstructure:"toggle_auto"`
|
||||
RevealSecret string `mapstructure:"reveal_secret"`
|
||||
Known string `mapstructure:"known"`
|
||||
Info string `mapstructure:"info"`
|
||||
Disconnect string `mapstructure:"disconnect"`
|
||||
ToggleRadio string `mapstructure:"toggle_radio"`
|
||||
}
|
||||
|
||||
type BluetoothKeys struct {
|
||||
Select string `mapstructure:"select"`
|
||||
Refresh string `mapstructure:"refresh"`
|
||||
Forget string `mapstructure:"forget"`
|
||||
ToggleTrust string `mapstructure:"toggle_trust"`
|
||||
Info string `mapstructure:"info"`
|
||||
TogglePower string `mapstructure:"toggle_power"`
|
||||
}
|
||||
|
||||
type AudioKeys struct {
|
||||
Select string `mapstructure:"select"`
|
||||
Refresh string `mapstructure:"refresh"`
|
||||
Mute string `mapstructure:"mute"`
|
||||
Test string `mapstructure:"test"`
|
||||
}
|
||||
|
||||
type Keybindings struct {
|
||||
Global GlobalKeys `mapstructure:"global"`
|
||||
Docs DocsKeys `mapstructure:"docs"`
|
||||
Network NetworkKeys `mapstructure:"network"`
|
||||
Bluetooth BluetoothKeys `mapstructure:"bluetooth"`
|
||||
Audio AudioKeys `mapstructure:"audio"`
|
||||
}
|
||||
Reference in New Issue
Block a user