Refactor awgo's unmaintainable global hell out of main

This commit is contained in:
Kristóf Tóth 2019-12-24 22:20:41 +01:00
parent 90d47f1a16
commit 5c6b71b3a8

24
main.go
View File

@ -9,17 +9,21 @@ import (
"regexp"
)
var wf *aw.Workflow
func init() {
wf = aw.New()
}
func main() {
wf.Run(run)
translations := translateProgramArgs()
wf := aw.New()
wf.Run(func() {
for _, translation := range translations {
wf.NewItem(translation).Arg(translation).Valid(true)
}
wf.WarnEmpty("No results", "Try a different query?")
wf.SendFeedback()
})
}
func run() {
func translateProgramArgs() []string {
word, fromLang, toLang := parseProgramArgs(os.Args)
if fromLang == "" && toLang == "" {
fromLang, toLang = decideWhatLanguagesToUse(word)
@ -28,11 +32,7 @@ func run() {
t := trans.New(word)
t.Language = fromLang
translations := t.Translate(toLang)
for _, translation := range translations {
wf.NewItem(translation).Arg(translation).Valid(true)
}
wf.WarnEmpty("No results", "Try a different query?")
wf.SendFeedback()
return translations
}
func parseProgramArgs(args []string) (string, string, string) {