Support running main outside of Alfred

This commit is contained in:
Kristóf Tóth 2019-12-24 22:40:10 +01:00
parent 5c6b71b3a8
commit 23388ca46c

45
main.go
View File

@ -7,20 +7,18 @@ import (
"./trans" "./trans"
"github.com/deanishe/awgo" "github.com/deanishe/awgo"
"regexp" "regexp"
"fmt"
) )
func main() { func main() {
translations := translateProgramArgs() translations := translateProgramArgs()
wf := aw.New() if !runningInAlfred() {
wf.Run(func() { runAsCliTool(translations)
for _, translation := range translations { } else {
wf.NewItem(translation).Arg(translation).Valid(true) runAsAlfredWorkflow(translations)
} }
wf.WarnEmpty("No results", "Try a different query?")
wf.SendFeedback()
})
} }
func translateProgramArgs() []string { func translateProgramArgs() []string {
@ -69,3 +67,34 @@ func decideWhatLanguagesToUse(word string) (string, string) {
} }
return fromLang, toLang return fromLang, toLang
} }
func runningInAlfred() bool {
envVarsSetByAlfred := []string {
"alfred_version",
"alfred_workflow_uid",
"alfred_workflow_data",
}
for _, envVar := range envVarsSetByAlfred {
if _, ok := os.LookupEnv(envVar); !ok {
return false
}
}
return true
}
func runAsCliTool(translations []string) {
for _, translation := range translations {
fmt.Println(translation)
}
}
func runAsAlfredWorkflow(translations []string) {
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()
})
}