Skip to content

Commit 04c3eff

Browse files
senorprogrammerChris Cummer
authored andcommitted
Mark calendar start time conflicts
1 parent bd078f0 commit 04c3eff

2 files changed

Lines changed: 46 additions & 15 deletions

File tree

‎gcal/client.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ func Fetch() (*calendar.Events, error) {
5858

5959
/* -------------------- Unexported Functions -------------------- */
6060

61+
func fromMidnight() time.Time {
62+
now := time.Now()
63+
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
64+
}
65+
6166
// getClient uses a Context and Config to retrieve a Token
6267
// then generate a Client. It returns the generated Client.
6368
func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
@@ -92,11 +97,6 @@ func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
9297
return tok
9398
}
9499

95-
func fromMidnight() time.Time {
96-
now := time.Now()
97-
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
98-
}
99-
100100
// tokenCacheFile generates credential file path/filename.
101101
// It returns the generated credential path/filename.
102102
func tokenCacheFile() (string, error) {

‎gcal/widget.go‎

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ func (widget *Widget) contentFrom(events *calendar.Events) string {
6767

6868
str := ""
6969
for _, event := range events.Items {
70+
conflict := widget.hasConflict(event, events)
71+
7072
str = str + fmt.Sprintf(
7173
"%s [%s]%s[white]\n [%s]%s %s[white]\n\n",
7274
widget.dayDivider(event, prevEvent),
7375
widget.titleColor(event),
74-
widget.eventSummary(event),
76+
widget.eventSummary(event, conflict),
7577
widget.descriptionColor(event),
7678
widget.eventTimestamp(event),
7779
widget.until(event),
@@ -96,12 +98,33 @@ func (widget *Widget) dayDivider(event, prevEvent *calendar.Event) string {
9698
return ""
9799
}
98100

99-
func (widget *Widget) eventSummary(event *calendar.Event) string {
101+
func (widget *Widget) descriptionColor(event *calendar.Event) string {
102+
ts, _ := time.Parse(time.RFC3339, event.Start.DateTime)
103+
104+
color := "white"
105+
if (widget.eventIsNow(event) == false) && ts.Before(time.Now()) {
106+
color = "grey"
107+
}
108+
109+
return color
110+
}
111+
112+
func (widget *Widget) eventSummary(event *calendar.Event, conflict bool) string {
113+
summary := event.Summary
114+
100115
if widget.eventIsNow(event) {
101-
return fmt.Sprintf("%s %s", Config.UString("wtf.mods.gcal.currentIcon", "🔸"), event.Summary)
116+
summary = fmt.Sprintf(
117+
"%s %s",
118+
Config.UString("wtf.mods.gcal.currentIcon", "🔸"),
119+
event.Summary,
120+
)
102121
}
103122

104-
return event.Summary
123+
if conflict {
124+
return fmt.Sprintf("%s %s", Config.UString("wtf.mods.gcal.conflictIcon", "🚨"), summary)
125+
} else {
126+
return event.Summary
127+
}
105128
}
106129

107130
func (widget *Widget) eventTimestamp(event *calendar.Event) string {
@@ -117,15 +140,23 @@ func (widget *Widget) eventIsNow(event *calendar.Event) bool {
117140
return time.Now().After(startTime) && time.Now().Before(endTime)
118141
}
119142

120-
func (widget *Widget) descriptionColor(event *calendar.Event) string {
121-
ts, _ := time.Parse(time.RFC3339, event.Start.DateTime)
143+
// hasConflict returns TRUE if this event conflicts with another, FALSE if it does not
144+
// Very basic implementation. Should really operate on ranges
145+
func (widget *Widget) hasConflict(event *calendar.Event, events *calendar.Events) bool {
146+
conflict := false
122147

123-
color := "white"
124-
if (widget.eventIsNow(event) == false) && ts.Before(time.Now()) {
125-
color = "grey"
148+
for _, otherEvent := range events.Items {
149+
if event == otherEvent {
150+
continue
151+
}
152+
153+
if event.Start.DateTime == otherEvent.Start.DateTime {
154+
conflict = true
155+
break
156+
}
126157
}
127158

128-
return color
159+
return conflict
129160
}
130161

131162
func (widget *Widget) titleColor(event *calendar.Event) string {

0 commit comments

Comments
 (0)
⌂ 换网址