Skip to content

Commit e1f3172

Browse files
committed
Implement top-level variables and rerun introduced in Alfred 3.2
1 parent aef36ca commit e1f3172

File tree

3 files changed

+74
-19
lines changed

3 files changed

+74
-19
lines changed

feedback.go

+28-13
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,10 @@ func (m *Modifier) MarshalJSON() ([]byte, error) {
351351
// and Modifier structs.
352352
type Feedback struct {
353353
// Items are the results to be sent to Alfred.
354-
Items []*Item `json:"items"`
355-
// Set to true when feedback has been sent.
356-
sent bool
357-
vars map[string]string
354+
Items []*Item
355+
rerun float64 // Tell Alfred to re-run Script Filter.
356+
sent bool // Set to true when feedback has been sent.
357+
vars map[string]string // Top-level feedback variables.
358358
}
359359

360360
// NewFeedback creates a new, initialised Feedback struct.
@@ -374,10 +374,11 @@ func (fb *Feedback) Var(k, v string) *Feedback {
374374
return fb
375375
}
376376

377-
// Var returns the value of Feedback's workflow variable for key k.
378-
// func (fb *Feedback) Var(k string) string {
379-
// return fb.vars[k]
380-
// }
377+
// Rerun tells Alfred to re-run the Script Filter after `secs` seconds.
378+
func (fb *Feedback) Rerun(secs float64) *Feedback {
379+
fb.rerun = secs
380+
return fb
381+
}
381382

382383
// Vars returns the Feedback's workflow variables.
383384
func (fb *Feedback) Vars() map[string]string {
@@ -402,11 +403,11 @@ func (fb *Feedback) NewItem(title string) *Item {
402403
it := &Item{title: title, vars: map[string]string{}}
403404

404405
// Variables
405-
if len(fb.vars) > 0 {
406-
for k, v := range fb.vars {
407-
it.Var(k, v)
408-
}
409-
}
406+
// if len(fb.vars) > 0 {
407+
// for k, v := range fb.vars {
408+
// it.Var(k, v)
409+
// }
410+
// }
410411

411412
fb.Items = append(fb.Items, it)
412413
return it
@@ -432,6 +433,20 @@ func (fb *Feedback) NewFileItem(path string) *Item {
432433
return it
433434
}
434435

436+
// MarshalJSON serializes Feedback to Alfred 3's JSON format.
437+
// You shouldn't need to call this: use Send() instead.
438+
func (fb *Feedback) MarshalJSON() ([]byte, error) {
439+
return json.Marshal(&struct {
440+
Variables map[string]string `json:"variables,omitempty"`
441+
Rerun float64 `json:"rerun,omitempty"`
442+
Items []*Item `json:"items"`
443+
}{
444+
Items: fb.Items,
445+
Rerun: fb.rerun,
446+
Variables: fb.vars,
447+
})
448+
}
449+
435450
// Send generates JSON from this struct and sends it to Alfred
436451
// (by writing the JSON to STDOUT).
437452
func (fb *Feedback) Send() error {

feedback_test.go

+36-5
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,17 @@ func TestMarshalFeedback(t *testing.T) {
289289

290290
// Feedback with item
291291
// want = `<items><item valid="no"><title>item 1</title></item></items>`
292-
want = `{"items":[{"title":"item 1"}]}`
292+
want = `{"items":[{"title":"item 1","valid":false}]}`
293293
fb.NewItem("item 1")
294294

295295
got, err = json.Marshal(fb)
296296
if err != nil {
297297
t.Fatalf("Error marshalling feedback: got: %s want: %s: %v",
298298
got, want, err)
299299
}
300+
if string(got) != want {
301+
t.Fatalf("Wrong feedback JSON. Expected=%s, got=%s", want, got)
302+
}
300303

301304
}
302305

@@ -313,6 +316,22 @@ func TestModifiersInheritVars(t *testing.T) {
313316
}
314317
}
315318

319+
// TestFeedbackRerun verifies that rerun is properly set.
320+
func TestFeedbackRerun(t *testing.T) {
321+
fb := NewFeedback()
322+
323+
fb.Rerun(1.5)
324+
325+
want := `{"rerun":1.5,"items":[]}`
326+
got, err := json.Marshal(fb)
327+
if err != nil {
328+
t.Fatalf("Error serializing feedback: got: %s want: %s: %s", got, want, err)
329+
}
330+
if string(got) != want {
331+
t.Fatalf("Wrong feedback JSON. Expected=%s, got=%s", want, got)
332+
}
333+
}
334+
316335
// TestFeedbackVars tests if vars are properly inherited by Items and Modifiers
317336
func TestFeedbackVars(t *testing.T) {
318337
fb := NewFeedback()
@@ -322,14 +341,26 @@ func TestFeedbackVars(t *testing.T) {
322341
t.Fatalf("Feedback var has wrong value. Expected=bar, Received=%v", fb.Vars()["foo"])
323342
}
324343

344+
want := `{"variables":{"foo":"bar"},"items":[]}`
345+
got, err := json.Marshal(fb)
346+
if err != nil {
347+
t.Fatalf("Error serializing feedback: got: %s want: %s: %s", got, want, err)
348+
}
349+
if string(got) != want {
350+
t.Fatalf("Wrong feedback JSON. Expected=%s, got=%s", want, got)
351+
}
352+
353+
// Top-level vars are not inherited
325354
it := fb.NewItem("title")
326-
if it.Vars()["foo"] != "bar" {
327-
t.Fatalf("Item var has wrong value. Expected=bar, Received=%v", it.Vars()["foo"])
355+
if it.Vars()["foo"] != "" {
356+
t.Fatalf("Item var has wrong value. Expected='', Received=%v", it.Vars()["foo"])
328357
}
329358

359+
// Modifier inherits Item vars
360+
it.Var("foo", "baz")
330361
m := it.NewModifier("cmd")
331-
if m.Vars()["foo"] != "bar" {
332-
t.Fatalf("Modifier var has wrong value. Expected=bar, Received=%v", m.Vars()["foo"])
362+
if m.Vars()["foo"] != "baz" {
363+
t.Fatalf("Modifier var has wrong value. Expected=baz, Received=%v", m.Vars()["foo"])
333364
}
334365
}
335366

workflow.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,18 @@ func (wf *Workflow) OpenLog() error {
602602
// --------------------------------------------------------------------
603603
// Feedback
604604

605+
// Rerun tells Alfred to re-run the Script Filter after `secs` seconds.
606+
func Rerun(secs float64) *Workflow { return wf.Rerun(secs) }
607+
608+
// Rerun tells Alfred to re-run the Script Filter after `secs` seconds.
609+
func (wf *Workflow) Rerun(secs float64) *Workflow {
610+
wf.Feedback.Rerun(secs)
611+
return wf
612+
}
613+
605614
// Vars returns the workflow variables set on Workflow.Feedback.
606615
// See Feedback.Vars() for more information.
607-
func Vars() map[string]string { return wf.Feedback.Vars() }
616+
func Vars() map[string]string { return wf.Vars() }
608617

609618
// Vars returns the workflow variables set on Workflow.Feedback.
610619
// See Feedback.Vars() for more information.

0 commit comments

Comments
 (0)