-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
90 lines (79 loc) · 1.88 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"regexp"
"strings"
aw "github.com/deanishe/awgo"
"github.com/hzenginx/tureng/tureng"
"golang.org/x/text/unicode/norm"
)
var wf *aw.Workflow
func init() {
wf = aw.New()
}
func splitInput(query string) (string, []string) {
splited := strings.Split(query, ":")
return splited[0], splited[1:]
}
func getIcon(category string) *aw.Icon {
var icon string
matched, _ := regexp.MatchString(`en->tr`, category)
print(category)
if matched {
icon = "en_tr.png"
} else {
icon = "tr_en.png"
}
return &aw.Icon{
Value: icon,
}
}
func handleSearchResponse(response *tureng.SearchResponse, wf *aw.Workflow) {
if response.IsSuccessful {
if response.Result.IsFound == 1 {
for _, result := range response.Result.Results {
icon := getIcon(result.Category)
wf.NewItem(result.Term).Subtitle(result.Category).Icon(icon).Arg(result.Term).Valid(true)
}
} else {
for _, suggestion := range response.Result.Suggestions {
wf.NewWarningItem(suggestion, "Did you mean this?").Autocomplete(fmt.Sprintf("translate:%s", suggestion))
}
}
} else {
wf.Fatal(response.Exception)
}
wf.SendFeedback()
}
func handleAutocompleteResponse(response *tureng.AutoCompleteResponse, wf *aw.Workflow) {
for _, word := range response.Words {
wf.NewItem(word).Autocomplete(fmt.Sprintf("translate:%s", word)).Icon(&aw.Icon{Value: "tureng.png"})
}
wf.SendFeedback()
}
func run() {
var query = wf.Args()[0]
query = norm.NFC.String(query)
command, args := splitInput(query)
if command == "translate" {
if len(args) > 0 {
word := args[0]
response, err := tureng.Search(word)
if err != nil {
wf.FatalError(err)
} else {
handleSearchResponse(response, wf)
}
}
} else {
response, err := tureng.AutoComplete(command)
if err != nil {
wf.FatalError(err)
} else {
handleAutocompleteResponse(response, wf)
}
}
}
func main() {
wf.Run(run)
}