Enable default keybindings by default

This commit is contained in:
Tulir Asokan 2022-03-06 22:28:02 +02:00
parent 9a0a1636af
commit 073739b79b
2 changed files with 38 additions and 62 deletions

View File

@ -17,7 +17,9 @@
package config package config
import ( import (
_ "embed"
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -65,14 +67,14 @@ type Keybind struct {
Ch rune Ch rune
} }
type Keybindings struct { type ParsedKeybindings struct {
Main map[Keybind]string `yaml:"main,omitempty"` Main map[Keybind]string
Room map[Keybind]string `yaml:"room,omitempty"` Room map[Keybind]string
Modal map[Keybind]string `yaml:"modal,omitempty"` Modal map[Keybind]string
Visual map[Keybind]string `yaml:"visual,omitempty"` Visual map[Keybind]string
} }
type KeybindingsConfig struct { type RawKeybindings struct {
Main map[string]string `yaml:"main,omitempty"` Main map[string]string `yaml:"main,omitempty"`
Room map[string]string `yaml:"room,omitempty"` Room map[string]string `yaml:"room,omitempty"`
Modal map[string]string `yaml:"modal,omitempty"` Modal map[string]string `yaml:"modal,omitempty"`
@ -108,7 +110,7 @@ type Config struct {
AuthCache AuthCache `yaml:"-"` AuthCache AuthCache `yaml:"-"`
Rooms *rooms.RoomCache `yaml:"-"` Rooms *rooms.RoomCache `yaml:"-"`
PushRules *pushrules.PushRuleset `yaml:"-"` PushRules *pushrules.PushRuleset `yaml:"-"`
Keybindings Keybindings `yaml:"-"` Keybindings ParsedKeybindings `yaml:"-"`
nosave bool nosave bool
} }
@ -214,64 +216,39 @@ func (config *Config) SavePreferences() {
config.save("user preferences", config.CacheDir, "preferences.yaml", &config.Preferences) config.save("user preferences", config.CacheDir, "preferences.yaml", &config.Preferences)
} }
//go:embed keybindings.yaml
var DefaultKeybindings string
func parseKeybindings(input map[string]string) (output map[Keybind]string) {
output = make(map[Keybind]string, len(input))
for shortcut, action := range input {
mod, key, ch, err := cbind.Decode(shortcut)
if err != nil {
panic(fmt.Errorf("failed to parse keybinding %s -> %s: %w", shortcut, action, err))
}
parsedShortcut := Keybind{
Mod: mod,
Key: key,
Ch: ch,
}
output[parsedShortcut] = action
}
return
}
func (config *Config) LoadKeybindings() { func (config *Config) LoadKeybindings() {
cfg := KeybindingsConfig{} var inputConfig RawKeybindings
config.load("keybindings", config.Dir, "keybindings.yaml", &cfg)
config.Keybindings.Main = make(map[Keybind]string)
config.Keybindings.Room = make(map[Keybind]string)
config.Keybindings.Modal = make(map[Keybind]string)
config.Keybindings.Visual = make(map[Keybind]string)
for k, v := range cfg.Main { err := yaml.Unmarshal([]byte(DefaultKeybindings), &inputConfig)
mod, key, ch, err := cbind.Decode(k) if err != nil {
if err != nil { panic(fmt.Errorf("failed to unmarshal default keybindings: %w", err))
// todo
}
kb := Keybind{
Mod: mod,
Key: key,
Ch: ch,
}
config.Keybindings.Main[kb] = v
}
for k, v := range cfg.Room {
mod, key, ch, err := cbind.Decode(k)
if err != nil {
// todo
}
kb := Keybind{
Mod: mod,
Key: key,
Ch: ch,
}
config.Keybindings.Room[kb] = v
} }
config.load("keybindings", config.Dir, "keybindings.yaml", &inputConfig)
for k, v := range cfg.Modal { config.Keybindings.Main = parseKeybindings(inputConfig.Main)
mod, key, ch, err := cbind.Decode(k) config.Keybindings.Room = parseKeybindings(inputConfig.Room)
if err != nil { config.Keybindings.Modal = parseKeybindings(inputConfig.Modal)
// todo config.Keybindings.Visual = parseKeybindings(inputConfig.Visual)
}
kb := Keybind{
Mod: mod,
Key: key,
Ch: ch,
}
config.Keybindings.Modal[kb] = v
}
for k, v := range cfg.Visual {
mod, key, ch, err := cbind.Decode(k)
if err != nil {
// todo
}
kb := Keybind{
Mod: mod,
Key: key,
Ch: ch,
}
config.Keybindings.Visual[kb] = v
}
} }
func (config *Config) SaveKeybindings() { func (config *Config) SaveKeybindings() {

View File

@ -1,4 +1,3 @@
---
main: main:
'Ctrl+Down': next_room 'Ctrl+Down': next_room
'Ctrl+Up': prev_room 'Ctrl+Up': prev_room