-
Notifications
You must be signed in to change notification settings - Fork 47
/
workflow_feedback.go
144 lines (119 loc) · 4 KB
/
workflow_feedback.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
// Copyright (c) 2018 Dean Jackson <deanishe@deanishe.net>
// MIT Licence - http://opensource.org/licenses/MIT
package aw
import (
"fmt"
"log"
"path/filepath"
"go.deanishe.net/fuzzy"
"github.com/deanishe/awgo/util"
)
// --------------------------------------------------------------------
// Feedback
// Rerun tells Alfred to re-run the Script Filter after `secs` seconds.
func (wf *Workflow) Rerun(secs float64) *Workflow {
wf.Feedback.Rerun(secs)
return wf
}
// Vars returns the workflow variables set on Workflow.Feedback.
// See Feedback.Vars() for more information.
func (wf *Workflow) Vars() map[string]string {
return wf.Feedback.Vars()
}
// Var sets the value of workflow variable k on Workflow.Feedback to v.
// See Feedback.Var() for more information.
func (wf *Workflow) Var(k, v string) *Workflow {
wf.Feedback.Var(k, v)
return wf
}
// NewItem adds and returns a new feedback Item.
// See Feedback.NewItem() for more information.
func (wf *Workflow) NewItem(title string) *Item {
return wf.Feedback.NewItem(title)
}
// NewFileItem adds and returns a new Item pre-populated from path.
// Title and Autocomplete are the base name of the file,
// Subtitle is the path to the file (using "~" for $HOME),
// Valid is true,
// UID and Arg are set to path,
// Type is "file", and
// Icon is the icon of the file at path.
func (wf *Workflow) NewFileItem(path string) *Item {
name := filepath.Base(path)
it := wf.NewItem(name)
it.Subtitle(util.PrettyPath(path)).
Arg(path).
Valid(true).
UID(path).
Autocomplete(name).
IsFile(true).
Icon(&Icon{path, "fileicon"})
return it
}
// NewWarningItem adds and returns a new Feedback Item with the system
// warning icon (exclamation mark on yellow triangle).
func (wf *Workflow) NewWarningItem(title, subtitle string) *Item {
return wf.Feedback.NewItem(title).
Subtitle(subtitle).
Icon(IconWarning)
}
// IsEmpty returns true if Workflow contains no items.
func (wf *Workflow) IsEmpty() bool { return len(wf.Feedback.Items) == 0 }
// FatalError displays an error message in Alfred, then calls log.Fatal(),
// terminating the workflow.
func (wf *Workflow) FatalError(err error) { wf.Fatal(err.Error()) }
// Fatal displays an error message in Alfred, then calls log.Fatal(),
// terminating the workflow.
func (wf *Workflow) Fatal(msg string) { wf.outputErrorMsg(msg) }
// Fatalf displays an error message in Alfred, then calls log.Fatal(),
// terminating the workflow.
func (wf *Workflow) Fatalf(format string, args ...interface{}) {
wf.Fatal(fmt.Sprintf(format, args...))
}
// Warn displays a warning message in Alfred immediately. Unlike
// FatalError()/Fatal(), this does not terminate the workflow,
// but you can't send any more results to Alfred.
func (wf *Workflow) Warn(title, subtitle string) *Workflow {
// Remove any existing items
wf.Feedback.Clear()
wf.NewItem(title).
Subtitle(subtitle).
Icon(IconWarning)
return wf.SendFeedback()
}
// WarnEmpty adds a warning item to feedback if there are no other items.
func (wf *Workflow) WarnEmpty(title, subtitle string) {
if wf.IsEmpty() {
wf.Warn(title, subtitle)
}
}
// Filter fuzzy-sorts feedback Items against query and deletes Items that don't match.
func (wf *Workflow) Filter(query string) []*fuzzy.Result {
return wf.Feedback.Filter(query, wf.sortOptions...)
}
// SendFeedback sends Script Filter results to Alfred.
//
// Results are output as JSON to STDOUT. As you can output results only once,
// subsequent calls to sending methods are logged and ignored.
//
// The sending methods are:
//
// SendFeedback()
// Fatal()
// Fatalf()
// FatalError()
// Warn()
// WarnEmpty() // only sends if there are no items
//
func (wf *Workflow) SendFeedback() *Workflow {
// Set session ID
wf.Var("AW_SESSION_ID", wf.SessionID())
// Truncate Items if maxResults is set
if wf.maxResults > 0 && len(wf.Feedback.Items) > wf.maxResults {
wf.Feedback.Items = wf.Feedback.Items[0:wf.maxResults]
}
if err := wf.Feedback.Send(); err != nil {
log.Fatalf("Error generating JSON : %v", err)
}
return wf
}