trans-alfred/identify/id.go

48 lines
847 B
Go

package identify
import (
"os/exec"
"regexp"
)
type Indetification struct {
word string
Identifier func(*Indetification) (string, error)
}
func New(word string) Indetification {
return Indetification{
word: word,
Identifier: executeTransShell,
}
}
func executeTransShell(i *Indetification) (string, error) {
config := []string{
"-no-ansi",
"-id",
i.word,
}
outBytes, err := exec.Command("trans", config...).Output()
return string(outBytes), err
}
func (i Indetification) Identify() string {
output, err := i.Identifier(&i)
if err != nil {
panic(err)
}
return parseTransIdentifyOutput(output)
}
func parseTransIdentifyOutput(out string) string {
re := regexp.MustCompile(`(?m)^Code\s+(\w+)$`)
result := ""
matches := re.FindStringSubmatch(out)
if (len(matches) >= 2) {
result = matches[1]
}
return result
}