github.com/quantumghost/awgo@v0.15.0/workflow_feedback.go (about) 1 // 2 // Copyright (c) 2018 Dean Jackson <deanishe@deanishe.net> 3 // 4 // MIT Licence. See http://opensource.org/licenses/MIT 5 // 6 // Created on 2018-02-09 7 // 8 9 package aw 10 11 import ( 12 "fmt" 13 "log" 14 15 "github.com/deanishe/awgo/fuzzy" 16 ) 17 18 // -------------------------------------------------------------------- 19 // Feedback 20 21 // Rerun tells Alfred to re-run the Script Filter after `secs` seconds. 22 func (wf *Workflow) Rerun(secs float64) *Workflow { 23 wf.Feedback.Rerun(secs) 24 return wf 25 } 26 27 // Vars returns the workflow variables set on Workflow.Feedback. 28 // See Feedback.Vars() for more information. 29 func (wf *Workflow) Vars() map[string]string { 30 return wf.Feedback.Vars() 31 } 32 33 // Var sets the value of workflow variable k on Workflow.Feedback to v. 34 // See Feedback.Var() for more information. 35 func (wf *Workflow) Var(k, v string) *Workflow { 36 wf.Feedback.Var(k, v) 37 return wf 38 } 39 40 // NewItem adds and returns a new feedback Item. 41 // See Feedback.NewItem() for more information. 42 func (wf *Workflow) NewItem(title string) *Item { 43 return wf.Feedback.NewItem(title) 44 } 45 46 // NewFileItem adds and returns a new feedback Item pre-populated from path. 47 // See Feedback.NewFileItem() for more information. 48 func (wf *Workflow) NewFileItem(path string) *Item { 49 return wf.Feedback.NewFileItem(path) 50 } 51 52 // NewWarningItem adds and returns a new Feedback Item with the system 53 // warning icon (exclamation mark on yellow triangle). 54 func (wf *Workflow) NewWarningItem(title, subtitle string) *Item { 55 56 return wf.Feedback.NewItem(title). 57 Subtitle(subtitle). 58 Icon(IconWarning) 59 } 60 61 // IsEmpty returns true if Workflow contains no items. 62 func (wf *Workflow) IsEmpty() bool { return len(wf.Feedback.Items) == 0 } 63 64 // FatalError displays an error message in Alfred, then calls log.Fatal(), 65 // terminating the workflow. 66 func (wf *Workflow) FatalError(err error) { wf.Fatal(err.Error()) } 67 68 // Fatal displays an error message in Alfred, then calls log.Fatal(), 69 // terminating the workflow. 70 func (wf *Workflow) Fatal(msg string) { wf.outputErrorMsg(msg) } 71 72 // Fatalf displays an error message in Alfred, then calls log.Fatal(), 73 // terminating the workflow. 74 func (wf *Workflow) Fatalf(format string, args ...interface{}) { 75 wf.Fatal(fmt.Sprintf(format, args...)) 76 } 77 78 // Warn displays a warning message in Alfred immediately. Unlike 79 // FatalError()/Fatal(), this does not terminate the workflow, 80 // but you can't send any more results to Alfred. 81 func (wf *Workflow) Warn(title, subtitle string) *Workflow { 82 83 // Remove any existing items 84 wf.Feedback.Clear() 85 86 wf.NewItem(title). 87 Subtitle(subtitle). 88 Icon(IconWarning) 89 90 return wf.SendFeedback() 91 } 92 93 // WarnEmpty adds a warning item to feedback if there are no other items. 94 func (wf *Workflow) WarnEmpty(title, subtitle string) { 95 if wf.IsEmpty() { 96 wf.Warn(title, subtitle) 97 } 98 } 99 100 // Filter fuzzy-sorts feedback Items against query and deletes Items that don't match. 101 func (wf *Workflow) Filter(query string) []*fuzzy.Result { 102 return wf.Feedback.Filter(query, wf.sortOptions...) 103 } 104 105 // SendFeedback sends Script Filter results to Alfred. 106 // 107 // Results are output as JSON to STDOUT. As you can output results only once, 108 // subsequent calls to sending methods are logged and ignored. 109 // 110 // The sending methods are: 111 // 112 // SendFeedback() 113 // Fatal() 114 // Fatalf() 115 // FatalError() 116 // Warn() 117 // WarnEmpty() // only sends if there are no items 118 // 119 func (wf *Workflow) SendFeedback() *Workflow { 120 121 // Set session ID 122 wf.Var("AW_SESSION_ID", wf.SessionID()) 123 124 // Truncate Items if maxResults is set 125 if wf.maxResults > 0 && len(wf.Feedback.Items) > wf.maxResults { 126 wf.Feedback.Items = wf.Feedback.Items[0:wf.maxResults] 127 } 128 129 if err := wf.Feedback.Send(); err != nil { 130 log.Fatalf("Error generating JSON : %v", err) 131 } 132 133 return wf 134 }