-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
alfred.go
120 lines (104 loc) · 3.72 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
package alfred
import (
"encoding/json"
. "fmt"
"strings"
)
// Indent specifies the indent string used for the JSON output for String() and Run(). If set to "", no indentation will be used.
var Indent = " "
// Rerun specifies the "rerun" value.
var Rerun float64
// Variables specifies the script filter level "variables" object.
var Variables = map[string]string{}
// Items specifies the "items" array. It can be accessed and iterated directly. It can also be appended to directly or appended to using the convenience function Add(item).
var Items = []Item{}
// MaxTitle specifies the maximum title length used by ExtendTitleToSubtitle.
var MaxTitle = 45
// Icon specifies the "icon" field of an Item.
type Icon struct {
Type string `json:"type,omitempty"`
Path string `json:"path,omitempty"`
}
// Mod specifies the values of an Item.Mods map for the "mods" object.
type Mod struct {
Variables map[string]string `json:"variables,omitempty"`
Valid *bool `json:"valid,omitempty"`
Arg string `json:"arg,omitempty"`
Subtitle string `json:"subtitle,omitempty"`
Icon *Icon `json:"icon,omitempty"`
}
// Text specifies the "text" field of an Item.
type Text struct {
Copy string `json:"copy,omitempty"`
Largetype string `json:"largetype,omitempty"`
}
// Item specifies the members of the "items" array.
type Item struct {
Variables map[string]string `json:"variables,omitempty"`
UID string `json:"uid,omitempty"`
Title string `json:"title"`
Subtitle string `json:"subtitle,omitempty"`
Arg string `json:"arg,omitempty"`
Icon *Icon `json:"icon,omitempty"`
Autocomplete string `json:"autocomplete,omitempty"`
Type string `json:"type,omitempty"`
Valid *bool `json:"valid,omitempty"`
Match string `json:"match,omitempty"`
Mods map[string]Mod `json:"mods,omitempty"`
Text *Text `json:"text,omitempty"`
QuicklookURL string `json:"quicklookurl,omitempty"`
}
// Bool is a convenience function for filling optional bool values.
func Bool(b bool) *bool {
return &b
}
// Add is a convenience function for adding new Item instances to Items.
func Add(item Item) {
Items = append(Items, item)
}
// ExtendTitleToSubtitle extends words of item.Title that cause its string length to exceed MaxTitle to item.Subtitle.
func ExtendTitleToSubtitle(item Item) Item {
if len(item.Title) > MaxTitle {
words := strings.Split(item.Title, " ")
titleLen := len(words[0])
titleWords := []string{words[0]}
i := 1
for ; titleLen+1+len(words[i]) < MaxTitle; i++ {
titleWords = append(titleWords, words[i])
titleLen += 1 + len(words[i])
}
item.Title = strings.Join(titleWords, " ")
item.Subtitle = strings.Join(words[i:], " ")
}
return item
}
type output struct {
Rerun float64 `json:"rerun,omitempty"`
Variables map[string]string `json:"variables,omitempty"`
Items []Item `json:"items"`
}
// String returns the JSON for currently populated values or the minimum required values.
func String() string {
output := output{
Rerun: Rerun,
Variables: Variables,
Items: Items,
}
var err error
var b []byte
if Indent == "" {
b, err = json.Marshal(output)
} else {
b, err = json.MarshalIndent(output, "", Indent)
}
if err != nil {
messageErr := Errorf("Error in parser. Please report this output to https://github.com/drgrib/alfred/issues: %v", err)
panic(messageErr)
}
s := string(b)
return s
}
// Run prints the result of String() to standard output for debugging or direct consumption by an Alfred script filter.
func Run() {
Println(String())
}