diff --git a/trans.go b/trans.go index 8e36079..becaccd 100644 --- a/trans.go +++ b/trans.go @@ -5,26 +5,41 @@ import ( "os" "os/exec" "strings" + "log" ) func main() { - outBytes, err := execTrans(os.Args[1]) - if err != nil { - panic(err) - } - translations := parseTransOutput(string(outBytes)) - fmt.Println(strings.Join(uniqueSlice(translations), "\n")) + results := New(os.Args[1], "en", "hu").Translate() + fmt.Println(strings.Join(results, "\n")) } -func execTrans(input string) ([]byte, error) { +type transJob struct { + word string + fromLang string + toLang string +} + +func New(word, fromLang, toLang string) transJob { + return transJob{word: word, fromLang: fromLang, toLang: toLang} +} + +func (tj transJob) Translate() []string { + outBytes, err := tj.execTrans() + if err != nil { + log.Fatal("Failed to execute command 'trans'!") + } + return uniqueSlice(parseTransOutput(string(outBytes))) +} + +func (tj transJob) execTrans() ([]byte, error) { config := []string{ "-show-original", "n", "-show-original-phonetics", "n", "-show-dictionary", "n", "-show-languages", "n", "-show-prompt-message", "n", - "hu:en", - input, + fmt.Sprintf("%s:%s", tj.fromLang, tj.toLang), + tj.word, } return exec.Command("trans", config...).Output() }