trans-alfred/main.go
2019-04-06 18:09:02 +02:00

55 lines
1.0 KiB
Go

package main
import (
"log"
"os"
"strings"
"./trans"
"github.com/deanishe/awgo"
"regexp"
)
var wf *aw.Workflow
func init() {
wf = aw.New()
}
func main() {
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
}