diff --git a/config.go b/config.go index f9425ee..cbbe1fb 100644 --- a/config.go +++ b/config.go @@ -15,6 +15,7 @@ func configInit() { var ( defaultConfigPath string themeConfig = config.New() + keyMapConfig = config.New() defaultKeyMaps = map[string]string{ "translate": "j", "swap_language": "s", @@ -56,17 +57,21 @@ func configInit() { config.SetConfigType("yaml") themeConfig.SetConfigName("theme") themeConfig.SetConfigType("yaml") + keyMapConfig.SetConfigName("keymap") + themeConfig.SetConfigType("yaml") if len(os.Getenv("XDG_CONFIG_HOME")) > 0 { defaultConfigPath = os.Getenv("XDG_CONFIG_HOME") + "/gtt" config.AddConfigPath(defaultConfigPath) themeConfig.AddConfigPath(defaultConfigPath) + keyMapConfig.AddConfigPath(defaultConfigPath) } else { defaultConfigPath = os.Getenv("HOME") + "/.config/gtt" } config.AddConfigPath("$HOME/.config/gtt") themeConfig.AddConfigPath("$HOME/.config/gtt") + keyMapConfig.AddConfigPath("$HOME/.config/gtt") - // import theme if file exists + // Import theme if file exists if err := themeConfig.ReadInConfig(); err == nil { var ( palate = make(map[string]int32) @@ -112,6 +117,22 @@ func configInit() { } } + // Setup key map + // If keymap file exist and action in file exist, then set the keyMap + // Otherwise, set to defaultKeyMap + if err := keyMapConfig.ReadInConfig(); err == nil { + for action, key := range defaultKeyMaps { + if keyMapConfig.Get(action) == nil { + keyMaps[action] = ui.NewKeyData(key) + } else { + keyMaps[action] = ui.NewKeyData(keyMapConfig.GetString(action)) + } + } + } else { + for action, key := range defaultKeyMaps { + keyMaps[action] = ui.NewKeyData(key) + } + } // Setup for _, name := range translate.AllTranslator { translators[name] = translate.NewTranslator(name) @@ -139,11 +160,6 @@ func configInit() { if len(*dstLangArg) > 0 { translator.SetDstLang(*dstLangArg) } - - // Set key map - for action, key := range defaultKeyMaps { - keyMaps[action] = ui.NewKeyData(key) - } } // Check if need to modify config file when quit program diff --git a/internal/ui/key.go b/internal/ui/key.go index 30ce21e..35f7468 100644 --- a/internal/ui/key.go +++ b/internal/ui/key.go @@ -13,22 +13,22 @@ type keyData struct { type KeyMaps map[string]keyData -func NewKeyData(chStr string) keyData { +func NewKeyData(keyStr string) keyData { var ( name string key tcell.Key ) - if len(chStr) > 1 && chStr[0] == 'F' { + if len(keyStr) > 1 && keyStr[0] == 'F' { // function key, can be F1 to F64 - name = chStr - fNum, err := strconv.Atoi(chStr[1:]) + name = keyStr + fNum, err := strconv.Atoi(keyStr[1:]) if err != nil { panic(err) } key = tcell.KeyF1 + tcell.Key(fNum-1) } else { - switch chStr[0] { + switch keyStr[0] { case ' ': name = "C-Space" key = tcell.KeyCtrlSpace @@ -46,8 +46,8 @@ func NewKeyData(chStr string) keyData { key = tcell.KeyCtrlUnderscore default: // This should be a to z - name = "C-" + chStr - key = tcell.KeyCtrlA + tcell.Key(chStr[0]-'a') + name = "C-" + keyStr + key = tcell.KeyCtrlA + tcell.Key(keyStr[0]-'a') } }