From df42efc81dfd06b38d6e1ec8627bd7f2e00833c7 Mon Sep 17 00:00:00 2001 From: eeeXun Date: Mon, 13 Feb 2023 21:09:48 +0800 Subject: [PATCH 1/5] refactor: declare data as `map[string]interface` instead of `interface` data in Translate function of ApertiumTranslate and ArgosTranslate is a map[string]interface, so no need to declare it as interface then convert it to map[string]interface --- internal/translate/apertiumtranslate/translator.go | 6 +++--- internal/translate/argostranslate/translator.go | 7 +++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/internal/translate/apertiumtranslate/translator.go b/internal/translate/apertiumtranslate/translator.go index cc8b464..af7d891 100644 --- a/internal/translate/apertiumtranslate/translator.go +++ b/internal/translate/apertiumtranslate/translator.go @@ -51,7 +51,7 @@ func (t *ApertiumTranslate) SwapLang() { } func (t *ApertiumTranslate) Translate(message string) (translation, definition, partOfSpeech string, err error) { - var data interface{} + var data map[string]interface{} urlStr := fmt.Sprintf( textURL, @@ -71,11 +71,11 @@ func (t *ApertiumTranslate) Translate(message string) (translation, definition, return "", "", "", err } - if len(data.(map[string]interface{})) > 0 { + if len(data) > 0 { switch res.StatusCode { case 200: translation += fmt.Sprintf("%v", - data.(map[string]interface{})["responseData"].(map[string]interface{})["translatedText"]) + data["responseData"].(map[string]interface{})["translatedText"]) default: return "", "", "", errors.New( fmt.Sprintf("%s does not support translate from %s to %s.\nSee available pair on %s", diff --git a/internal/translate/argostranslate/translator.go b/internal/translate/argostranslate/translator.go index 7afcc86..4703e0b 100644 --- a/internal/translate/argostranslate/translator.go +++ b/internal/translate/argostranslate/translator.go @@ -51,7 +51,7 @@ func (t *ArgosTranslate) SwapLang() { } func (t *ArgosTranslate) Translate(message string) (translation, definition, partOfSpeech string, err error) { - var data interface{} + var data map[string]interface{} res, err := http.PostForm(textURL, url.Values{ @@ -70,9 +70,8 @@ func (t *ArgosTranslate) Translate(message string) (translation, definition, par return "", "", "", err } - if len(data.(map[string]interface{})) > 0 { - translation += fmt.Sprintf("%v", - data.(map[string]interface{})["translatedText"]) + if len(data) > 0 { + translation += fmt.Sprintf("%v", data["translatedText"]) return translation, definition, partOfSpeech, nil } From c5cdbcb99a115c856aa6dbc22bfc84a37adbf24c Mon Sep 17 00:00:00 2001 From: eeeXun Date: Tue, 14 Feb 2023 15:25:40 +0800 Subject: [PATCH 2/5] refactor: move module color to style - rename control.go to style.go - remove windowStyle struct, don't need it - rename global variable style to uiStyle --- config.go | 26 +++--- internal/color/control.go | 92 -------------------- internal/{color => style}/color.go | 2 +- internal/style/style.go | 88 +++++++++++++++++++ ui.go | 134 ++++++++++++++--------------- 5 files changed, 169 insertions(+), 173 deletions(-) delete mode 100644 internal/color/control.go rename internal/{color => style}/color.go (98%) create mode 100644 internal/style/style.go diff --git a/config.go b/config.go index 5fef13f..a84dbe9 100644 --- a/config.go +++ b/config.go @@ -4,14 +4,14 @@ import ( "fmt" "os" - "github.com/eeeXun/gtt/internal/color" + "github.com/eeeXun/gtt/internal/style" "github.com/eeeXun/gtt/internal/translate" config "github.com/spf13/viper" ) var ( // settings - style = color.NewStyle() + uiStyle = style.NewStyle() hideBelow bool ) @@ -78,9 +78,9 @@ func configInit() { } translator = translators[config.GetString("translator")] hideBelow = config.GetBool("hide_below") - style.Theme = config.GetString("theme") - style.Transparent = config.GetBool("transparent") - style.SetSrcBorderColor(config.GetString("source.borderColor")). + uiStyle.Theme = config.GetString("theme") + uiStyle.Transparent = config.GetBool("transparent") + uiStyle.SetSrcBorderColor(config.GetString("source.borderColor")). SetDstBorderColor(config.GetString("destination.borderColor")) // set argument language if len(*srcLangArg) > 0 { @@ -121,21 +121,21 @@ func updateConfig() { changed = true config.Set("hide_below", hideBelow) } - if config.GetString("theme") != style.Theme { + if config.GetString("theme") != uiStyle.Theme { changed = true - config.Set("theme", style.Theme) + config.Set("theme", uiStyle.Theme) } - if config.GetBool("transparent") != style.Transparent { + if config.GetBool("transparent") != uiStyle.Transparent { changed = true - config.Set("transparent", style.Transparent) + config.Set("transparent", uiStyle.Transparent) } - if config.GetString("source.borderColor") != style.SrcBorderStr() { + if config.GetString("source.borderColor") != uiStyle.SrcBorderStr() { changed = true - config.Set("source.borderColor", style.SrcBorderStr()) + config.Set("source.borderColor", uiStyle.SrcBorderStr()) } - if config.GetString("destination.borderColor") != style.DstBorderStr() { + if config.GetString("destination.borderColor") != uiStyle.DstBorderStr() { changed = true - config.Set("destination.borderColor", style.DstBorderStr()) + config.Set("destination.borderColor", uiStyle.DstBorderStr()) } if changed { diff --git a/internal/color/control.go b/internal/color/control.go deleted file mode 100644 index b793d05..0000000 --- a/internal/color/control.go +++ /dev/null @@ -1,92 +0,0 @@ -package color - -import ( - "github.com/gdamore/tcell/v2" -) - -type windowStyle struct { - borderColor string -} - -type Style struct { - src windowStyle - dst windowStyle - backgroundColor string - foregroundColor string - selectedColor string - prefixColor string - labelColor string - pressColor string - highLightColor string - Theme string - Transparent bool -} - -func NewStyle() *Style { - return &Style{ - backgroundColor: "bg", - foregroundColor: "fg", - selectedColor: "gray", - prefixColor: "cyan", - labelColor: "yellow", - pressColor: "purple", - highLightColor: "orange", - } -} - -func (s Style) BackgroundColor() tcell.Color { - if s.Transparent { - return tcell.ColorDefault - } - return themes[s.Theme][s.backgroundColor] -} - -func (s Style) ForegroundColor() tcell.Color { - return themes[s.Theme][s.foregroundColor] -} - -func (s Style) SelectedColor() tcell.Color { - return themes[s.Theme][s.selectedColor] -} - -func (s Style) PrefixColor() tcell.Color { - return themes[s.Theme][s.prefixColor] -} - -func (s Style) LabelColor() tcell.Color { - return themes[s.Theme][s.labelColor] -} - -func (s Style) PressColor() tcell.Color { - return themes[s.Theme][s.pressColor] -} - -func (s Style) HighLightColor() tcell.Color { - return themes[s.Theme][s.highLightColor] -} - -func (s Style) SrcBorderColor() tcell.Color { - return themes[s.Theme][s.src.borderColor] -} - -func (s Style) DstBorderColor() tcell.Color { - return themes[s.Theme][s.dst.borderColor] -} - -func (s Style) SrcBorderStr() string { - return s.src.borderColor -} - -func (s Style) DstBorderStr() string { - return s.dst.borderColor -} - -func (s *Style) SetSrcBorderColor(color string) *Style { - s.src.borderColor = color - return s -} - -func (s *Style) SetDstBorderColor(color string) *Style { - s.dst.borderColor = color - return s -} diff --git a/internal/color/color.go b/internal/style/color.go similarity index 98% rename from internal/color/color.go rename to internal/style/color.go index aeee67b..a90eca4 100644 --- a/internal/color/color.go +++ b/internal/style/color.go @@ -1,4 +1,4 @@ -package color +package style import ( "github.com/gdamore/tcell/v2" diff --git a/internal/style/style.go b/internal/style/style.go new file mode 100644 index 0000000..e7407f8 --- /dev/null +++ b/internal/style/style.go @@ -0,0 +1,88 @@ +package style + +import ( + "github.com/gdamore/tcell/v2" +) + +type style struct { + srcBorderColor string + dstBorderColor string + backgroundColor string + foregroundColor string + selectedColor string + prefixColor string + labelColor string + pressColor string + highLightColor string + Theme string + Transparent bool +} + +func NewStyle() *style { + return &style{ + backgroundColor: "bg", + foregroundColor: "fg", + selectedColor: "gray", + prefixColor: "cyan", + labelColor: "yellow", + pressColor: "purple", + highLightColor: "orange", + } +} + +func (s style) BackgroundColor() tcell.Color { + if s.Transparent { + return tcell.ColorDefault + } + return themes[s.Theme][s.backgroundColor] +} + +func (s style) ForegroundColor() tcell.Color { + return themes[s.Theme][s.foregroundColor] +} + +func (s style) SelectedColor() tcell.Color { + return themes[s.Theme][s.selectedColor] +} + +func (s style) PrefixColor() tcell.Color { + return themes[s.Theme][s.prefixColor] +} + +func (s style) LabelColor() tcell.Color { + return themes[s.Theme][s.labelColor] +} + +func (s style) PressColor() tcell.Color { + return themes[s.Theme][s.pressColor] +} + +func (s style) HighLightColor() tcell.Color { + return themes[s.Theme][s.highLightColor] +} + +func (s style) SrcBorderColor() tcell.Color { + return themes[s.Theme][s.srcBorderColor] +} + +func (s style) DstBorderColor() tcell.Color { + return themes[s.Theme][s.dstBorderColor] +} + +func (s style) SrcBorderStr() string { + return s.srcBorderColor +} + +func (s style) DstBorderStr() string { + return s.dstBorderColor +} + +func (s *style) SetSrcBorderColor(color string) *style { + s.srcBorderColor = color + return s +} + +func (s *style) SetDstBorderColor(color string) *style { + s.dstBorderColor = color + return s +} diff --git a/ui.go b/ui.go index 621ea22..c3dbad3 100644 --- a/ui.go +++ b/ui.go @@ -4,7 +4,7 @@ import ( "fmt" "strconv" - "github.com/eeeXun/gtt/internal/color" + "github.com/eeeXun/gtt/internal/style" "github.com/eeeXun/gtt/internal/translate" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" @@ -66,18 +66,18 @@ func updateTranslateWindow() { func updateBackgroundColor() { // input/output srcInput.SetTextStyle(tcell.StyleDefault. - Background(style.BackgroundColor()). - Foreground(style.ForegroundColor())). - SetBackgroundColor(style.BackgroundColor()) - dstOutput.SetBackgroundColor(style.BackgroundColor()) + Background(uiStyle.BackgroundColor()). + Foreground(uiStyle.ForegroundColor())). + SetBackgroundColor(uiStyle.BackgroundColor()) + dstOutput.SetBackgroundColor(uiStyle.BackgroundColor()) defOutput.SetTextStyle(tcell.StyleDefault. - Background(style.BackgroundColor()). - Foreground(style.ForegroundColor())). - SetBackgroundColor(style.BackgroundColor()) + Background(uiStyle.BackgroundColor()). + Foreground(uiStyle.ForegroundColor())). + SetBackgroundColor(uiStyle.BackgroundColor()) posOutput.SetTextStyle(tcell.StyleDefault. - Background(style.BackgroundColor()). - Foreground(style.ForegroundColor())). - SetBackgroundColor(style.BackgroundColor()) + Background(uiStyle.BackgroundColor()). + Foreground(uiStyle.ForegroundColor())). + SetBackgroundColor(uiStyle.BackgroundColor()) // dropdown for _, dropdown := range []*tview.DropDown{ @@ -90,58 +90,58 @@ func updateBackgroundColor() { srcBorderDropDown, dstBorderDropDown} { dropdown.SetListStyles(tcell.StyleDefault. - Background(style.BackgroundColor()). - Foreground(style.ForegroundColor()), + Background(uiStyle.BackgroundColor()). + Foreground(uiStyle.ForegroundColor()), tcell.StyleDefault. - Background(style.SelectedColor()). - Foreground(style.PrefixColor())). - SetBackgroundColor(style.BackgroundColor()) + Background(uiStyle.SelectedColor()). + Foreground(uiStyle.PrefixColor())). + SetBackgroundColor(uiStyle.BackgroundColor()) } // key map - keyMapMenu.SetBackgroundColor(style.BackgroundColor()) + keyMapMenu.SetBackgroundColor(uiStyle.BackgroundColor()) } func updateBorderColor() { // input/output - srcInput.SetBorderColor(style.SrcBorderColor()). - SetTitleColor(style.SrcBorderColor()) - dstOutput.SetBorderColor(style.DstBorderColor()). - SetTitleColor(style.DstBorderColor()) - defOutput.SetBorderColor(style.SrcBorderColor()). - SetTitleColor(style.SrcBorderColor()) - posOutput.SetBorderColor(style.DstBorderColor()). - SetTitleColor(style.DstBorderColor()) + srcInput.SetBorderColor(uiStyle.SrcBorderColor()). + SetTitleColor(uiStyle.SrcBorderColor()) + dstOutput.SetBorderColor(uiStyle.DstBorderColor()). + SetTitleColor(uiStyle.DstBorderColor()) + defOutput.SetBorderColor(uiStyle.SrcBorderColor()). + SetTitleColor(uiStyle.SrcBorderColor()) + posOutput.SetBorderColor(uiStyle.DstBorderColor()). + SetTitleColor(uiStyle.DstBorderColor()) // dropdown for _, srcDropDown := range []*tview.DropDown{srcLangDropDown, srcBorderDropDown} { - srcDropDown.SetBorderColor(style.SrcBorderColor()). - SetTitleColor(style.SrcBorderColor()) + srcDropDown.SetBorderColor(uiStyle.SrcBorderColor()). + SetTitleColor(uiStyle.SrcBorderColor()) } for _, dstDropDown := range []*tview.DropDown{dstLangDropDown, dstBorderDropDown} { - dstDropDown.SetBorderColor(style.DstBorderColor()). - SetTitleColor(style.DstBorderColor()) + dstDropDown.SetBorderColor(uiStyle.DstBorderColor()). + SetTitleColor(uiStyle.DstBorderColor()) } } func updateNonConfigColor() { // input/output srcInput.SetSelectedStyle(tcell.StyleDefault. - Background(style.SelectedColor()). - Foreground(style.ForegroundColor())) - dstOutput.SetTextColor(style.ForegroundColor()) + Background(uiStyle.SelectedColor()). + Foreground(uiStyle.ForegroundColor())) + dstOutput.SetTextColor(uiStyle.ForegroundColor()) defOutput.SetSelectedStyle(tcell.StyleDefault. - Background(style.SelectedColor()). - Foreground(style.ForegroundColor())) + Background(uiStyle.SelectedColor()). + Foreground(uiStyle.ForegroundColor())) posOutput.SetSelectedStyle(tcell.StyleDefault. - Background(style.SelectedColor()). - Foreground(style.ForegroundColor())) + Background(uiStyle.SelectedColor()). + Foreground(uiStyle.ForegroundColor())) // dropdown for _, noLabelDropDown := range []*tview.DropDown{srcLangDropDown, dstLangDropDown} { - noLabelDropDown.SetFieldBackgroundColor(style.SelectedColor()). - SetFieldTextColor(style.ForegroundColor()). - SetPrefixTextColor(style.PrefixColor()) + noLabelDropDown.SetFieldBackgroundColor(uiStyle.SelectedColor()). + SetFieldTextColor(uiStyle.ForegroundColor()). + SetPrefixTextColor(uiStyle.PrefixColor()) } for _, labelDropDown := range []*tview.DropDown{ translatorDropDown, @@ -150,27 +150,27 @@ func updateNonConfigColor() { hideBelowDropDown, srcBorderDropDown, dstBorderDropDown} { - labelDropDown.SetLabelColor(style.LabelColor()). - SetFieldBackgroundColor(style.SelectedColor()). - SetFieldTextColor(style.ForegroundColor()). - SetPrefixTextColor(style.PrefixColor()) + labelDropDown.SetLabelColor(uiStyle.LabelColor()). + SetFieldBackgroundColor(uiStyle.SelectedColor()). + SetFieldTextColor(uiStyle.ForegroundColor()). + SetPrefixTextColor(uiStyle.PrefixColor()) } // button for _, button := range []*tview.Button{langButton, styleButton, keyMapButton} { - button.SetLabelColor(style.ForegroundColor()). - SetBackgroundColorActivated(style.PressColor()). - SetLabelColorActivated(style.ForegroundColor()). - SetBackgroundColor(style.SelectedColor()) + button.SetLabelColor(uiStyle.ForegroundColor()). + SetBackgroundColorActivated(uiStyle.PressColor()). + SetLabelColorActivated(uiStyle.ForegroundColor()). + SetBackgroundColor(uiStyle.SelectedColor()) } // key map - keyMapMenu.SetTextColor(style.ForegroundColor()). + keyMapMenu.SetTextColor(uiStyle.ForegroundColor()). SetText(fmt.Sprintf(keyMapText, fmt.Sprintf("%.6x", - style.HighLightColor().TrueColor().Hex()))). - SetBorderColor(style.HighLightColor()). - SetTitleColor(style.HighLightColor()) + uiStyle.HighLightColor().TrueColor().Hex()))). + SetBorderColor(uiStyle.HighLightColor()). + SetTitleColor(uiStyle.HighLightColor()) } func updateAllColor() { @@ -247,8 +247,8 @@ func uiInit() { srcLangDropDown.SetBorder(true) dstLangDropDown.SetBorder(true) themeDropDown.SetLabel("Theme: "). - SetOptions(color.AllTheme, nil). - SetCurrentOption(IndexOf(style.Theme, color.AllTheme)) + SetOptions(style.AllTheme, nil). + SetCurrentOption(IndexOf(uiStyle.Theme, style.AllTheme)) hideBelowDropDown.SetLabel("Hide below: "). SetOptions([]string{"true", "false"}, nil). SetCurrentOption( @@ -257,27 +257,27 @@ func uiInit() { transparentDropDown.SetLabel("Transparent: "). SetOptions([]string{"true", "false"}, nil). SetCurrentOption( - IndexOf(strconv.FormatBool(style.Transparent), + IndexOf(strconv.FormatBool(uiStyle.Transparent), []string{"true", "false"})) srcBorderDropDown.SetLabel("Border Color: "). - SetOptions(color.Palette, nil). + SetOptions(style.Palette, nil). SetCurrentOption( - IndexOf(style.SrcBorderStr(), - color.Palette)). + IndexOf(uiStyle.SrcBorderStr(), + style.Palette)). SetBorder(true). SetTitle("Source") dstBorderDropDown.SetLabel("Border Color: "). - SetOptions(color.Palette, nil). + SetOptions(style.Palette, nil). SetCurrentOption( - IndexOf(style.DstBorderStr(), - color.Palette)). + IndexOf(uiStyle.DstBorderStr(), + style.Palette)). SetBorder(true). SetTitle("Destination") // key map keyMapMenu.SetDynamicColors(true). SetText(fmt.Sprintf(keyMapText, - fmt.Sprintf("%.6x", style.HighLightColor().TrueColor().Hex()))). + fmt.Sprintf("%.6x", uiStyle.HighLightColor().TrueColor().Hex()))). SetBorder(true). SetTitle("Key Map") @@ -372,12 +372,12 @@ func uiInit() { dstLangDropDown.SetDoneFunc(langDropDownHandler) themeDropDown.SetDoneFunc(styleDropDownHandler). SetSelectedFunc(func(text string, index int) { - style.Theme = text + uiStyle.Theme = text updateAllColor() }) transparentDropDown.SetDoneFunc(styleDropDownHandler). SetSelectedFunc(func(text string, index int) { - style.Transparent, _ = strconv.ParseBool(text) + uiStyle.Transparent, _ = strconv.ParseBool(text) updateBackgroundColor() }) hideBelowDropDown.SetDoneFunc(styleDropDownHandler). @@ -387,12 +387,12 @@ func uiInit() { }) srcBorderDropDown.SetDoneFunc(styleDropDownHandler). SetSelectedFunc(func(text string, index int) { - style.SetSrcBorderColor(text) + uiStyle.SetSrcBorderColor(text) updateBorderColor() }) dstBorderDropDown.SetDoneFunc(styleDropDownHandler). SetSelectedFunc(func(text string, index int) { - style.SetDstBorderColor(text) + uiStyle.SetDstBorderColor(text) updateBorderColor() }) keyMapMenu.SetDoneFunc(func(key tcell.Key) { @@ -426,10 +426,10 @@ func mainPageHandler(event *tcell.EventKey) *tcell.EventKey { switch key { case tcell.KeyCtrlT: // Toggle transparent - style.Transparent = !style.Transparent + uiStyle.Transparent = !uiStyle.Transparent updateBackgroundColor() transparentDropDown.SetCurrentOption( - IndexOf(strconv.FormatBool(style.Transparent), + IndexOf(strconv.FormatBool(uiStyle.Transparent), []string{"true", "false"})) case tcell.KeyCtrlBackslash: hideBelow = !hideBelow From 303441125b9424a0cd1560fea314b8bb60e122f7 Mon Sep 17 00:00:00 2001 From: eeeXun Date: Tue, 14 Feb 2023 15:45:51 +0800 Subject: [PATCH 3/5] refacotr: move hide_below to style struct --- config.go | 13 +++---------- internal/style/style.go | 5 +++-- main.go | 3 +++ ui.go | 10 +++++----- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/config.go b/config.go index a84dbe9..88a465b 100644 --- a/config.go +++ b/config.go @@ -4,17 +4,10 @@ import ( "fmt" "os" - "github.com/eeeXun/gtt/internal/style" "github.com/eeeXun/gtt/internal/translate" config "github.com/spf13/viper" ) -var ( - // settings - uiStyle = style.NewStyle() - hideBelow bool -) - // Search XDG_CONFIG_HOME or $HOME/.config func configInit() { var ( @@ -77,8 +70,8 @@ func configInit() { config.GetString(fmt.Sprintf("destination.language.%s", name))) } translator = translators[config.GetString("translator")] - hideBelow = config.GetBool("hide_below") uiStyle.Theme = config.GetString("theme") + uiStyle.HideBelow = config.GetBool("hide_below") uiStyle.Transparent = config.GetBool("transparent") uiStyle.SetSrcBorderColor(config.GetString("source.borderColor")). SetDstBorderColor(config.GetString("destination.borderColor")) @@ -117,9 +110,9 @@ func updateConfig() { changed = true config.Set("translator", translator.GetEngineName()) } - if config.GetBool("hide_below") != hideBelow { + if config.GetBool("hide_below") != uiStyle.HideBelow { changed = true - config.Set("hide_below", hideBelow) + config.Set("hide_below", uiStyle.HideBelow) } if config.GetString("theme") != uiStyle.Theme { changed = true diff --git a/internal/style/style.go b/internal/style/style.go index e7407f8..a55429f 100644 --- a/internal/style/style.go +++ b/internal/style/style.go @@ -5,6 +5,9 @@ import ( ) type style struct { + HideBelow bool + Transparent bool + Theme string srcBorderColor string dstBorderColor string backgroundColor string @@ -14,8 +17,6 @@ type style struct { labelColor string pressColor string highLightColor string - Theme string - Transparent bool } func NewStyle() *style { diff --git a/main.go b/main.go index f96d2eb..68f8921 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "flag" + "github.com/eeeXun/gtt/internal/style" "github.com/eeeXun/gtt/internal/translate" "github.com/eeeXun/gtt/internal/ui" "github.com/rivo/tview" @@ -17,6 +18,8 @@ var ( // Translate translator translate.Translator translators = make(map[string]translate.Translator, len(translate.AllTranslator)) + // UI style + uiStyle = style.NewStyle() // UI app = tview.NewApplication() srcInput = tview.NewTextArea() diff --git a/ui.go b/ui.go index c3dbad3..dd58fa2 100644 --- a/ui.go +++ b/ui.go @@ -54,7 +54,7 @@ type Item struct { func updateTranslateWindow() { translateWindow.Clear() - if hideBelow { + if uiStyle.HideBelow { translateWindow.AddItem(translateAboveWidget, 0, 1, true) } else { translateWindow.SetDirection(tview.FlexRow). @@ -252,7 +252,7 @@ func uiInit() { hideBelowDropDown.SetLabel("Hide below: "). SetOptions([]string{"true", "false"}, nil). SetCurrentOption( - IndexOf(strconv.FormatBool(hideBelow), + IndexOf(strconv.FormatBool(uiStyle.HideBelow), []string{"true", "false"})) transparentDropDown.SetLabel("Transparent: "). SetOptions([]string{"true", "false"}, nil). @@ -382,7 +382,7 @@ func uiInit() { }) hideBelowDropDown.SetDoneFunc(styleDropDownHandler). SetSelectedFunc(func(text string, index int) { - hideBelow, _ = strconv.ParseBool(text) + uiStyle.HideBelow, _ = strconv.ParseBool(text) updateTranslateWindow() }) srcBorderDropDown.SetDoneFunc(styleDropDownHandler). @@ -432,10 +432,10 @@ func mainPageHandler(event *tcell.EventKey) *tcell.EventKey { IndexOf(strconv.FormatBool(uiStyle.Transparent), []string{"true", "false"})) case tcell.KeyCtrlBackslash: - hideBelow = !hideBelow + uiStyle.HideBelow = !uiStyle.HideBelow updateTranslateWindow() hideBelowDropDown.SetCurrentOption( - IndexOf(strconv.FormatBool(hideBelow), + IndexOf(strconv.FormatBool(uiStyle.HideBelow), []string{"true", "false"})) } From c1ebe7eb3cd4c1485c10b4c44416247026e044fc Mon Sep 17 00:00:00 2001 From: eeeXun Date: Wed, 15 Feb 2023 20:39:53 +0800 Subject: [PATCH 4/5] style: move main code out of if statement --- .../translate/apertiumtranslate/translator.go | 35 +++--- .../translate/argostranslate/translator.go | 11 +- .../translate/googletranslate/translator.go | 103 +++++++++--------- internal/translate/googletranslate/tts.go | 3 +- 4 files changed, 76 insertions(+), 76 deletions(-) diff --git a/internal/translate/apertiumtranslate/translator.go b/internal/translate/apertiumtranslate/translator.go index af7d891..cf4e314 100644 --- a/internal/translate/apertiumtranslate/translator.go +++ b/internal/translate/apertiumtranslate/translator.go @@ -71,22 +71,23 @@ func (t *ApertiumTranslate) Translate(message string) (translation, definition, return "", "", "", err } - if len(data) > 0 { - switch res.StatusCode { - case 200: - translation += fmt.Sprintf("%v", - data["responseData"].(map[string]interface{})["translatedText"]) - default: - return "", "", "", errors.New( - fmt.Sprintf("%s does not support translate from %s to %s.\nSee available pair on %s", - t.EngineName, - t.srcLang, - t.dstLang, - "https://www.apertium.org/", - )) - } - - return translation, definition, partOfSpeech, nil + if len(data) <= 0 { + return "", "", "", errors.New("Translation not found") } - return "", "", "", errors.New("Translation not found") + + switch res.StatusCode { + case 200: + translation += fmt.Sprintf("%v", + data["responseData"].(map[string]interface{})["translatedText"]) + default: + return "", "", "", errors.New( + fmt.Sprintf("%s does not support translate from %s to %s.\nSee available pair on %s", + t.EngineName, + t.srcLang, + t.dstLang, + "https://www.apertium.org/", + )) + } + + return translation, definition, partOfSpeech, nil } diff --git a/internal/translate/argostranslate/translator.go b/internal/translate/argostranslate/translator.go index 4703e0b..a7dc6de 100644 --- a/internal/translate/argostranslate/translator.go +++ b/internal/translate/argostranslate/translator.go @@ -70,10 +70,11 @@ func (t *ArgosTranslate) Translate(message string) (translation, definition, par return "", "", "", err } - if len(data) > 0 { - translation += fmt.Sprintf("%v", data["translatedText"]) - - return translation, definition, partOfSpeech, nil + if len(data) <= 0 { + return "", "", "", errors.New("Translation not found") } - return "", "", "", errors.New("Translation not found") + + translation += fmt.Sprintf("%v", data["translatedText"]) + + return translation, definition, partOfSpeech, nil } diff --git a/internal/translate/googletranslate/translator.go b/internal/translate/googletranslate/translator.go index 0822efc..66f75ab 100644 --- a/internal/translate/googletranslate/translator.go +++ b/internal/translate/googletranslate/translator.go @@ -71,58 +71,57 @@ func (t *GoogleTranslate) Translate(message string) (translation, definition, pa return "", "", "", err } - if len(data) > 0 { - // translation = data[0] - for _, lines := range data[0].([]interface{}) { - translatedLine := lines.([]interface{})[0] - translation += fmt.Sprintf("%v", translatedLine) - } - - // 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" - } - } - } - - // 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 + if len(data) <= 0 { + return "", "", "", errors.New("Translation not found") } - return "", "", "", errors.New("Translation not found") + // translation = data[0] + for _, lines := range data[0].([]interface{}) { + translatedLine := lines.([]interface{})[0] + translation += fmt.Sprintf("%v", translatedLine) + } + // 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" + } + } + } + // 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 } diff --git a/internal/translate/googletranslate/tts.go b/internal/translate/googletranslate/tts.go index e6c0e3f..e0ff87d 100644 --- a/internal/translate/googletranslate/tts.go +++ b/internal/translate/googletranslate/tts.go @@ -54,9 +54,8 @@ func (t *GoogleTranslate) PlayTTS(lang, message string) error { if t.SoundLock.Stop { t.SoundLock.Release() return nil - } else { - time.Sleep(time.Millisecond) } + time.Sleep(time.Millisecond) } if err = player.Close(); err != nil { t.SoundLock.Release() From 529decb1907f8a4ef29539bf65763eab26636b7b Mon Sep 17 00:00:00 2001 From: eeeXun Date: Fri, 17 Feb 2023 22:31:04 +0800 Subject: [PATCH 5/5] style: change borderColor to border_color in config --- config.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/config.go b/config.go index 88a465b..4088383 100644 --- a/config.go +++ b/config.go @@ -13,17 +13,17 @@ func configInit() { var ( defaultConfigPath string defaultConfig = map[string]interface{}{ + "hide_below": false, "transparent": false, "theme": "Gruvbox", - "source.borderColor": "red", - "destination.borderColor": "blue", + "source.border_color": "red", + "destination.border_color": "blue", "source.language.apertiumtranslate": "English", "destination.language.apertiumtranslate": "English", "source.language.argostranslate": "English", "destination.language.argostranslate": "English", "source.language.googletranslate": "English", "destination.language.googletranslate": "English", - "hide_below": false, "translator": "ArgosTranslate", } ) @@ -73,8 +73,8 @@ func configInit() { uiStyle.Theme = config.GetString("theme") uiStyle.HideBelow = config.GetBool("hide_below") uiStyle.Transparent = config.GetBool("transparent") - uiStyle.SetSrcBorderColor(config.GetString("source.borderColor")). - SetDstBorderColor(config.GetString("destination.borderColor")) + uiStyle.SetSrcBorderColor(config.GetString("source.border_color")). + SetDstBorderColor(config.GetString("destination.border_color")) // set argument language if len(*srcLangArg) > 0 { translator.SetSrcLang(*srcLangArg) @@ -122,13 +122,13 @@ func updateConfig() { changed = true config.Set("transparent", uiStyle.Transparent) } - if config.GetString("source.borderColor") != uiStyle.SrcBorderStr() { + if config.GetString("source.border_color") != uiStyle.SrcBorderStr() { changed = true - config.Set("source.borderColor", uiStyle.SrcBorderStr()) + config.Set("source.border_color", uiStyle.SrcBorderStr()) } - if config.GetString("destination.borderColor") != uiStyle.DstBorderStr() { + if config.GetString("destination.border_color") != uiStyle.DstBorderStr() { changed = true - config.Set("destination.borderColor", uiStyle.DstBorderStr()) + config.Set("destination.border_color", uiStyle.DstBorderStr()) } if changed {