Support running main outside of Alfred
This commit is contained in:
parent
5c6b71b3a8
commit
23388ca46c
45
main.go
45
main.go
@ -7,20 +7,18 @@ import (
|
||||
"./trans"
|
||||
"github.com/deanishe/awgo"
|
||||
"regexp"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
|
||||
func main() {
|
||||
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()
|
||||
})
|
||||
if !runningInAlfred() {
|
||||
runAsCliTool(translations)
|
||||
} else {
|
||||
runAsAlfredWorkflow(translations)
|
||||
}
|
||||
}
|
||||
|
||||
func translateProgramArgs() []string {
|
||||
@ -69,3 +67,34 @@ func decideWhatLanguagesToUse(word string) (string, string) {
|
||||
}
|
||||
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()
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user