-
Notifications
You must be signed in to change notification settings - Fork 0
/
alfred.go
77 lines (66 loc) · 1.55 KB
/
alfred.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
package ralphred
import (
"encoding/json"
"fmt"
"os"
)
type AlfredItem struct {
UID string `json:"uid,omitempty"`
Title string `json:"title"`
Subtitle string `json:"subtitle,omitempty"`
Arg []string `json:"arg"`
Autocomplete string `json:"autocomplete"`
}
func alfredItemFromString(str string, set_uid bool) AlfredItem {
uid := ""
if set_uid {
uid = str
}
return AlfredItem{
UID: uid,
Title: str,
Subtitle: "",
Arg: []string{str},
Autocomplete: str,
}
}
func alfredItemFromStringForwarded(str string, set_uid bool) AlfredItem {
uid := ""
if set_uid {
uid = str
}
return AlfredItem{
UID: uid,
Title: str,
Subtitle: "",
Arg: []string{fmt.Sprintf("%s ", str)},
Autocomplete: str,
}
}
type AlfredResponse struct {
Items []AlfredItem `json:"items"`
}
func (resp AlfredResponse) Print() {
json_data, err := json.Marshal(resp)
if err != nil {
fmt.Fprintln(os.Stderr, "Error formatting string commands json")
return
}
fmt.Println(string(json_data))
}
func errorAlfredItems(errMsg string) []AlfredItem {
return []AlfredItem{
alfredItemFromString(errMsg, false),
}
}
// Filter alfred items by the search query. This shouldn't be used when there
// are a large amount of items
func filterAlfredItems(items []AlfredItem, searchQuery []string) []AlfredItem {
matchedItems := []AlfredItem{}
for _, item := range items {
if queryMatches(item.Title, searchQuery) {
matchedItems = append(matchedItems, item)
}
}
return matchedItems
}