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

feat: support OSC 52 (#32)

* feat: support OSC 52

* docs: note for OSC 52
This commit is contained in:
Yin-Hsun Hong 2024-08-10 14:12:15 +08:00 committed by GitHub
parent 06dafb5bf8
commit f87b7b43ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 45 additions and 16 deletions

View File

@ -64,6 +64,8 @@ For RedHat-based Linux, you need `alsa-lib-devel`.
[`wl-clipboard`](https://github.com/bugaevc/wl-clipboard) (optional) - for Linux/Wayland to copy text. [`wl-clipboard`](https://github.com/bugaevc/wl-clipboard) (optional) - for Linux/Wayland to copy text.
Or, if your terminal supports OSC 52, you can enable OSC 52 in page 2 of the pop out menu to copy text.
### Arch Linux ([AUR](https://aur.archlinux.org/packages/gtt-bin)) ### Arch Linux ([AUR](https://aur.archlinux.org/packages/gtt-bin))
```sh ```sh
@ -120,7 +122,7 @@ docker run -it eeexun/gtt:latest
Exit program. Exit program.
`<Esc>` `<Esc>`
Toggle pop out window. Toggle pop out menu.
`<C-j>` `<C-j>`
Translate from source to destination window. Translate from source to destination window.
@ -159,7 +161,7 @@ Toggle Definition/Example & Part of speech.
Cycle through the pop out widget. Cycle through the pop out widget.
`<1>`, `<2>`, `<3>` `<1>`, `<2>`, `<3>`
Switch pop out window. Switch pop out menu.
### Customize key map ### Customize key map

View File

@ -37,6 +37,7 @@ func configInit() {
"toggle_below": "C-\\", "toggle_below": "C-\\",
} }
defaultConfig = map[string]interface{}{ defaultConfig = map[string]interface{}{
"osc52": false,
"hide_below": false, "hide_below": false,
"transparent": false, "transparent": false,
"theme": "gruvbox", "theme": "gruvbox",
@ -153,6 +154,7 @@ func configInit() {
} }
translator = translators[config.GetString("translator")] translator = translators[config.GetString("translator")]
uiStyle.Theme = config.GetString("theme") uiStyle.Theme = config.GetString("theme")
uiStyle.OSC52 = config.GetBool("osc52")
uiStyle.HideBelow = config.GetBool("hide_below") uiStyle.HideBelow = config.GetBool("hide_below")
uiStyle.Transparent = config.GetBool("transparent") uiStyle.Transparent = config.GetBool("transparent")
uiStyle.SetSrcBorderColor(config.GetString("source.border_color")). uiStyle.SetSrcBorderColor(config.GetString("source.border_color")).
@ -211,10 +213,6 @@ func updateConfig() {
changed = true changed = true
config.Set("translator", translator.GetEngineName()) config.Set("translator", translator.GetEngineName())
} }
if config.GetBool("hide_below") != uiStyle.HideBelow {
changed = true
config.Set("hide_below", uiStyle.HideBelow)
}
if config.GetString("theme") != uiStyle.Theme { if config.GetString("theme") != uiStyle.Theme {
changed = true changed = true
config.Set("theme", uiStyle.Theme) config.Set("theme", uiStyle.Theme)
@ -223,6 +221,14 @@ func updateConfig() {
changed = true changed = true
config.Set("transparent", uiStyle.Transparent) config.Set("transparent", uiStyle.Transparent)
} }
if config.GetBool("hide_below") != uiStyle.HideBelow {
changed = true
config.Set("hide_below", uiStyle.HideBelow)
}
if config.GetBool("osc52") != uiStyle.OSC52 {
changed = true
config.Set("osc52", uiStyle.OSC52)
}
if config.GetString("source.border_color") != uiStyle.SrcBorderStr() { if config.GetString("source.border_color") != uiStyle.SrcBorderStr() {
changed = true changed = true
config.Set("source.border_color", uiStyle.SrcBorderStr()) config.Set("source.border_color", uiStyle.SrcBorderStr())

View File

@ -5,6 +5,7 @@ import (
) )
type style struct { type style struct {
OSC52 bool
HideBelow bool HideBelow bool
Transparent bool Transparent bool
Theme string Theme string

View File

@ -35,12 +35,14 @@ var (
themeDropDown = tview.NewDropDown() themeDropDown = tview.NewDropDown()
transparentDropDown = tview.NewDropDown() transparentDropDown = tview.NewDropDown()
hideBelowDropDown = tview.NewDropDown() hideBelowDropDown = tview.NewDropDown()
osc52DropDown = tview.NewDropDown()
srcBorderDropDown = tview.NewDropDown() srcBorderDropDown = tview.NewDropDown()
dstBorderDropDown = tview.NewDropDown() dstBorderDropDown = tview.NewDropDown()
styleCycle = ui.NewUICycle( styleCycle = ui.NewUICycle(
themeDropDown, themeDropDown,
transparentDropDown, transparentDropDown,
hideBelowDropDown, hideBelowDropDown,
osc52DropDown,
srcBorderDropDown, srcBorderDropDown,
dstBorderDropDown) dstBorderDropDown)
keyMapMenu = tview.NewTextView() keyMapMenu = tview.NewTextView()

32
ui.go
View File

@ -18,12 +18,12 @@ type Item struct {
} }
const ( const (
popOutWindowHeight int = 20 popOutMenuHeight int = 20
langStrMaxLength int = 32 langStrMaxLength int = 32
keyMapText string = `[#%[1]s]<C-c>[-] keyMapText string = `[#%[1]s]<C-c>[-]
Exit program. Exit program.
[#%[1]s]<Esc>[-] [#%[1]s]<Esc>[-]
Toggle pop out window. Toggle pop out menu.
[#%[1]s]<%[2]s>[-] [#%[1]s]<%[2]s>[-]
Translate from source to destination window. Translate from source to destination window.
[#%[1]s]<%[3]s>[-] [#%[1]s]<%[3]s>[-]
@ -49,7 +49,7 @@ const (
[#%[1]s]<Tab>, <S-Tab>[-] [#%[1]s]<Tab>, <S-Tab>[-]
Cycle through the pop out widget. Cycle through the pop out widget.
[#%[1]s]<1>, <2>, <3>[-] [#%[1]s]<1>, <2>, <3>[-]
Switch pop out window.` Switch pop out menu.`
) )
func updateTranslateWindow() { func updateTranslateWindow() {
@ -84,6 +84,7 @@ func updateBackgroundColor() {
themeDropDown, themeDropDown,
transparentDropDown, transparentDropDown,
hideBelowDropDown, hideBelowDropDown,
osc52DropDown,
srcBorderDropDown, srcBorderDropDown,
dstBorderDropDown} { dstBorderDropDown} {
dropdown.SetListStyles(tcell.StyleDefault. dropdown.SetListStyles(tcell.StyleDefault.
@ -145,6 +146,7 @@ func updateNonConfigColor() {
themeDropDown, themeDropDown,
transparentDropDown, transparentDropDown,
hideBelowDropDown, hideBelowDropDown,
osc52DropDown,
srcBorderDropDown, srcBorderDropDown,
dstBorderDropDown} { dstBorderDropDown} {
labelDropDown.SetLabelColor(uiStyle.LabelColor()). labelDropDown.SetLabelColor(uiStyle.LabelColor()).
@ -286,15 +288,20 @@ func uiInit() {
themeDropDown.SetLabel("Theme: "). themeDropDown.SetLabel("Theme: ").
SetOptions(style.AllTheme, nil). SetOptions(style.AllTheme, nil).
SetCurrentOption(IndexOf(uiStyle.Theme, style.AllTheme)) SetCurrentOption(IndexOf(uiStyle.Theme, style.AllTheme))
transparentDropDown.SetLabel("Transparent: ").
SetOptions([]string{"true", "false"}, nil).
SetCurrentOption(
IndexOf(strconv.FormatBool(uiStyle.Transparent),
[]string{"true", "false"}))
hideBelowDropDown.SetLabel("Hide below: "). hideBelowDropDown.SetLabel("Hide below: ").
SetOptions([]string{"true", "false"}, nil). SetOptions([]string{"true", "false"}, nil).
SetCurrentOption( SetCurrentOption(
IndexOf(strconv.FormatBool(uiStyle.HideBelow), IndexOf(strconv.FormatBool(uiStyle.HideBelow),
[]string{"true", "false"})) []string{"true", "false"}))
transparentDropDown.SetLabel("Transparent: "). osc52DropDown.SetLabel("OSC 52: ").
SetOptions([]string{"true", "false"}, nil). SetOptions([]string{"true", "false"}, nil).
SetCurrentOption( SetCurrentOption(
IndexOf(strconv.FormatBool(uiStyle.Transparent), IndexOf(strconv.FormatBool(uiStyle.OSC52),
[]string{"true", "false"})) []string{"true", "false"}))
srcBorderDropDown.SetLabel("Border Color: "). srcBorderDropDown.SetLabel("Border Color: ").
SetOptions(style.Palette, nil). SetOptions(style.Palette, nil).
@ -340,7 +347,7 @@ func uiInit() {
Item{item: dstLangDropDown, fixedSize: 0, proportion: 1, focus: false}), Item{item: dstLangDropDown, fixedSize: 0, proportion: 1, focus: false}),
fixedSize: 0, proportion: 1, focus: true}), fixedSize: 0, proportion: 1, focus: true}),
fixedSize: 2 * langStrMaxLength, proportion: 1, focus: true}), fixedSize: 2 * langStrMaxLength, proportion: 1, focus: true}),
popOutWindowHeight, 1, true). popOutMenuHeight, 1, true).
AddItem(attachButton(), 1, 1, false). AddItem(attachButton(), 1, 1, false).
AddItem(nil, 0, 1, false) AddItem(nil, 0, 1, false)
stylePopOut.SetDirection(tview.FlexRow). stylePopOut.SetDirection(tview.FlexRow).
@ -351,22 +358,23 @@ func uiInit() {
Item{item: attachItems(false, tview.FlexRow, Item{item: attachItems(false, tview.FlexRow,
Item{item: themeDropDown, fixedSize: 0, proportion: 1, focus: true}, Item{item: themeDropDown, fixedSize: 0, proportion: 1, focus: true},
Item{item: transparentDropDown, fixedSize: 0, proportion: 1, focus: false}, Item{item: transparentDropDown, fixedSize: 0, proportion: 1, focus: false},
Item{item: hideBelowDropDown, fixedSize: 0, proportion: 1, focus: false}), Item{item: hideBelowDropDown, fixedSize: 0, proportion: 1, focus: false},
Item{item: osc52DropDown, fixedSize: 0, proportion: 1, focus: false}),
fixedSize: 0, proportion: 1, focus: true}), fixedSize: 0, proportion: 1, focus: true}),
fixedSize: 3, proportion: 1, focus: true}, fixedSize: 4, proportion: 1, focus: true},
Item{item: attachItems(false, tview.FlexColumn, Item{item: attachItems(false, tview.FlexColumn,
Item{item: srcBorderDropDown, fixedSize: 0, proportion: 1, focus: false}, Item{item: srcBorderDropDown, fixedSize: 0, proportion: 1, focus: false},
Item{item: dstBorderDropDown, fixedSize: 0, proportion: 1, focus: false}), Item{item: dstBorderDropDown, fixedSize: 0, proportion: 1, focus: false}),
fixedSize: 0, proportion: 1, focus: false}), fixedSize: 0, proportion: 1, focus: false}),
fixedSize: 2 * langStrMaxLength, proportion: 1, focus: true}), fixedSize: 2 * langStrMaxLength, proportion: 1, focus: true}),
popOutWindowHeight, 1, true). popOutMenuHeight, 1, true).
AddItem(attachButton(), 1, 1, false). AddItem(attachButton(), 1, 1, false).
AddItem(nil, 0, 1, false) AddItem(nil, 0, 1, false)
keyMapPopOut.SetDirection(tview.FlexRow). keyMapPopOut.SetDirection(tview.FlexRow).
AddItem(nil, 0, 1, false). AddItem(nil, 0, 1, false).
AddItem(attachItems(true, tview.FlexColumn, AddItem(attachItems(true, tview.FlexColumn,
Item{item: keyMapMenu, fixedSize: 2 * langStrMaxLength, proportion: 1, focus: true}), Item{item: keyMapMenu, fixedSize: 2 * langStrMaxLength, proportion: 1, focus: true}),
popOutWindowHeight, 1, true). popOutMenuHeight, 1, true).
AddItem(attachButton(), 1, 1, false). AddItem(attachButton(), 1, 1, false).
AddItem(nil, 0, 1, false) AddItem(nil, 0, 1, false)
@ -429,6 +437,10 @@ func uiInit() {
uiStyle.HideBelow, _ = strconv.ParseBool(text) uiStyle.HideBelow, _ = strconv.ParseBool(text)
updateTranslateWindow() updateTranslateWindow()
}) })
osc52DropDown.SetDoneFunc(styleDropDownHandler).
SetSelectedFunc(func(text string, index int) {
uiStyle.OSC52, _ = strconv.ParseBool(text)
})
srcBorderDropDown.SetDoneFunc(styleDropDownHandler). srcBorderDropDown.SetDoneFunc(styleDropDownHandler).
SetSelectedFunc(func(text string, index int) { SetSelectedFunc(func(text string, index int) {
uiStyle.SetSrcBorderColor(text) uiStyle.SetSrcBorderColor(text)

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/base64"
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
@ -22,6 +23,11 @@ func SetTermTitle(name string) {
} }
func CopyToClipboard(text string) { func CopyToClipboard(text string) {
if uiStyle.OSC52 {
fmt.Printf("\033]52;c;%s\a", base64.StdEncoding.EncodeToString([]byte(text)))
return
}
var cmd *exec.Cmd var cmd *exec.Cmd
switch runtime.GOOS { switch runtime.GOOS {