Skip to content

Commit

Permalink
feat: support OSC 52 (#32)
Browse files Browse the repository at this point in the history
* feat: support OSC 52

* docs: note for OSC 52
  • Loading branch information
eeeXun authored Aug 10, 2024
1 parent 06dafb5 commit f87b7b4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -120,7 +122,7 @@ docker run -it eeexun/gtt:latest
Exit program.

`<Esc>`
Toggle pop out window.
Toggle pop out menu.

`<C-j>`
Translate from source to destination window.
Expand Down Expand Up @@ -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

Expand Down
14 changes: 10 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func configInit() {
"toggle_below": "C-\\",
}
defaultConfig = map[string]interface{}{
"osc52": false,
"hide_below": false,
"transparent": false,
"theme": "gruvbox",
Expand Down Expand Up @@ -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")).
Expand Down Expand Up @@ -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)
Expand All @@ -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())
Expand Down
1 change: 1 addition & 0 deletions internal/style/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

type style struct {
OSC52 bool
HideBelow bool
Transparent bool
Theme string
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
32 changes: 22 additions & 10 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ type Item struct {
}

const (
popOutWindowHeight int = 20
popOutMenuHeight int = 20
langStrMaxLength int = 32
keyMapText string = `[#%[1]s]<C-c>[-]
Exit program.
[#%[1]s]<Esc>[-]
Toggle pop out window.
Toggle pop out menu.
[#%[1]s]<%[2]s>[-]
Translate from source to destination window.
[#%[1]s]<%[3]s>[-]
Expand All @@ -49,7 +49,7 @@ const (
[#%[1]s]<Tab>, <S-Tab>[-]
Cycle through the pop out widget.
[#%[1]s]<1>, <2>, <3>[-]
Switch pop out window.`
Switch pop out menu.`
)

func updateTranslateWindow() {
Expand Down Expand Up @@ -84,6 +84,7 @@ func updateBackgroundColor() {
themeDropDown,
transparentDropDown,
hideBelowDropDown,
osc52DropDown,
srcBorderDropDown,
dstBorderDropDown} {
dropdown.SetListStyles(tcell.StyleDefault.
Expand Down Expand Up @@ -145,6 +146,7 @@ func updateNonConfigColor() {
themeDropDown,
transparentDropDown,
hideBelowDropDown,
osc52DropDown,
srcBorderDropDown,
dstBorderDropDown} {
labelDropDown.SetLabelColor(uiStyle.LabelColor()).
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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).
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/base64"
"fmt"
"os"
"os/exec"
Expand All @@ -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 {
Expand Down

0 comments on commit f87b7b4

Please sign in to comment.