From c20c8b67dbca811098daceca703cf5011c2d3c61 Mon Sep 17 00:00:00 2001 From: eeeXun Date: Sat, 24 Jun 2023 10:57:11 +0800 Subject: [PATCH] feat: let exit be configurable --- README.md | 3 +-- config.go | 1 + example/keymap.yaml | 3 ++- ui.go | 12 ++++++++++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 78fb36e..e5931c4 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,7 @@ Switch pop out window. You can overwrite the following key +- `exit`: Exit program. - `translate`: Translate from source to destination window. - `swap_language`: Swap language. - `clear`: Clear all text in source of translation window. @@ -177,8 +178,6 @@ You can overwrite the following key For key to combine with `Ctrl`, the value can be `"C-space"`, `"C-\\"`, `"C-]"`, `"C-^"`, `"C-_"` or `"C-a"` to `"C-z"`. -⚠️ Note, don't use `"C-c"`, `` is for exit program. - For key to combine with `Alt`, the value can be `"A-space"` or `"A-"` + the character you want. Or you can use function key, the value can be `"F1"` to `"F64"`. diff --git a/config.go b/config.go index 2920723..2c7b990 100644 --- a/config.go +++ b/config.go @@ -16,6 +16,7 @@ func configInit() { themeConfig = config.New() keyMapConfig = config.New() defaultKeyMaps = map[string]string{ + "exit": "C-c", "translate": "C-j", "swap_language": "C-s", "clear": "C-q", diff --git a/example/keymap.yaml b/example/keymap.yaml index 6acb34e..750ba90 100644 --- a/example/keymap.yaml +++ b/example/keymap.yaml @@ -1,7 +1,6 @@ # This file should be located at $XDG_CONFIG_HOME/gtt/keymap.yaml or $HOME/.config/gtt/keymap.yaml. # For key to combine with Ctrl, the value can be "C-space", "C-\\", "C-]", "C-^", "C-_" or "C-a" to "C-z". -# ⚠️ Note, don't use "C-c", is for exit program. # For key to combine with Alt, the value can be "A-space" or "A-" + the character you want. @@ -10,6 +9,8 @@ # The following is the default key map +# Exit program, +exit: "C-c" # Translate from source to destination window, translate: "C-j" # Swap language, diff --git a/ui.go b/ui.go index 34d90c6..62e95bd 100644 --- a/ui.go +++ b/ui.go @@ -366,7 +366,7 @@ func uiInit() { updateCurrentLang() // handler - mainPage.SetInputCapture(mainPageHandler) + app.SetInputCapture(appHandler) translateWindow.SetInputCapture(translateWindowHandler) for _, widget := range []*tview.TextArea{srcInput, defOutput, posOutput} { // fix for loop problem @@ -441,7 +441,7 @@ func uiInit() { keyMapButton.SetSelectedFunc(showKeyMapPopout) } -func mainPageHandler(event *tcell.EventKey) *tcell.EventKey { +func appHandler(event *tcell.EventKey) *tcell.EventKey { keyName := getKeyName(event) if len(keyName) == 0 { @@ -449,6 +449,9 @@ func mainPageHandler(event *tcell.EventKey) *tcell.EventKey { } switch keyName { + case keyMaps["exit"]: + app.Stop() + return nil case keyMaps["toggle_transparent"]: // Toggle transparent uiStyle.Transparent = !uiStyle.Transparent @@ -467,6 +470,11 @@ func mainPageHandler(event *tcell.EventKey) *tcell.EventKey { return nil } + // Force C-c not to exit program + if event.Key() == tcell.KeyCtrlC { + return tcell.NewEventKey(tcell.KeyCtrlC, 0, tcell.ModNone) + } + return event }