diff --git a/main.go b/main.go index 958f4ee..bd9a704 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,7 @@ import ( var ( // Translate - translator Translator + translator = NewTranslator() // UI app = tview.NewApplication() src_box = tview.NewTextArea() diff --git a/translator.go b/translator.go index 8d36d4e..bfa77d9 100644 --- a/translator.go +++ b/translator.go @@ -13,13 +13,20 @@ import ( ) 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&sl=%s&tl=%s&dt=t&q=%s" soundURL = "https://translate.google.com.vn/translate_tts?ie=UTF-8&q=%s&tl=%s&client=tw-ob" ) type Translator struct { - srcLang string - dstLang string + srcLang string + dstLang string + soundLock *Lock +} + +func NewTranslator() Translator { + return Translator{ + soundLock: NewLock(), + } } func (t Translator) Translate(message string) (string, error) { diff --git a/utils.go b/utils.go index 46ae5a1..94aa536 100644 --- a/utils.go +++ b/utils.go @@ -10,5 +10,27 @@ func IndexOf(candidate string, arr []string) int { } func SetTermTitle(title string) { - print("\033]0;" , title , "\007") + print("\033]0;", title, "\007") +} + +type Lock struct { + stop bool + threadCount int8 +} + +func NewLock() *Lock { + return &Lock{ + stop: true, + threadCount: 0, + } +} + +func (l *Lock) Aquire() { + l.stop = false + l.threadCount++ +} + +func (l *Lock) Release() { + l.stop = true + l.threadCount-- }