-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
101 lines (80 loc) · 2.35 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
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"fmt"
"os"
"sort"
"strings"
"github.com/deanishe/awgo"
"github.com/devnoname120/alfred-emoji-picker/scoring"
"github.com/hackebrot/turtle"
"github.com/samber/lo"
)
var wf *aw.Workflow
func init() {
wf = aw.New()
}
func run() {
query := os.Args[1]
results := search(query)
for _, result := range results {
wf.NewItem(result.Name).
Subtitle(fmt.Sprintf("Input \"%s\" (%s) into foremost application", result.Char, result.Name)).
Arg(result.Char).
Icon(&aw.Icon{Value: fmt.Sprintf("emojis/%s.png", result.Name)}).
Valid(true)
}
wf.WarnEmpty("No matching emojis", "Try a different query?")
wf.SendFeedback()
}
func main() {
wf.Run(run)
}
// FIXME: `che` doesn't return the checkbox in Alfred
func search(query string) []*turtle.Emoji {
if query == "" {
return make([]*turtle.Emoji, 0)
}
nameExactMatches := turtle.Filter(func(e *turtle.Emoji) bool {
return e.Name == query
})
namePrefixMatches := turtle.Filter(func(e *turtle.Emoji) bool {
return e.Name != query && strings.HasPrefix(e.Name, query)
})
nameContainMatches := turtle.Filter(func(e *turtle.Emoji) bool {
return e.Name != query && !strings.HasPrefix(e.Name, query) && strings.Contains(e.Name, query)
})
nameMatches := lo.Flatten([][]*turtle.Emoji{nameExactMatches, namePrefixMatches, nameContainMatches})
sort.Stable(scoring.SortedByScoreDsc{Query: query, Emojis: &nameMatches})
keywordExactMatches := turtle.Filter(func(e *turtle.Emoji) bool {
for _, keyword := range e.Keywords {
if keyword == query {
return true
}
}
return false
})
keywordContainMatches := turtle.Filter(func(e *turtle.Emoji) bool {
for _, keyword := range e.Keywords {
if keyword != query && strings.HasPrefix(keyword, query) {
return true
}
}
return false
})
keywordMatches := lo.Flatten([][]*turtle.Emoji{keywordExactMatches, keywordContainMatches})
sort.Stable(scoring.SortedByScoreDsc{Query: query, Emojis: &keywordMatches})
categoryExactMatches := turtle.Filter(func(e *turtle.Emoji) bool {
return e.Category == query
})
categoryMatches := turtle.Filter(func(e *turtle.Emoji) bool {
return e.Category != query && strings.HasPrefix(e.Category, query)
})
results := [][]*turtle.Emoji{
nameMatches,
keywordMatches,
categoryExactMatches,
categoryMatches,
}
consolidated := lo.Uniq(lo.Flatten(results))
return consolidated
}