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