Integrate whole thingie to alfred

This commit is contained in:
Kristóf Tóth 2019-04-06 18:09:02 +02:00
parent bd2e08e8f3
commit 1bc5ec5b73
1 changed files with 44 additions and 4 deletions

48
main.go
View File

@ -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
}