v/GTT
1
0
mirror of https://github.com/eeeXun/GTT.git synced 2025-05-18 08:40:35 -07:00

feat: support read config

This commit is contained in:
eeeXun 2023-06-21 11:51:27 +08:00
parent efe7b6dd26
commit 42ffdca542
2 changed files with 29 additions and 13 deletions

View File

@ -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

View File

@ -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')
}
}