-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathoptions.go
115 lines (98 loc) · 2.57 KB
/
options.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
package main
import (
"encoding/json"
"fmt"
"log"
"reflect"
"strconv"
"github.com/jason0x43/go-alfred"
)
// OptionsCommand is a command
type OptionsCommand struct{}
// About returns information about a command
func (c OptionsCommand) About() alfred.CommandDef {
return alfred.CommandDef{
Keyword: "options",
Description: "Sets options",
IsEnabled: config.APIKey != "",
}
}
// Items returns a list of filter items
func (c OptionsCommand) Items(arg, data string) (items []alfred.Item, err error) {
ct := reflect.TypeOf(config)
cfg := reflect.Indirect(reflect.ValueOf(config))
for i := 0; i < ct.NumField(); i++ {
field := ct.Field(i)
desc := field.Tag.Get("desc")
if desc == "" {
continue
}
name, value := alfred.SplitCmd(arg)
if !alfred.FuzzyMatches(field.Name, name) {
continue
}
item := alfred.Item{
Title: field.Name,
Subtitle: desc,
Autocomplete: field.Name,
}
itemArg := &alfred.ItemArg{
Keyword: "options",
Mode: alfred.ModeDo,
}
switch field.Type.Name() {
case "bool":
f := cfg.FieldByName(field.Name)
if name == field.Name {
item.Title += " (press Enter to toggle)"
}
// copy the current options, update them, and use as the arg
opts := config
o := reflect.Indirect(reflect.ValueOf(&opts))
newVal := !f.Bool()
o.FieldByName(field.Name).SetBool(newVal)
item.Arg = itemArg
item.Arg.Data = alfred.Stringify(opts)
item.AddCheckBox(f.Bool())
case "int":
item.Autocomplete += " "
if value != "" {
val, err := strconv.Atoi(value)
if err != nil {
return items, err
}
item.Title += fmt.Sprintf(": %d", val)
// copy the current options, update them, and use as the arg
opts := config
o := reflect.Indirect(reflect.ValueOf(&opts))
o.FieldByName(field.Name).SetInt(int64(val))
item.Arg = itemArg
item.Arg.Data = alfred.Stringify(opts)
} else {
f := cfg.FieldByName(field.Name)
val := f.Int()
item.Title += fmt.Sprintf(": %v", val)
if name == field.Name {
item.Title += " (type a new value to change)"
}
}
case "string":
f := cfg.FieldByName(field.Name)
item.Autocomplete += " "
item.Title += ": " + f.String()
}
items = append(items, item)
}
return
}
// Do runs the command
func (c OptionsCommand) Do(data string) (out string, err error) {
if err = json.Unmarshal([]byte(data), &config); err != nil {
return
}
if err = alfred.SaveJSON(configFile, &config); err != nil {
log.Printf("Error saving config: %s\n", err)
return "Error updating options", err
}
return "Updated options", err
}