v/GTT
1
0
mirror of https://github.com/eeeXun/GTT.git synced 2025-05-19 01:00:20 -07:00

feat: support function key

This commit is contained in:
eeeXun 2023-06-21 10:59:19 +08:00
parent 7fae960ce2
commit 91ee46933a
2 changed files with 52 additions and 33 deletions

View File

@ -1,44 +1,63 @@
package ui package ui
import ( import (
"strconv"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
) )
type keyData struct { type keyData struct {
ch rune name string
key tcell.Key key tcell.Key
} }
type KeyMaps map[string]keyData type KeyMaps map[string]keyData
func NewKeyData(chStr string) keyData { func NewKeyData(chStr string) keyData {
var key tcell.Key var (
ch := rune(chStr[0]) name string
key tcell.Key
)
switch ch { if len(chStr) > 1 && chStr[0] == 'F' {
case ' ': name = chStr
key = tcell.KeyCtrlSpace fNum, err := strconv.Atoi(chStr[1:])
case '\\': if err != nil {
key = tcell.KeyCtrlBackslash panic(err)
case ']': }
key = tcell.KeyCtrlRightSq key = tcell.KeyF1 + tcell.Key(fNum-1)
case '^': } else {
key = tcell.KeyCtrlCarat switch chStr[0] {
case '_': case ' ':
key = tcell.KeyCtrlUnderscore name = "C-Space"
default: key = tcell.KeyCtrlSpace
// This should be a to z case '\\':
key = tcell.KeyCtrlA + tcell.Key(ch-'a') name = "C-\\"
key = tcell.KeyCtrlBackslash
case ']':
name = "C-]"
key = tcell.KeyCtrlRightSq
case '^':
name = "C-^"
key = tcell.KeyCtrlCarat
case '_':
name = "C-_"
key = tcell.KeyCtrlUnderscore
default:
// This should be a to z
name = "C-" + chStr
key = tcell.KeyCtrlA + tcell.Key(chStr[0]-'a')
}
} }
return keyData{ return keyData{
ch: ch, name: name,
key: key, key: key,
} }
} }
func (k keyData) GetName() rune { func (k keyData) GetName() string {
return k.ch return k.name
} }
func (k keyData) GetKey() tcell.Key { func (k keyData) GetKey() tcell.Key {

22
ui.go
View File

@ -17,27 +17,27 @@ const (
Exit program. Exit program.
[#%[1]s]<Esc>[-] [#%[1]s]<Esc>[-]
Toggle pop out window. Toggle pop out window.
[#%[1]s]<C-%[2]c>[-] [#%[1]s]<%[2]s>[-]
Translate from source to destination window. Translate from source to destination window.
[#%[1]s]<C-%[3]c>[-] [#%[1]s]<%[3]s>[-]
Swap language. Swap language.
[#%[1]s]<C-%[4]c>[-] [#%[1]s]<%[4]s>[-]
Clear all text in source of translation window. Clear all text in source of translation window.
[#%[1]s]<C-%[5]c>[-] [#%[1]s]<%[5]s>[-]
Copy selected text. Copy selected text.
[#%[1]s]<C-%[6]c>[-] [#%[1]s]<%[6]s>[-]
Copy all text in source of translation window. Copy all text in source of translation window.
[#%[1]s]<C-%[7]c>[-] [#%[1]s]<%[7]s>[-]
Copy all text in destination of translation window. Copy all text in destination of translation window.
[#%[1]s]<C-%[8]c>[-] [#%[1]s]<%[8]s>[-]
Play text to speech on source of translation window. Play text to speech on source of translation window.
[#%[1]s]<C-%[9]c>[-] [#%[1]s]<%[9]s>[-]
Play text to speech on destination of translation window. Play text to speech on destination of translation window.
[#%[1]s]<C-%[10]c>[-] [#%[1]s]<%[10]s>[-]
Stop playing text to speech. Stop playing text to speech.
[#%[1]s]<C-%[11]c>[-] [#%[1]s]<%[11]s>[-]
Toggle transparent. Toggle transparent.
[#%[1]s]<C-%[12]c>[-] [#%[1]s]<%[12]s>[-]
Toggle Definition/Example & Part of speech. Toggle Definition/Example & Part of speech.
[#%[1]s]<Tab>, <S-Tab>[-] [#%[1]s]<Tab>, <S-Tab>[-]
Cycle through the pop out widget. Cycle through the pop out widget.