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:
parent
efe7b6dd26
commit
42ffdca542
28
config.go
28
config.go
@ -15,6 +15,7 @@ func configInit() {
|
|||||||
var (
|
var (
|
||||||
defaultConfigPath string
|
defaultConfigPath string
|
||||||
themeConfig = config.New()
|
themeConfig = config.New()
|
||||||
|
keyMapConfig = config.New()
|
||||||
defaultKeyMaps = map[string]string{
|
defaultKeyMaps = map[string]string{
|
||||||
"translate": "j",
|
"translate": "j",
|
||||||
"swap_language": "s",
|
"swap_language": "s",
|
||||||
@ -56,17 +57,21 @@ func configInit() {
|
|||||||
config.SetConfigType("yaml")
|
config.SetConfigType("yaml")
|
||||||
themeConfig.SetConfigName("theme")
|
themeConfig.SetConfigName("theme")
|
||||||
themeConfig.SetConfigType("yaml")
|
themeConfig.SetConfigType("yaml")
|
||||||
|
keyMapConfig.SetConfigName("keymap")
|
||||||
|
themeConfig.SetConfigType("yaml")
|
||||||
if len(os.Getenv("XDG_CONFIG_HOME")) > 0 {
|
if len(os.Getenv("XDG_CONFIG_HOME")) > 0 {
|
||||||
defaultConfigPath = os.Getenv("XDG_CONFIG_HOME") + "/gtt"
|
defaultConfigPath = os.Getenv("XDG_CONFIG_HOME") + "/gtt"
|
||||||
config.AddConfigPath(defaultConfigPath)
|
config.AddConfigPath(defaultConfigPath)
|
||||||
themeConfig.AddConfigPath(defaultConfigPath)
|
themeConfig.AddConfigPath(defaultConfigPath)
|
||||||
|
keyMapConfig.AddConfigPath(defaultConfigPath)
|
||||||
} else {
|
} else {
|
||||||
defaultConfigPath = os.Getenv("HOME") + "/.config/gtt"
|
defaultConfigPath = os.Getenv("HOME") + "/.config/gtt"
|
||||||
}
|
}
|
||||||
config.AddConfigPath("$HOME/.config/gtt")
|
config.AddConfigPath("$HOME/.config/gtt")
|
||||||
themeConfig.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 {
|
if err := themeConfig.ReadInConfig(); err == nil {
|
||||||
var (
|
var (
|
||||||
palate = make(map[string]int32)
|
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
|
// Setup
|
||||||
for _, name := range translate.AllTranslator {
|
for _, name := range translate.AllTranslator {
|
||||||
translators[name] = translate.NewTranslator(name)
|
translators[name] = translate.NewTranslator(name)
|
||||||
@ -139,11 +160,6 @@ func configInit() {
|
|||||||
if len(*dstLangArg) > 0 {
|
if len(*dstLangArg) > 0 {
|
||||||
translator.SetDstLang(*dstLangArg)
|
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
|
// Check if need to modify config file when quit program
|
||||||
|
@ -13,22 +13,22 @@ type keyData struct {
|
|||||||
|
|
||||||
type KeyMaps map[string]keyData
|
type KeyMaps map[string]keyData
|
||||||
|
|
||||||
func NewKeyData(chStr string) keyData {
|
func NewKeyData(keyStr string) keyData {
|
||||||
var (
|
var (
|
||||||
name string
|
name string
|
||||||
key tcell.Key
|
key tcell.Key
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(chStr) > 1 && chStr[0] == 'F' {
|
if len(keyStr) > 1 && keyStr[0] == 'F' {
|
||||||
// function key, can be F1 to F64
|
// function key, can be F1 to F64
|
||||||
name = chStr
|
name = keyStr
|
||||||
fNum, err := strconv.Atoi(chStr[1:])
|
fNum, err := strconv.Atoi(keyStr[1:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
key = tcell.KeyF1 + tcell.Key(fNum-1)
|
key = tcell.KeyF1 + tcell.Key(fNum-1)
|
||||||
} else {
|
} else {
|
||||||
switch chStr[0] {
|
switch keyStr[0] {
|
||||||
case ' ':
|
case ' ':
|
||||||
name = "C-Space"
|
name = "C-Space"
|
||||||
key = tcell.KeyCtrlSpace
|
key = tcell.KeyCtrlSpace
|
||||||
@ -46,8 +46,8 @@ func NewKeyData(chStr string) keyData {
|
|||||||
key = tcell.KeyCtrlUnderscore
|
key = tcell.KeyCtrlUnderscore
|
||||||
default:
|
default:
|
||||||
// This should be a to z
|
// This should be a to z
|
||||||
name = "C-" + chStr
|
name = "C-" + keyStr
|
||||||
key = tcell.KeyCtrlA + tcell.Key(chStr[0]-'a')
|
key = tcell.KeyCtrlA + tcell.Key(keyStr[0]-'a')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user