trans-alfred/main.go

55 lines
1.0 KiB
Go
Raw Normal View History

2019-04-05 23:02:12 +00:00
package main
import (
2019-04-06 16:09:02 +00:00
"log"
2019-04-05 23:02:12 +00:00
"os"
"strings"
2019-04-06 16:09:02 +00:00
"./trans"
"github.com/deanishe/awgo"
"regexp"
2019-04-05 23:02:12 +00:00
)
2019-04-06 16:09:02 +00:00
var wf *aw.Workflow
func init() {
wf = aw.New()
}
2019-04-05 23:02:12 +00:00
func main() {
2019-04-06 16:09:02 +00:00
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
2019-04-05 23:02:12 +00:00
}