From a69c62fc9f245fff7ac60ae1375083b15f681140 Mon Sep 17 00:00:00 2001 From: eeeXun Date: Wed, 19 Oct 2022 01:14:15 +0800 Subject: [PATCH] add mp3 example --- README.md | 4 ++++ translator.go | 5 +++-- ui.go | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1ce6d76..bd3f147 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ https://translate.googleapis.com/translate_a/single?client=gtx&sl=%s&tl=%s&dt=t& [example](https://gist.github.com/iochen/acad285d2d23066df9449d40a7f3547c) +[play on network](https://github.com/tosone/minimp3#examples-are-here) + +[low level](https://github.com/hajimehoshi/oto) + ## config [viper](https://github.com/spf13/viper) diff --git a/translator.go b/translator.go index 116b627..77625d8 100644 --- a/translator.go +++ b/translator.go @@ -10,7 +10,8 @@ import ( ) const ( - API_URL = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=%s&tl=%s&dt=t&q=%s" + api_url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=%s&tl=%s&dt=t&q=%s" + sound_url = "https://translate.google.com.vn/translate_tts?ie=UTF-8&q=%s&tl=%s&client=tw-ob" ) type Translator struct { @@ -23,7 +24,7 @@ func (t Translator) Translate(message string) (string, error) { var translated string url_str := fmt.Sprintf( - API_URL, + api_url, Lang_Code[t.src_lang], Lang_Code[t.dest_lang], url.QueryEscape(message), diff --git a/ui.go b/ui.go index aa056a1..44ff533 100644 --- a/ui.go +++ b/ui.go @@ -16,10 +16,35 @@ func ui_init() { src_box.SetSelectedStyle(tcell.StyleDefault. Background(window.src.selected_color). Foreground(window.src.foreground_color)) + src_box.SetInputCapture(InputHandle) dest_box.SetBorder(true). SetTitle(translator.dest_lang). SetBorderColor(window.dest.border_color). SetTitleColor(window.dest.border_color). SetBackgroundColor(window.dest.background_color) + dest_box.SetTextColor(window.dest.foreground_color) +} + +func InputHandle(event *tcell.EventKey) *tcell.EventKey { + key := event.Key() + switch key { + case tcell.KeyCtrlJ: + result, err := translator.Translate(src_box.GetText()) + if err != nil { + dest_box.SetText(err.Error()) + } else { + dest_box.SetText(result) + } + case tcell.KeyCtrlQ: + src_box.SetText("", true) + case tcell.KeyCtrlN: + dest_box.SetText("NNN") + case tcell.KeyCtrlP: + dest_box.SetText("PPP") + case tcell.KeyCtrlT: + dest_box.SetText("TTT") + } + + return event }