From 1bc5ec5b73d327335e644ddafb316c9be5e9cda3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Sat, 6 Apr 2019 18:09:02 +0200 Subject: [PATCH] Integrate whole thingie to alfred --- main.go | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 62998a2..d7d1e3a 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,54 @@ package main import ( - "./trans" + "log" "os" - "fmt" "strings" + "./trans" + "github.com/deanishe/awgo" + "regexp" ) +var wf *aw.Workflow + +func init() { + wf = aw.New() +} func main() { - results := trans.New(os.Args[1], "en", "hu").Translate() - fmt.Println(strings.Join(results, "\n")) + wf.Run(run) +} + +func run() { + word, fromLang, toLang := parseProgramArgs(os.Args) + + translations := trans.New(word, fromLang, toLang).Translate() + for _, translation := range translations { + wf.NewItem(translation).Arg(translation).Valid(true) + } + wf.WarnEmpty("No results", "Try a different query?") + wf.SendFeedback() +} + +func parseProgramArgs(args []string) (string, string, string) { + if len(args) < 2 { + log.Fatal("Not enough arguments!") + } + word := strings.Join(args[1:], " ") + fromLang, toLang := "", "" + + if len(args) > 2 { + if isTransLanguageSpecifier(args[1]) { + languages := strings.Split(os.Args[1], ":") + fromLang, toLang = languages[0], languages[1] + word = strings.Join(os.Args[2:], " ") + } + } + + return word, fromLang, toLang +} + +func isTransLanguageSpecifier(arg string) bool { + ok, _ := regexp.MatchString("(\\w{2})?:(\\w{2})?", os.Args[1]) + return ok }