-
Notifications
You must be signed in to change notification settings - Fork 13
/
cmd_events.go
229 lines (187 loc) · 4.77 KB
/
cmd_events.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//
// Copyright (c) 2017 Dean Jackson <[email protected]>
//
// MIT Licence. See http://opensource.org/licenses/MIT
//
// Created on 2017-11-25
//
package main
import (
"fmt"
"log"
"os"
"os/exec"
"time"
aw "github.com/deanishe/awgo"
"github.com/pkg/errors"
)
// doEvents shows a list of events in Alfred.
func doEvents() error {
if len(accounts) == 0 {
wf.NewItem("No Accounts Configured").
Subtitle("Action this item to add a Google account").
Autocomplete("workflow:login").
Icon(aw.IconWarning)
wf.SendFeedback()
return nil
}
var (
cals []*Calendar
err error
)
if cals, err = activeCalendars(); err != nil {
if err == errNoActive {
wf.NewItem("No Active Calendars").
Subtitle("Action this item to choose calendars").
Autocomplete("workflow:calendars").
Icon(aw.IconWarning)
wf.SendFeedback()
return nil
}
if err == errNoCalendars {
if !wf.IsRunning("update-calendars") {
cmd := exec.Command(os.Args[0], "update", "calendars")
if err := wf.RunInBackground("update-calendars", cmd); err != nil {
return errors.Wrap(err, "run calendar update")
}
}
wf.NewItem("Fetching List of Calendars…").
Subtitle("List will reload shortly").
Valid(false).
Icon(ReloadIcon())
wf.Rerun(0.1)
wf.SendFeedback()
return nil
}
return err
}
log.Printf("%d active calendar(s)", len(cals))
var (
all []*Event
events []*Event
parsed time.Time
)
if all, err = loadEvents(opts.StartTime, cals...); err != nil {
return errors.Wrap(err, "load events")
}
// Filter out events after cutoff
for _, e := range all {
if !opts.ScheduleMode && e.Start.After(opts.EndTime) {
break
}
events = append(events, e)
log.Printf("%s", e.Title)
}
if len(all) == 0 && wf.IsRunning("update-events") {
wf.NewItem("Fetching Events…").
Subtitle("Results will refresh shortly").
Icon(ReloadIcon()).
Valid(false)
wf.Rerun(0.1)
}
log.Printf("%d event(s) for %s", len(events), opts.StartTime.Format(timeFormat))
if t, ok := parseDate(opts.Query); ok {
parsed = t
}
if len(events) == 0 && opts.Query == "" {
wf.NewItem(fmt.Sprintf("No Events on %s", opts.StartTime.Format(timeFormatLong))).
Icon(ColouredIcon(iconCalendar, yellow))
}
var day time.Time
for _, e := range events {
// Show day indicator if this is the first event of a given day
if opts.ScheduleMode && midnight(e.Start).After(day) {
day = midnight(e.Start)
wf.NewItem(day.Format(timeFormatLong)).
Arg(day.Format(timeFormat)).
Valid(true).
Icon(iconDay)
}
icon := ColouredIcon(iconCalendar, e.Colour)
sub := fmt.Sprintf("%s – %s / %s",
e.Start.Local().Format(hourFormat),
e.End.Local().Format(hourFormat),
e.CalendarTitle)
if e.Location != "" {
sub = sub + " / " + e.Location
}
it := wf.NewItem(e.Title).
Subtitle(sub).
Icon(icon).
Arg(e.URL).
Quicklook(previewURL(opts.StartTime, e.ID)).
Valid(true).
Var("action", "open")
if e.Location != "" {
app := "Google Maps"
if opts.UseAppleMaps {
app = "Apple Maps"
}
icon := ColouredIcon(iconMap, e.Colour)
it.NewModifier("cmd").
Subtitle("Open in "+app).
Arg(mapURL(e.Location)).
Valid(true).
Icon(icon).
Var("CALENDAR_APP", "") // Don't open Maps URLs in CALENDAR_APP
}
}
if !opts.ScheduleMode {
// Navigation items
prev := opts.StartTime.AddDate(0, 0, -1)
wf.NewItem("Previous: "+relativeDate(prev)).
Icon(iconPrevious).
Arg(prev.Format(timeFormat)).
Valid(true).
Var("action", "date")
next := opts.StartTime.AddDate(0, 0, 1)
wf.NewItem("Next: "+relativeDate(next)).
Icon(iconNext).
Arg(next.Format(timeFormat)).
Valid(true).
Var("action", "date")
}
if opts.Query != "" {
wf.Filter(opts.Query)
}
if !parsed.IsZero() {
s := parsed.Format(timeFormat)
wf.NewItem(parsed.Format(timeFormatLong)).
Subtitle(relativeDays(parsed, false)).
Arg(s).
Autocomplete(s).
Valid(true).
Icon(iconDefault)
}
wf.WarnEmpty("No Matching Events", "Try a different query?")
wf.SendFeedback()
return nil
}
// loadEvents loads events for given date calendar(s) from cache or server.
func loadEvents(t time.Time, cal ...*Calendar) ([]*Event, error) {
var (
events = []*Event{}
dateStr = t.Format(timeFormat)
name = fmt.Sprintf("events-%s.json", dateStr)
jobName = "update-events"
)
if wf.Cache.Expired(name, opts.MaxAgeEvents()) {
wf.Rerun(0.1)
if !wf.IsRunning(jobName) {
cmd := exec.Command(os.Args[0], "update", "events", dateStr)
if err := wf.RunInBackground(jobName, cmd); err != nil {
return nil, err
}
}
}
if wf.Cache.Exists(name) {
if err := wf.Cache.LoadJSON(name, &events); err != nil {
return nil, err
}
}
// Set map URL
for _, e := range events {
e.MapURL = mapURL(e.Location)
}
return events, nil
}