-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalfred.go
182 lines (164 loc) · 4.42 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// +build darwin
package main
import (
"regexp"
"strings"
aw "github.com/deanishe/awgo"
)
type alfred struct {
itemTitle string
itemSubtitle string
itemArg string
iconFilename string
copyText string
itemVars arrayFlags
cannedItems arrayFlags
filter string
filterFunc func(string) bool
format *regexp.Regexp
globalVars arrayFlags
replacePlaceholder func(string, string, string) string
itemModifiers arrayFlags
}
func (a alfred) list(values []string) {
wf := aw.New()
wf.Args()
wf.Run(func() {
for name, value := range buildVariables(a.globalVars, "", a.replacePlaceholder) {
wf.Var(name, value)
}
hasCanned := false
validFormat := a.format.MatchString(a.filter)
if a.filter == "" || !validFormat {
for _, item := range a.cannedItems {
if writeCannedItem(wf, item, a.filterFunc) {
hasCanned = true
}
}
}
if len(values) == 0 && !hasCanned {
if a.filter != "" && validFormat {
values = append(values, a.filter)
} else if !validFormat {
for _, item := range a.cannedItems {
writeCannedItem(wf, item, func(title string) bool { return true })
}
}
}
for _, line := range values {
item := wf.NewItem(a.replacePlaceholder(a.itemTitle, "VALUE", line)).
Arg(a.replacePlaceholder(a.itemArg, "VALUE", line)).
Valid(true)
if a.itemSubtitle != "" {
item.Subtitle(a.replacePlaceholder(a.itemSubtitle, "VALUE", line))
}
if a.iconFilename != "" {
item.Icon(&aw.Icon{
Value: a.iconFilename,
Type: aw.IconTypeImage,
})
}
if a.copyText != "" {
item.Copytext(a.replacePlaceholder(a.copyText, "VALUE", line))
}
for name, value := range buildVariables(a.itemVars, line, a.replacePlaceholder) {
item.Var(name, value)
}
for modifier, modifierValues := range buildModifiers(a.itemModifiers) {
var mod *aw.Modifier
switch modifier {
case "alt":
mod = item.Alt().Valid(true)
case "cmd":
mod = item.Cmd().Valid(true)
case "ctrl":
mod = item.Ctrl().Valid(true)
case "fn":
mod = item.Fn().Valid(true)
default:
mod = nil
}
if mod != nil {
for name, value := range modifierValues {
switch name {
case "arg":
mod.Arg(a.replacePlaceholder(value, "VALUE", line))
case "icon":
mod.Icon(&aw.Icon{
Value: value,
Type: aw.IconTypeImage,
})
case "subtitle":
mod.Subtitle(a.replacePlaceholder(value, "VALUE", line))
case "var":
for varName, varValue := range mapNameValuePairs(value) {
mod.Var(varName, a.replacePlaceholder(varValue, "VALUE", line))
}
}
}
}
}
}
wf.SendFeedback()
})
}
func buildModifiers(modifierFlags arrayFlags) map[string]map[string]string {
modifiers := make(map[string]map[string]string)
for _, modifier := range modifierFlags {
parts := strings.SplitN(modifier, ":", 3)
if len(parts) == 3 {
if modifiers[parts[0]] == nil {
modifiers[parts[0]] = make(map[string]string)
}
modifiers[parts[0]][parts[1]] = parts[2]
}
}
return modifiers
}
func buildVariables(vars arrayFlags, itemValue string, replacePlaceholder func(string, string, string) string) map[string]string {
m := make(map[string]string)
for _, avar := range vars {
for varName, varValue := range mapNameValuePairs(avar) {
m[varName] = replacePlaceholder(varValue, "VALUE", itemValue)
}
}
return m
}
func writeCannedItem(wf *aw.Workflow, cannedItem string, filterFunc func(string) bool) bool {
pairs := mapNameValuePairs(cannedItem)
if len(pairs) == 0 {
if filterFunc(cannedItem) {
wf.NewItem(cannedItem).Arg(strings.ToLower(cannedItem)).Subtitle(strings.ToLower(cannedItem)).Valid(true)
return true
}
} else {
title, okTitle := pairs["title"]
if okTitle && filterFunc(title) {
arg, okArg := pairs["arg"]
if !okArg {
arg = strings.ToLower(title)
}
subtitle, okSubtitle := pairs["subtitle"]
if !okSubtitle {
subtitle = "Open " + arg
}
wf.NewItem(title).Arg(arg).Subtitle(subtitle).Valid(true)
return true
}
}
return false
}
func mapNameValuePairs(input string) map[string]string {
m := make(map[string]string)
results := strings.Split(input, "||")
if results != nil {
for i := 0; i < len(results); i++ {
item := results[i]
parts := strings.SplitN(item, "=", 2)
if len(parts) == 2 {
m[parts[0]] = parts[1]
}
}
}
return m
}