mirror of
https://github.com/eeeXun/GTT.git
synced 2025-05-17 00:00:16 -07:00
feat: add definition & part of speech (#5)
This commit is contained in:
parent
e49639fcbe
commit
d99585b386
21
README.md
21
README.md
@ -4,7 +4,7 @@ Google Translate TUI
|
|||||||
|
|
||||||
## ScreenShot
|
## ScreenShot
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
@ -31,28 +31,28 @@ Exit program.
|
|||||||
Toggle pop out window.
|
Toggle pop out window.
|
||||||
|
|
||||||
`<C-j>`
|
`<C-j>`
|
||||||
Translate from left window to right window.
|
Translate from source to destination window.
|
||||||
|
|
||||||
`<C-s>`
|
`<C-s>`
|
||||||
Swap language.
|
Swap language.
|
||||||
|
|
||||||
`<C-q>`
|
`<C-q>`
|
||||||
Clear all text in left window.
|
Clear all text in source of translation window.
|
||||||
|
|
||||||
`<C-y>`
|
`<C-y>`
|
||||||
Copy selected text in left window.
|
Copy selected text.
|
||||||
|
|
||||||
`<C-g>`
|
`<C-g>`
|
||||||
Copy all text in left window.
|
Copy all text in source of translation window.
|
||||||
|
|
||||||
`<C-r>`
|
`<C-r>`
|
||||||
Copy all text in right window.
|
Copy all text in destination of translation window.
|
||||||
|
|
||||||
`<C-o>`
|
`<C-o>`
|
||||||
Play sound on left window.
|
Play sound on source of translation window.
|
||||||
|
|
||||||
`<C-p>`
|
`<C-p>`
|
||||||
Play sound on right window.
|
Play sound on destination of translation window.
|
||||||
|
|
||||||
`<C-x>`
|
`<C-x>`
|
||||||
Stop play sound.
|
Stop play sound.
|
||||||
@ -60,6 +60,9 @@ Stop play sound.
|
|||||||
`<C-t>`
|
`<C-t>`
|
||||||
Toggle transparent.
|
Toggle transparent.
|
||||||
|
|
||||||
|
`<C-\>`
|
||||||
|
Toggle Definition & Part of speech
|
||||||
|
|
||||||
`<Tab>`, `<S-Tab>`
|
`<Tab>`, `<S-Tab>`
|
||||||
Cycle through the pop out widget.
|
Cycle through the pop out widget.
|
||||||
|
|
||||||
@ -74,6 +77,8 @@ Switch pop out window.
|
|||||||
|
|
||||||
## Credit
|
## Credit
|
||||||
|
|
||||||
|
[soimort/translate-shell](https://github.com/soimort/translate-shell) For translation URL.
|
||||||
|
|
||||||
[snsd0805/GoogleTranslate-TUI](https://github.com/snsd0805/GoogleTranslate-TUI) For inspiration.
|
[snsd0805/GoogleTranslate-TUI](https://github.com/snsd0805/GoogleTranslate-TUI) For inspiration.
|
||||||
|
|
||||||
[turk/free-google-translate](https://github.com/turk/free-google-translate) For Google translate in Golang.
|
[turk/free-google-translate](https://github.com/turk/free-google-translate) For Google translate in Golang.
|
||||||
|
14
config.go
14
config.go
@ -2,12 +2,20 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"gtt/internal/color"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
// argument
|
||||||
srcLangArg *string = flag.String("src", "", "Source Language")
|
srcLangArg *string = flag.String("src", "", "Source Language")
|
||||||
dstLangArg *string = flag.String("dst", "", "Destination Language")
|
dstLangArg *string = flag.String("dst", "", "Destination Language")
|
||||||
|
// settings
|
||||||
|
config = viper.New()
|
||||||
|
style = color.NewStyle()
|
||||||
|
hideBelow bool
|
||||||
)
|
)
|
||||||
|
|
||||||
// Search XDG_CONFIG_HOME or $HOME/.config
|
// Search XDG_CONFIG_HOME or $HOME/.config
|
||||||
@ -32,6 +40,7 @@ func configInit() {
|
|||||||
config.Set("source.borderColor", "red")
|
config.Set("source.borderColor", "red")
|
||||||
config.Set("destination.language", "Chinese (Traditional)")
|
config.Set("destination.language", "Chinese (Traditional)")
|
||||||
config.Set("destination.borderColor", "blue")
|
config.Set("destination.borderColor", "blue")
|
||||||
|
config.Set("hide_below", false)
|
||||||
if _, err = os.Stat(defaultConfigPath); os.IsNotExist(err) {
|
if _, err = os.Stat(defaultConfigPath); os.IsNotExist(err) {
|
||||||
os.MkdirAll(defaultConfigPath, os.ModePerm)
|
os.MkdirAll(defaultConfigPath, os.ModePerm)
|
||||||
}
|
}
|
||||||
@ -50,6 +59,7 @@ func configInit() {
|
|||||||
} else {
|
} else {
|
||||||
translator.DstLang = config.GetString("destination.language")
|
translator.DstLang = config.GetString("destination.language")
|
||||||
}
|
}
|
||||||
|
hideBelow = config.GetBool("hide_below")
|
||||||
style.Theme = config.GetString("theme")
|
style.Theme = config.GetString("theme")
|
||||||
style.Transparent = config.GetBool("transparent")
|
style.Transparent = config.GetBool("transparent")
|
||||||
style.SetSrcBorderColor(config.GetString("source.borderColor")).
|
style.SetSrcBorderColor(config.GetString("source.borderColor")).
|
||||||
@ -72,6 +82,10 @@ func updateConfig() {
|
|||||||
changed = true
|
changed = true
|
||||||
config.Set("destination.language", translator.DstLang)
|
config.Set("destination.language", translator.DstLang)
|
||||||
}
|
}
|
||||||
|
if config.GetBool("hide_below") != hideBelow {
|
||||||
|
changed = true
|
||||||
|
config.Set("hide_below", hideBelow)
|
||||||
|
}
|
||||||
if config.GetString("theme") != style.Theme {
|
if config.GetString("theme") != style.Theme {
|
||||||
changed = true
|
changed = true
|
||||||
config.Set("theme", style.Theme)
|
config.Set("theme", style.Theme)
|
||||||
|
@ -14,7 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
textURL = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=%s&tl=%s&dt=t&q=%s"
|
textURL = "https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&dt=bd&dt=md&dt=ex&sl=%s&tl=%s&q=%s"
|
||||||
soundURL = "https://translate.google.com.vn/translate_tts?ie=UTF-8&q=%s&tl=%s&client=tw-ob"
|
soundURL = "https://translate.google.com.vn/translate_tts?ie=UTF-8&q=%s&tl=%s&client=tw-ob"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,7 +30,11 @@ func NewTranslator() *Translator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Translator) Translate(message string) (translation string, err error) {
|
func (t *Translator) Translate(message string) (
|
||||||
|
translation string,
|
||||||
|
definition string,
|
||||||
|
partOfSpeech string,
|
||||||
|
err error) {
|
||||||
var data []interface{}
|
var data []interface{}
|
||||||
|
|
||||||
urlStr := fmt.Sprintf(
|
urlStr := fmt.Sprintf(
|
||||||
@ -41,26 +45,71 @@ func (t *Translator) Translate(message string) (translation string, err error) {
|
|||||||
)
|
)
|
||||||
res, err := http.Get(urlStr)
|
res, err := http.Get(urlStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", "", "", err
|
||||||
}
|
}
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = json.Unmarshal(body, &data); err != nil {
|
if err = json.Unmarshal(body, &data); err != nil {
|
||||||
return "", err
|
return "", "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(data) > 0 {
|
if len(data) > 0 {
|
||||||
|
// translation = data[0]
|
||||||
for _, lines := range data[0].([]interface{}) {
|
for _, lines := range data[0].([]interface{}) {
|
||||||
translatedLine := lines.([]interface{})[0]
|
translatedLine := lines.([]interface{})[0]
|
||||||
translation += fmt.Sprintf("%v", translatedLine)
|
translation += fmt.Sprintf("%v", translatedLine)
|
||||||
}
|
}
|
||||||
return translation, nil
|
|
||||||
|
// part of speech = data[1]
|
||||||
|
if data[1] != nil {
|
||||||
|
for _, parts := range data[1].([]interface{}) {
|
||||||
|
// part of speech
|
||||||
|
part := parts.([]interface{})[0]
|
||||||
|
partOfSpeech += fmt.Sprintf("[%v]\n", part)
|
||||||
|
for _, words := range parts.([]interface{})[2].([]interface{}) {
|
||||||
|
// dst lang
|
||||||
|
dstWord := words.([]interface{})[0]
|
||||||
|
partOfSpeech += fmt.Sprintf("\t%v:", dstWord)
|
||||||
|
// src lang
|
||||||
|
firstWord := true
|
||||||
|
for _, word := range words.([]interface{})[1].([]interface{}) {
|
||||||
|
if firstWord {
|
||||||
|
partOfSpeech += fmt.Sprintf(" %v", word)
|
||||||
|
firstWord = false
|
||||||
|
} else {
|
||||||
|
partOfSpeech += fmt.Sprintf(", %v", word)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
partOfSpeech += "\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", errors.New("Translation not found")
|
// definition = data[12]
|
||||||
|
if len(data) >= 13 && data[12] != nil {
|
||||||
|
for _, parts := range data[12].([]interface{}) {
|
||||||
|
// part of speech
|
||||||
|
part := parts.([]interface{})[0]
|
||||||
|
definition += fmt.Sprintf("[%v]\n", part)
|
||||||
|
for _, sentences := range parts.([]interface{})[1].([]interface{}) {
|
||||||
|
// definition
|
||||||
|
def := sentences.([]interface{})[0]
|
||||||
|
definition += fmt.Sprintf("\t- %v\n", def)
|
||||||
|
// example sentence
|
||||||
|
if len(sentences.([]interface{})) >= 3 && sentences.([]interface{})[2] != nil {
|
||||||
|
example := sentences.([]interface{})[2]
|
||||||
|
definition += fmt.Sprintf("\t\t\"%v\"\n", example)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return translation, definition, partOfSpeech, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", "", "", errors.New("Translation not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Translator) PlaySound(lang string, message string) error {
|
func (t *Translator) PlaySound(lang string, message string) error {
|
||||||
|
17
main.go
17
main.go
@ -1,12 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"gtt/internal/color"
|
|
||||||
"gtt/internal/translate"
|
"gtt/internal/translate"
|
||||||
"gtt/internal/ui"
|
"gtt/internal/ui"
|
||||||
|
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
"github.com/spf13/viper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -16,26 +14,33 @@ var (
|
|||||||
app = tview.NewApplication()
|
app = tview.NewApplication()
|
||||||
srcInput = tview.NewTextArea()
|
srcInput = tview.NewTextArea()
|
||||||
dstOutput = tview.NewTextView()
|
dstOutput = tview.NewTextView()
|
||||||
|
defOutput = tview.NewTextArea()
|
||||||
|
posOutput = tview.NewTextArea()
|
||||||
srcLangDropDown = tview.NewDropDown()
|
srcLangDropDown = tview.NewDropDown()
|
||||||
dstLangDropDown = tview.NewDropDown()
|
dstLangDropDown = tview.NewDropDown()
|
||||||
langCycle = ui.NewUICycle(srcLangDropDown, dstLangDropDown)
|
langCycle = ui.NewUICycle(srcLangDropDown, dstLangDropDown)
|
||||||
themeDropDown = tview.NewDropDown()
|
themeDropDown = tview.NewDropDown()
|
||||||
transparentDropDown = tview.NewDropDown()
|
transparentDropDown = tview.NewDropDown()
|
||||||
|
hideBelowDropDown = tview.NewDropDown()
|
||||||
srcBorderDropDown = tview.NewDropDown()
|
srcBorderDropDown = tview.NewDropDown()
|
||||||
dstBorderDropDown = tview.NewDropDown()
|
dstBorderDropDown = tview.NewDropDown()
|
||||||
styleCycle = ui.NewUICycle(themeDropDown, transparentDropDown, srcBorderDropDown, dstBorderDropDown)
|
styleCycle = ui.NewUICycle(
|
||||||
|
themeDropDown,
|
||||||
|
transparentDropDown,
|
||||||
|
hideBelowDropDown,
|
||||||
|
srcBorderDropDown,
|
||||||
|
dstBorderDropDown)
|
||||||
keyMapMenu = tview.NewTextView()
|
keyMapMenu = tview.NewTextView()
|
||||||
langButton = tview.NewButton("(1)Language")
|
langButton = tview.NewButton("(1)Language")
|
||||||
styleButton = tview.NewButton("(2)Style")
|
styleButton = tview.NewButton("(2)Style")
|
||||||
keyMapButton = tview.NewButton("(3)KeyMap")
|
keyMapButton = tview.NewButton("(3)KeyMap")
|
||||||
translateWindow = tview.NewFlex()
|
translateWindow = tview.NewFlex()
|
||||||
|
translateAboveWidget = tview.NewFlex()
|
||||||
|
translateBelowWidget = tview.NewFlex()
|
||||||
langWindow = tview.NewFlex()
|
langWindow = tview.NewFlex()
|
||||||
styleWindow = tview.NewFlex()
|
styleWindow = tview.NewFlex()
|
||||||
keyMapWindow = tview.NewFlex()
|
keyMapWindow = tview.NewFlex()
|
||||||
mainPage = tview.NewPages()
|
mainPage = tview.NewPages()
|
||||||
// settings
|
|
||||||
config = viper.New()
|
|
||||||
style = color.NewStyle()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
107
ui.go
107
ui.go
@ -18,31 +18,44 @@ const (
|
|||||||
[#%[1]s]<Esc>[-]
|
[#%[1]s]<Esc>[-]
|
||||||
Toggle pop out window.
|
Toggle pop out window.
|
||||||
[#%[1]s]<C-j>[-]
|
[#%[1]s]<C-j>[-]
|
||||||
Translate from left window to right window.
|
Translate from source to destination window.
|
||||||
[#%[1]s]<C-s>[-]
|
[#%[1]s]<C-s>[-]
|
||||||
Swap language.
|
Swap language.
|
||||||
[#%[1]s]<C-q>[-]
|
[#%[1]s]<C-q>[-]
|
||||||
Clear all text in left window.
|
Clear all text in source of translation window.
|
||||||
[#%[1]s]<C-y>[-]
|
[#%[1]s]<C-y>[-]
|
||||||
Copy selected text in left window.
|
Copy selected text.
|
||||||
[#%[1]s]<C-g>[-]
|
[#%[1]s]<C-g>[-]
|
||||||
Copy all text in left window.
|
Copy all text in source of translation window.
|
||||||
[#%[1]s]<C-r>[-]
|
[#%[1]s]<C-r>[-]
|
||||||
Copy all text in right window.
|
Copy all text in destination of translation window.
|
||||||
[#%[1]s]<C-o>[-]
|
[#%[1]s]<C-o>[-]
|
||||||
Play sound on left window.
|
Play sound on source of translation window.
|
||||||
[#%[1]s]<C-p>[-]
|
[#%[1]s]<C-p>[-]
|
||||||
Play sound on right window.
|
Play sound on destination of translation window.
|
||||||
[#%[1]s]<C-x>[-]
|
[#%[1]s]<C-x>[-]
|
||||||
Stop play sound.
|
Stop play sound.
|
||||||
[#%[1]s]<C-t>[-]
|
[#%[1]s]<C-t>[-]
|
||||||
Toggle transparent.
|
Toggle transparent.
|
||||||
|
[#%[1]s]<C-\>[-]
|
||||||
|
Toggle Definition & 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.
|
||||||
[#%[1]s]<1>, <2>, <3>[-]
|
[#%[1]s]<1>, <2>, <3>[-]
|
||||||
Switch pop out window.`
|
Switch pop out window.`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func updateTranslateWindow() {
|
||||||
|
translateWindow.Clear()
|
||||||
|
if hideBelow {
|
||||||
|
translateWindow.AddItem(translateAboveWidget, 0, 1, true)
|
||||||
|
} else {
|
||||||
|
translateWindow.SetDirection(tview.FlexRow).
|
||||||
|
AddItem(translateAboveWidget, 0, 1, true).
|
||||||
|
AddItem(translateBelowWidget, 0, 1, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func updateBackgroundColor() {
|
func updateBackgroundColor() {
|
||||||
// input/output
|
// input/output
|
||||||
srcInput.SetTextStyle(tcell.StyleDefault.
|
srcInput.SetTextStyle(tcell.StyleDefault.
|
||||||
@ -50,6 +63,14 @@ func updateBackgroundColor() {
|
|||||||
Foreground(style.ForegroundColor())).
|
Foreground(style.ForegroundColor())).
|
||||||
SetBackgroundColor(style.BackgroundColor())
|
SetBackgroundColor(style.BackgroundColor())
|
||||||
dstOutput.SetBackgroundColor(style.BackgroundColor())
|
dstOutput.SetBackgroundColor(style.BackgroundColor())
|
||||||
|
defOutput.SetTextStyle(tcell.StyleDefault.
|
||||||
|
Background(style.BackgroundColor()).
|
||||||
|
Foreground(style.ForegroundColor())).
|
||||||
|
SetBackgroundColor(style.BackgroundColor())
|
||||||
|
posOutput.SetTextStyle(tcell.StyleDefault.
|
||||||
|
Background(style.BackgroundColor()).
|
||||||
|
Foreground(style.ForegroundColor())).
|
||||||
|
SetBackgroundColor(style.BackgroundColor())
|
||||||
|
|
||||||
// dropdown
|
// dropdown
|
||||||
for _, dropdown := range []*tview.DropDown{
|
for _, dropdown := range []*tview.DropDown{
|
||||||
@ -57,6 +78,7 @@ func updateBackgroundColor() {
|
|||||||
dstLangDropDown,
|
dstLangDropDown,
|
||||||
themeDropDown,
|
themeDropDown,
|
||||||
transparentDropDown,
|
transparentDropDown,
|
||||||
|
hideBelowDropDown,
|
||||||
srcBorderDropDown,
|
srcBorderDropDown,
|
||||||
dstBorderDropDown} {
|
dstBorderDropDown} {
|
||||||
dropdown.SetListStyles(tcell.StyleDefault.
|
dropdown.SetListStyles(tcell.StyleDefault.
|
||||||
@ -78,6 +100,10 @@ func updateBorderColor() {
|
|||||||
SetTitleColor(style.SrcBorderColor())
|
SetTitleColor(style.SrcBorderColor())
|
||||||
dstOutput.SetBorderColor(style.DstBorderColor()).
|
dstOutput.SetBorderColor(style.DstBorderColor()).
|
||||||
SetTitleColor(style.DstBorderColor())
|
SetTitleColor(style.DstBorderColor())
|
||||||
|
defOutput.SetBorderColor(style.SrcBorderColor()).
|
||||||
|
SetTitleColor(style.SrcBorderColor())
|
||||||
|
posOutput.SetBorderColor(style.DstBorderColor()).
|
||||||
|
SetTitleColor(style.DstBorderColor())
|
||||||
|
|
||||||
// dropdown
|
// dropdown
|
||||||
for _, srcDropDown := range []*tview.DropDown{srcLangDropDown, srcBorderDropDown} {
|
for _, srcDropDown := range []*tview.DropDown{srcLangDropDown, srcBorderDropDown} {
|
||||||
@ -96,6 +122,12 @@ func updateNonConfigColor() {
|
|||||||
Background(style.SelectedColor()).
|
Background(style.SelectedColor()).
|
||||||
Foreground(style.ForegroundColor()))
|
Foreground(style.ForegroundColor()))
|
||||||
dstOutput.SetTextColor(style.ForegroundColor())
|
dstOutput.SetTextColor(style.ForegroundColor())
|
||||||
|
defOutput.SetSelectedStyle(tcell.StyleDefault.
|
||||||
|
Background(style.SelectedColor()).
|
||||||
|
Foreground(style.ForegroundColor()))
|
||||||
|
posOutput.SetSelectedStyle(tcell.StyleDefault.
|
||||||
|
Background(style.SelectedColor()).
|
||||||
|
Foreground(style.ForegroundColor()))
|
||||||
|
|
||||||
// dropdown
|
// dropdown
|
||||||
for _, noLabelDropDown := range []*tview.DropDown{srcLangDropDown, dstLangDropDown} {
|
for _, noLabelDropDown := range []*tview.DropDown{srcLangDropDown, dstLangDropDown} {
|
||||||
@ -106,6 +138,7 @@ func updateNonConfigColor() {
|
|||||||
for _, labelDropDown := range []*tview.DropDown{
|
for _, labelDropDown := range []*tview.DropDown{
|
||||||
themeDropDown,
|
themeDropDown,
|
||||||
transparentDropDown,
|
transparentDropDown,
|
||||||
|
hideBelowDropDown,
|
||||||
srcBorderDropDown,
|
srcBorderDropDown,
|
||||||
dstBorderDropDown} {
|
dstBorderDropDown} {
|
||||||
labelDropDown.SetLabelColor(style.LabelColor()).
|
labelDropDown.SetLabelColor(style.LabelColor()).
|
||||||
@ -166,6 +199,8 @@ func uiInit() {
|
|||||||
// input/output
|
// input/output
|
||||||
srcInput.SetBorder(true)
|
srcInput.SetBorder(true)
|
||||||
dstOutput.SetBorder(true)
|
dstOutput.SetBorder(true)
|
||||||
|
defOutput.SetBorder(true).SetTitle("Definition")
|
||||||
|
posOutput.SetBorder(true).SetTitle("Part of speech")
|
||||||
|
|
||||||
// dropdown
|
// dropdown
|
||||||
for _, langDropDown := range []*tview.DropDown{srcLangDropDown, dstLangDropDown} {
|
for _, langDropDown := range []*tview.DropDown{srcLangDropDown, dstLangDropDown} {
|
||||||
@ -175,6 +210,11 @@ func uiInit() {
|
|||||||
themeDropDown.SetLabel("Theme: ").
|
themeDropDown.SetLabel("Theme: ").
|
||||||
SetOptions(color.AllTheme, nil).
|
SetOptions(color.AllTheme, nil).
|
||||||
SetCurrentOption(IndexOf(style.Theme, color.AllTheme))
|
SetCurrentOption(IndexOf(style.Theme, color.AllTheme))
|
||||||
|
hideBelowDropDown.SetLabel("Hide below: ").
|
||||||
|
SetOptions([]string{"true", "false"}, nil).
|
||||||
|
SetCurrentOption(
|
||||||
|
IndexOf(strconv.FormatBool(hideBelow),
|
||||||
|
[]string{"true", "false"}))
|
||||||
transparentDropDown.SetLabel("Transparent: ").
|
transparentDropDown.SetLabel("Transparent: ").
|
||||||
SetOptions([]string{"true", "false"}, nil).
|
SetOptions([]string{"true", "false"}, nil).
|
||||||
SetCurrentOption(
|
SetCurrentOption(
|
||||||
@ -203,9 +243,13 @@ func uiInit() {
|
|||||||
SetTitle("Key Map")
|
SetTitle("Key Map")
|
||||||
|
|
||||||
// window
|
// window
|
||||||
translateWindow.SetDirection(tview.FlexColumn).
|
translateAboveWidget.SetDirection(tview.FlexColumn).
|
||||||
AddItem(srcInput, 0, 1, true).
|
AddItem(srcInput, 0, 1, true).
|
||||||
AddItem(dstOutput, 0, 1, false)
|
AddItem(dstOutput, 0, 1, false)
|
||||||
|
translateBelowWidget.SetDirection(tview.FlexColumn).
|
||||||
|
AddItem(defOutput, 0, 1, false).
|
||||||
|
AddItem(posOutput, 0, 1, false)
|
||||||
|
updateTranslateWindow()
|
||||||
langWindow.SetDirection(tview.FlexRow).
|
langWindow.SetDirection(tview.FlexRow).
|
||||||
AddItem(nil, 0, 1, false).
|
AddItem(nil, 0, 1, false).
|
||||||
AddItem(tview.NewFlex().SetDirection(tview.FlexColumn).
|
AddItem(tview.NewFlex().SetDirection(tview.FlexColumn).
|
||||||
@ -225,10 +269,11 @@ func uiInit() {
|
|||||||
AddItem(nil, 0, 1, false).
|
AddItem(nil, 0, 1, false).
|
||||||
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
|
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
|
||||||
AddItem(themeDropDown, 0, 1, true).
|
AddItem(themeDropDown, 0, 1, true).
|
||||||
AddItem(transparentDropDown, 0, 1, false),
|
AddItem(transparentDropDown, 0, 1, false).
|
||||||
|
AddItem(hideBelowDropDown, 0, 1, false),
|
||||||
0, 1, true).
|
0, 1, true).
|
||||||
AddItem(nil, 0, 1, false),
|
AddItem(nil, 0, 1, false),
|
||||||
2, 1, true).
|
3, 1, true).
|
||||||
AddItem(tview.NewFlex().SetDirection(tview.FlexColumn).
|
AddItem(tview.NewFlex().SetDirection(tview.FlexColumn).
|
||||||
AddItem(srcBorderDropDown, 0, 1, false).
|
AddItem(srcBorderDropDown, 0, 1, false).
|
||||||
AddItem(dstBorderDropDown, 0, 1, false),
|
AddItem(dstBorderDropDown, 0, 1, false),
|
||||||
@ -254,6 +299,25 @@ func uiInit() {
|
|||||||
// handler
|
// handler
|
||||||
mainPage.SetInputCapture(mainPageHandler)
|
mainPage.SetInputCapture(mainPageHandler)
|
||||||
translateWindow.SetInputCapture(translateWindowHandler)
|
translateWindow.SetInputCapture(translateWindowHandler)
|
||||||
|
for _, widget := range []*tview.TextArea{srcInput, defOutput, posOutput} {
|
||||||
|
// fix for loop problem
|
||||||
|
// https://github.com/golang/go/discussions/56010
|
||||||
|
widget := widget
|
||||||
|
widget.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
||||||
|
key := event.Key()
|
||||||
|
switch key {
|
||||||
|
case tcell.KeyCtrlY:
|
||||||
|
// copy selected text
|
||||||
|
text, _, _ := widget.GetSelection()
|
||||||
|
|
||||||
|
// only copy when text selected
|
||||||
|
if len(text) > 0 {
|
||||||
|
CopyToClipboard(text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return event
|
||||||
|
})
|
||||||
|
}
|
||||||
langWindow.SetInputCapture(popOutWindowHandler)
|
langWindow.SetInputCapture(popOutWindowHandler)
|
||||||
styleWindow.SetInputCapture(popOutWindowHandler)
|
styleWindow.SetInputCapture(popOutWindowHandler)
|
||||||
keyMapWindow.SetInputCapture(popOutWindowHandler)
|
keyMapWindow.SetInputCapture(popOutWindowHandler)
|
||||||
@ -279,6 +343,11 @@ func uiInit() {
|
|||||||
style.Transparent, _ = strconv.ParseBool(text)
|
style.Transparent, _ = strconv.ParseBool(text)
|
||||||
updateBackgroundColor()
|
updateBackgroundColor()
|
||||||
})
|
})
|
||||||
|
hideBelowDropDown.SetDoneFunc(styleDropDownHandler).
|
||||||
|
SetSelectedFunc(func(text string, index int) {
|
||||||
|
hideBelow, _ = strconv.ParseBool(text)
|
||||||
|
updateTranslateWindow()
|
||||||
|
})
|
||||||
srcBorderDropDown.SetDoneFunc(styleDropDownHandler).
|
srcBorderDropDown.SetDoneFunc(styleDropDownHandler).
|
||||||
SetSelectedFunc(func(text string, index int) {
|
SetSelectedFunc(func(text string, index int) {
|
||||||
style.SetSrcBorderColor(text)
|
style.SetSrcBorderColor(text)
|
||||||
@ -325,6 +394,12 @@ func mainPageHandler(event *tcell.EventKey) *tcell.EventKey {
|
|||||||
transparentDropDown.SetCurrentOption(
|
transparentDropDown.SetCurrentOption(
|
||||||
IndexOf(strconv.FormatBool(style.Transparent),
|
IndexOf(strconv.FormatBool(style.Transparent),
|
||||||
[]string{"true", "false"}))
|
[]string{"true", "false"}))
|
||||||
|
case tcell.KeyCtrlBackslash:
|
||||||
|
hideBelow = !hideBelow
|
||||||
|
updateTranslateWindow()
|
||||||
|
hideBelowDropDown.SetCurrentOption(
|
||||||
|
IndexOf(strconv.FormatBool(hideBelow),
|
||||||
|
[]string{"true", "false"}))
|
||||||
}
|
}
|
||||||
|
|
||||||
return event
|
return event
|
||||||
@ -341,23 +416,17 @@ func translateWindowHandler(event *tcell.EventKey) *tcell.EventKey {
|
|||||||
message := srcInput.GetText()
|
message := srcInput.GetText()
|
||||||
// Only translate when message exist
|
// Only translate when message exist
|
||||||
if len(message) > 0 {
|
if len(message) > 0 {
|
||||||
translation, err := translator.Translate(message)
|
translation, definition, partOfSpeech, err := translator.Translate(message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dstOutput.SetText(err.Error())
|
dstOutput.SetText(err.Error())
|
||||||
} else {
|
} else {
|
||||||
dstOutput.SetText(translation)
|
dstOutput.SetText(translation)
|
||||||
|
defOutput.SetText(definition, false)
|
||||||
|
posOutput.SetText(partOfSpeech, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case tcell.KeyCtrlQ:
|
case tcell.KeyCtrlQ:
|
||||||
srcInput.SetText("", true)
|
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:
|
case tcell.KeyCtrlG:
|
||||||
// copy all text in Input
|
// copy all text in Input
|
||||||
text := srcInput.GetText()
|
text := srcInput.GetText()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user