diff --git a/README.md b/README.md index 7ff6013..ee68993 100644 --- a/README.md +++ b/README.md @@ -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. +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)) ```sh @@ -120,7 +122,7 @@ docker run -it eeexun/gtt:latest Exit program. `` -Toggle pop out window. +Toggle pop out menu. `` Translate from source to destination window. @@ -159,7 +161,7 @@ Toggle Definition/Example & Part of speech. Cycle through the pop out widget. `<1>`, `<2>`, `<3>` -Switch pop out window. +Switch pop out menu. ### Customize key map diff --git a/config.go b/config.go index 24d90d9..b5882c2 100644 --- a/config.go +++ b/config.go @@ -37,6 +37,7 @@ func configInit() { "toggle_below": "C-\\", } defaultConfig = map[string]interface{}{ + "osc52": false, "hide_below": false, "transparent": false, "theme": "gruvbox", @@ -153,6 +154,7 @@ func configInit() { } translator = translators[config.GetString("translator")] uiStyle.Theme = config.GetString("theme") + uiStyle.OSC52 = config.GetBool("osc52") uiStyle.HideBelow = config.GetBool("hide_below") uiStyle.Transparent = config.GetBool("transparent") uiStyle.SetSrcBorderColor(config.GetString("source.border_color")). @@ -211,10 +213,6 @@ func updateConfig() { changed = true 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 { changed = true config.Set("theme", uiStyle.Theme) @@ -223,6 +221,14 @@ func updateConfig() { changed = true 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() { changed = true config.Set("source.border_color", uiStyle.SrcBorderStr()) diff --git a/internal/style/style.go b/internal/style/style.go index a55429f..5704434 100644 --- a/internal/style/style.go +++ b/internal/style/style.go @@ -5,6 +5,7 @@ import ( ) type style struct { + OSC52 bool HideBelow bool Transparent bool Theme string diff --git a/main.go b/main.go index 4881522..db48da4 100644 --- a/main.go +++ b/main.go @@ -35,12 +35,14 @@ var ( themeDropDown = tview.NewDropDown() transparentDropDown = tview.NewDropDown() hideBelowDropDown = tview.NewDropDown() + osc52DropDown = tview.NewDropDown() srcBorderDropDown = tview.NewDropDown() dstBorderDropDown = tview.NewDropDown() styleCycle = ui.NewUICycle( themeDropDown, transparentDropDown, hideBelowDropDown, + osc52DropDown, srcBorderDropDown, dstBorderDropDown) keyMapMenu = tview.NewTextView() diff --git a/ui.go b/ui.go index 88dde0a..db2d09f 100644 --- a/ui.go +++ b/ui.go @@ -18,12 +18,12 @@ type Item struct { } const ( - popOutWindowHeight int = 20 + popOutMenuHeight int = 20 langStrMaxLength int = 32 keyMapText string = `[#%[1]s][-] Exit program. [#%[1]s][-] - Toggle pop out window. + Toggle pop out menu. [#%[1]s]<%[2]s>[-] Translate from source to destination window. [#%[1]s]<%[3]s>[-] @@ -49,7 +49,7 @@ const ( [#%[1]s], [-] Cycle through the pop out widget. [#%[1]s]<1>, <2>, <3>[-] - Switch pop out window.` + Switch pop out menu.` ) func updateTranslateWindow() { @@ -84,6 +84,7 @@ func updateBackgroundColor() { themeDropDown, transparentDropDown, hideBelowDropDown, + osc52DropDown, srcBorderDropDown, dstBorderDropDown} { dropdown.SetListStyles(tcell.StyleDefault. @@ -145,6 +146,7 @@ func updateNonConfigColor() { themeDropDown, transparentDropDown, hideBelowDropDown, + osc52DropDown, srcBorderDropDown, dstBorderDropDown} { labelDropDown.SetLabelColor(uiStyle.LabelColor()). @@ -286,15 +288,20 @@ func uiInit() { themeDropDown.SetLabel("Theme: "). SetOptions(style.AllTheme, nil). 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: "). SetOptions([]string{"true", "false"}, nil). SetCurrentOption( IndexOf(strconv.FormatBool(uiStyle.HideBelow), []string{"true", "false"})) - transparentDropDown.SetLabel("Transparent: "). + osc52DropDown.SetLabel("OSC 52: "). SetOptions([]string{"true", "false"}, nil). SetCurrentOption( - IndexOf(strconv.FormatBool(uiStyle.Transparent), + IndexOf(strconv.FormatBool(uiStyle.OSC52), []string{"true", "false"})) srcBorderDropDown.SetLabel("Border Color: "). SetOptions(style.Palette, nil). @@ -340,7 +347,7 @@ func uiInit() { Item{item: dstLangDropDown, fixedSize: 0, proportion: 1, focus: false}), fixedSize: 0, proportion: 1, focus: true}), fixedSize: 2 * langStrMaxLength, proportion: 1, focus: true}), - popOutWindowHeight, 1, true). + popOutMenuHeight, 1, true). AddItem(attachButton(), 1, 1, false). AddItem(nil, 0, 1, false) stylePopOut.SetDirection(tview.FlexRow). @@ -351,22 +358,23 @@ func uiInit() { Item{item: attachItems(false, tview.FlexRow, Item{item: themeDropDown, fixedSize: 0, proportion: 1, focus: true}, 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: 3, proportion: 1, focus: true}, + fixedSize: 4, proportion: 1, focus: true}, Item{item: attachItems(false, tview.FlexColumn, Item{item: srcBorderDropDown, fixedSize: 0, proportion: 1, focus: false}, Item{item: dstBorderDropDown, fixedSize: 0, proportion: 1, focus: false}), fixedSize: 0, proportion: 1, focus: false}), fixedSize: 2 * langStrMaxLength, proportion: 1, focus: true}), - popOutWindowHeight, 1, true). + popOutMenuHeight, 1, true). AddItem(attachButton(), 1, 1, false). AddItem(nil, 0, 1, false) keyMapPopOut.SetDirection(tview.FlexRow). AddItem(nil, 0, 1, false). AddItem(attachItems(true, tview.FlexColumn, Item{item: keyMapMenu, fixedSize: 2 * langStrMaxLength, proportion: 1, focus: true}), - popOutWindowHeight, 1, true). + popOutMenuHeight, 1, true). AddItem(attachButton(), 1, 1, false). AddItem(nil, 0, 1, false) @@ -429,6 +437,10 @@ func uiInit() { uiStyle.HideBelow, _ = strconv.ParseBool(text) updateTranslateWindow() }) + osc52DropDown.SetDoneFunc(styleDropDownHandler). + SetSelectedFunc(func(text string, index int) { + uiStyle.OSC52, _ = strconv.ParseBool(text) + }) srcBorderDropDown.SetDoneFunc(styleDropDownHandler). SetSelectedFunc(func(text string, index int) { uiStyle.SetSrcBorderColor(text) diff --git a/utils.go b/utils.go index 7354053..b57f899 100644 --- a/utils.go +++ b/utils.go @@ -1,6 +1,7 @@ package main import ( + "encoding/base64" "fmt" "os" "os/exec" @@ -22,6 +23,11 @@ func SetTermTitle(name 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 switch runtime.GOOS {