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

feat: copy text (#1)

* copy key with `<C-y>` `<C-g>` `<C-r>`
This commit is contained in:
Xun 2022-10-26 08:18:23 +08:00 committed by GitHub
parent 98ff16df38
commit 7523cec0ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 0 deletions

View File

@ -29,6 +29,15 @@ Swap language.
`<C-q>`
Clear all text in left window.
`<C-y>`
Copy selected text in left window.
`<C-g>`
Copy all text in left window.
`<C-r>`
Copy all text in right window.
`<C-o>`
Play sound on left window.
@ -47,6 +56,12 @@ Cycle through the pop out widget.
`<1>`, `<2>`, `<3>`
Switch pop out window.
## Dependencies
`xclip` For Linux to copy text.
`pbcopy` For macOS to copy text.
## Credit
[snsd0805/GoogleTranslate-TUI](https://github.com/snsd0805/GoogleTranslate-TUI) For inspiration.

30
ui.go
View File

@ -20,6 +20,12 @@ const (
Swap language.
[#%[1]s]<C-q>[-]
Clear all text in left window.
[#%[1]s]<C-y>[-]
Copy selected text in left window.
[#%[1]s]<C-g>[-]
Copy all text in left window.
[#%[1]s]<C-r>[-]
Copy all text in right window.
[#%[1]s]<C-o>[-]
Play sound on left window.
[#%[1]s]<C-p>[-]
@ -370,6 +376,30 @@ func translatePageHandler(event *tcell.EventKey) *tcell.EventKey {
}
case tcell.KeyCtrlQ:
srcInput.SetText("", true)
case tcell.KeyCtrlY:
// copy selected text
text, _, _ := srcInput.GetSelection()
// only copy when text selected
if len(text) > 0 {
CopyToClipboard(text)
}
case tcell.KeyCtrlG:
// copy all text in Input
text := srcInput.GetText()
// only copy when text exist
if len(text) > 0 {
CopyToClipboard(text)
}
case tcell.KeyCtrlR:
// copy all text in Output
text := dstOutput.GetText(false)
// only copy when text exist
if len(text) > 0 {
CopyToClipboard(text[:len(text)-1])
}
case tcell.KeyCtrlS:
translator.SrcLang, translator.DstLang = translator.DstLang, translator.SrcLang
updateTitle()

View File

@ -1,5 +1,11 @@
package main
import (
"fmt"
"os/exec"
"runtime"
)
func IndexOf(candidate string, arr []string) int {
for index, element := range arr {
if element == candidate {
@ -12,3 +18,16 @@ func IndexOf(candidate string, arr []string) int {
func SetTermTitle(title string) {
print("\033]0;", title, "\007")
}
func CopyToClipboard(text string) {
switch runtime.GOOS {
case "linux":
exec.Command("sh", "-c",
fmt.Sprintf("echo -n '%s' | xclip -selection clipboard", text)).
Start()
case "darwin":
exec.Command("sh", "-c",
fmt.Sprintf("echo -n '%s' | pbcopy", text)).
Start()
}
}