1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Wrap Google's calendar Event in a CalEvent struct

This commit is contained in:
Chris Cummer 2018-06-29 12:25:25 -07:00
parent 3f010f8a9f
commit f8dabfb800
4 changed files with 200 additions and 134 deletions

104
gcal/cal_event.go Normal file
View File

@ -0,0 +1,104 @@
package gcal
import (
"time"
"github.com/senorprogrammer/wtf/wtf"
"google.golang.org/api/calendar/v3"
)
type CalEvent struct {
event *calendar.Event
}
func NewCalEvent(event *calendar.Event) *CalEvent {
calEvent := CalEvent{
event: event,
}
return &calEvent
}
/* -------------------- Exported Functions -------------------- */
func (calEvent *CalEvent) AllDay() bool {
return len(calEvent.event.Start.Date) > 0
}
func (calEvent *CalEvent) ConflictsWith(otherEvents []*CalEvent) bool {
hasConflict := false
for _, otherEvent := range otherEvents {
if calEvent.event == otherEvent.event {
continue
}
if calEvent.Start().Before(otherEvent.End()) && calEvent.End().After(otherEvent.Start()) {
hasConflict = true
break
}
}
return hasConflict
}
func (calEvent *CalEvent) Now() bool {
return time.Now().After(calEvent.Start()) && time.Now().Before(calEvent.End())
}
func (calEvent *CalEvent) Past() bool {
if calEvent.AllDay() {
// FIXME: This should calculate properly
return false
} else {
return (calEvent.Now() == false) && calEvent.Start().Before(time.Now())
}
}
func (calEvent *CalEvent) ResponseFor(email string) string {
for _, attendee := range calEvent.event.Attendees {
if attendee.Email == email {
return attendee.ResponseStatus
}
}
return ""
}
/* -------------------- DateTimes -------------------- */
func (calEvent *CalEvent) End() time.Time {
var calcTime string
if calEvent.AllDay() {
calcTime = calEvent.event.End.Date
} else {
calcTime = calEvent.event.End.DateTime
}
end, _ := time.Parse(time.RFC3339, calcTime)
return end
}
func (calEvent *CalEvent) Start() time.Time {
var calcTime string
if calEvent.AllDay() {
calcTime = calEvent.event.Start.Date
} else {
calcTime = calEvent.event.Start.DateTime
}
start, _ := time.Parse(time.RFC3339, calcTime)
return start
}
func (calEvent *CalEvent) Timestamp() string {
if calEvent.AllDay() {
startTime, _ := time.Parse("2006-01-02", calEvent.event.Start.Date)
return startTime.Format(wtf.FriendlyDateFormat)
} else {
startTime, _ := time.Parse(time.RFC3339, calEvent.event.Start.DateTime)
return startTime.Format(wtf.MinimumTimeFormat)
}
}

View File

@ -1,6 +1,8 @@
/* /*
* This butt-ugly code is direct from Google itself * This butt-ugly code is direct from Google itself
* https://developers.google.com/calendar/quickstart/go * https://developers.google.com/calendar/quickstart/go
*
* With some changes by me to improve things a bit.
*/ */
package gcal package gcal
@ -27,7 +29,7 @@ import (
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func Fetch() (*calendar.Events, error) { func Fetch() ([]*CalEvent, error) {
ctx := context.Background() ctx := context.Background()
secretPath, _ := wtf.ExpandHomeDir(wtf.Config.UString("wtf.mods.gcal.secretFile")) secretPath, _ := wtf.ExpandHomeDir(wtf.Config.UString("wtf.mods.gcal.secretFile"))
@ -75,13 +77,20 @@ func Fetch() (*calendar.Events, error) {
return time.Parse(time.RFC3339, event.Start.DateTime) return time.Parse(time.RFC3339, event.Start.DateTime)
} }
} }
sort.Slice(events.Items, func(i, j int) bool { sort.Slice(events.Items, func(i, j int) bool {
dateA, _ := timeDateChooser(events.Items[i]) dateA, _ := timeDateChooser(events.Items[i])
dateB, _ := timeDateChooser(events.Items[j]) dateB, _ := timeDateChooser(events.Items[j])
return dateA.Before(dateB) return dateA.Before(dateB)
}) })
return &events, err // Wrap the calendar events in our custom CalEvent
calEvents := []*CalEvent{}
for _, event := range events.Items {
calEvents = append(calEvents, NewCalEvent(event))
}
return calEvents, err
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */

View File

@ -7,96 +7,107 @@ import (
"time" "time"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
"google.golang.org/api/calendar/v3"
) )
func (widget *Widget) sortedEvents() ([]*CalEvent, []*CalEvent) {
allDayEvents := []*CalEvent{}
timedEvents := []*CalEvent{}
for _, calEvent := range widget.calEvents {
if calEvent.AllDay() {
allDayEvents = append(allDayEvents, calEvent)
} else {
timedEvents = append(timedEvents, calEvent)
}
}
return allDayEvents, timedEvents
}
func (widget *Widget) display() { func (widget *Widget) display() {
if widget.events == nil || len(widget.events.Items) == 0 { if widget.calEvents == nil || len(widget.calEvents) == 0 {
return return
} }
widget.mutex.Lock() widget.mutex.Lock()
defer widget.mutex.Unlock() defer widget.mutex.Unlock()
widget.View.SetText(widget.contentFrom(widget.events)) _, timedEvents := widget.sortedEvents()
widget.View.SetText(widget.contentFrom(timedEvents))
} }
func (widget *Widget) contentFrom(events *calendar.Events) string { func (widget *Widget) contentFrom(calEvents []*CalEvent) string {
if events == nil { if (calEvents == nil) || (len(calEvents) == 0) {
return "" return ""
} }
var prevEvent *calendar.Event var str string
var prevEvent *CalEvent
str := "" for _, calEvent := range calEvents {
timestamp := fmt.Sprintf("[%s]%s", widget.descriptionColor(calEvent), calEvent.Timestamp())
for _, event := range events.Items {
timestamp := fmt.Sprintf("[%s]%s",
widget.descriptionColor(event),
widget.eventTimestamp(event))
title := fmt.Sprintf("[%s]%s", title := fmt.Sprintf("[%s]%s",
widget.titleColor(event), widget.titleColor(calEvent),
widget.eventSummary(event, widget.conflicts(event, events))) widget.eventSummary(calEvent, calEvent.ConflictsWith(calEvents)),
)
lineOne := fmt.Sprintf( lineOne := fmt.Sprintf(
"%s %s %s %s %s[white]", "%s %s %s %s %s[white]",
widget.dayDivider(event, prevEvent), widget.dayDivider(calEvent, prevEvent),
widget.responseIcon(event), widget.responseIcon(calEvent),
timestamp, timestamp,
title, title,
widget.timeUntil(event), widget.timeUntil(calEvent),
) )
str = str + fmt.Sprintf("%s%s\n\n", str = str + fmt.Sprintf("%s%s\n\n",
lineOne, lineOne,
widget.location(event), // prefixes newline if non-empty widget.location(calEvent),
) )
prevEvent = event prevEvent = calEvent
} }
return str return str
} }
func (widget *Widget) dayDivider(event, prevEvent *calendar.Event) string { func (widget *Widget) dayDivider(event, prevEvent *CalEvent) string {
var prevStartTime time.Time var prevStartTime time.Time
if prevEvent != nil { if prevEvent != nil {
prevStartTime, _ = time.Parse(time.RFC3339, prevEvent.Start.DateTime) prevStartTime = prevEvent.Start()
} }
currStartTime, _ := time.Parse(time.RFC3339, event.Start.DateTime) if event.Start().Day() != prevStartTime.Day() {
//_, _, width, _ := widget.View.GetInnerRect()
if currStartTime.Day() != prevStartTime.Day() { return fmt.Sprintf("[%s::b]",
_, _, width, _ := widget.View.GetInnerRect() wtf.Config.UString("wtf.mods.gcal.colors.day", "forestgreen")) +
//wtf.CenterText(event.Start().Format(wtf.FullDateFormat), width) +
return fmt.Sprintf("[%s]", wtf.Config.UString("wtf.mods.gcal.colors.day", "forestgreen")) + event.Start().Format(wtf.FullDateFormat) +
wtf.CenterText(currStartTime.Format(wtf.FullDateFormat), width) +
"\n" "\n"
} }
return "" return ""
} }
func (widget *Widget) descriptionColor(event *calendar.Event) string { func (widget *Widget) descriptionColor(calEvent *CalEvent) string {
color := wtf.Config.UString("wtf.mods.gcal.colors.description", "white") if calEvent.Past() {
return wtf.Config.UString("wtf.mods.gcal.colors.past", "gray")
if widget.eventIsPast(event) { } else {
color = wtf.Config.UString("wtf.mods.gcal.colors.past", "gray") return wtf.Config.UString("wtf.mods.gcal.colors.description", "white")
} }
return color
} }
func (widget *Widget) eventSummary(event *calendar.Event, conflict bool) string { func (widget *Widget) eventSummary(calEvent *CalEvent, conflict bool) string {
summary := event.Summary summary := calEvent.event.Summary
if widget.eventIsNow(event) { if calEvent.Now() {
summary = fmt.Sprintf( summary = fmt.Sprintf(
"%s %s", "%s %s",
wtf.Config.UString("wtf.mods.gcal.currentIcon", "🔸"), wtf.Config.UString("wtf.mods.gcal.currentIcon", "🔸"),
event.Summary, summary,
) )
} }
@ -107,21 +118,10 @@ func (widget *Widget) eventSummary(event *calendar.Event, conflict bool) string
} }
} }
func (widget *Widget) eventTimestamp(event *calendar.Event) string { // timeUntil returns the number of hours or days until the event
if widget.eventIsAllDay(event) {
startTime, _ := time.Parse("2006-01-02", event.Start.Date)
return startTime.Format(wtf.FriendlyDateFormat)
} else {
startTime, _ := time.Parse(time.RFC3339, event.Start.DateTime)
return startTime.Format(wtf.MinimumTimeFormat)
}
}
// timeUuntil returns the number of hours or days until the event
// If the event is in the past, returns nil // If the event is in the past, returns nil
func (widget *Widget) timeUntil(event *calendar.Event) string { func (widget *Widget) timeUntil(calEvent *CalEvent) string {
startTime, _ := time.Parse(time.RFC3339, event.Start.DateTime) duration := time.Until(calEvent.Start()).Round(time.Minute)
duration := time.Until(startTime).Round(time.Minute)
if duration < 0 { if duration < 0 {
return "" return ""
@ -152,7 +152,7 @@ func (widget *Widget) timeUntil(event *calendar.Event) string {
return color + untilStr + "[white]" return color + untilStr + "[white]"
} }
func (widget *Widget) titleColor(event *calendar.Event) string { func (widget *Widget) titleColor(calEvent *CalEvent) string {
color := wtf.Config.UString("wtf.mods.gcal.colors.title", "white") color := wtf.Config.UString("wtf.mods.gcal.colors.title", "white")
for _, untypedArr := range wtf.Config.UList("wtf.mods.gcal.colors.highlights") { for _, untypedArr := range wtf.Config.UList("wtf.mods.gcal.colors.highlights") {
@ -160,7 +160,7 @@ func (widget *Widget) titleColor(event *calendar.Event) string {
match, _ := regexp.MatchString( match, _ := regexp.MatchString(
strings.ToLower(highlightElements[0]), strings.ToLower(highlightElements[0]),
strings.ToLower(event.Summary), strings.ToLower(calEvent.event.Summary),
) )
if match == true { if match == true {
@ -168,51 +168,47 @@ func (widget *Widget) titleColor(event *calendar.Event) string {
} }
} }
if widget.eventIsPast(event) { if calEvent.Past() {
color = wtf.Config.UString("wtf.mods.gcal.colors.past", "gray") color = wtf.Config.UString("wtf.mods.gcal.colors.past", "gray")
} }
return color return color
} }
func (widget *Widget) location(event *calendar.Event) string { func (widget *Widget) location(calEvent *CalEvent) string {
if wtf.Config.UBool("wtf.mods.gcal.displayLocation", true) == false { if wtf.Config.UBool("wtf.mods.gcal.displayLocation", true) == false {
return "" return ""
} }
if event.Location == "" { if calEvent.event.Location == "" {
return "" return ""
} }
return fmt.Sprintf( return fmt.Sprintf(
"\n [%s]%s", "\n [%s]%s",
widget.descriptionColor(event), widget.descriptionColor(calEvent),
event.Location, calEvent.event.Location,
) )
} }
func (widget *Widget) responseIcon(event *calendar.Event) string { func (widget *Widget) responseIcon(calEvent *CalEvent) string {
if false == wtf.Config.UBool("wtf.mods.gcal.displayResponseStatus", true) { if false == wtf.Config.UBool("wtf.mods.gcal.displayResponseStatus", true) {
return "" return ""
} }
for _, attendee := range event.Attendees { icon := "[gray]"
if attendee.Email == wtf.Config.UString("wtf.mods.gcal.email") {
icon := "[gray]"
switch attendee.ResponseStatus { switch calEvent.ResponseFor(wtf.Config.UString("wtf.mods.gcal.email")) {
case "accepted": case "accepted":
return icon + "✔︎" return icon + "✔︎"
case "declined": case "declined":
return icon + "✘" return icon + "✘"
case "needsAction": case "needsAction":
return icon + "?" return icon + "?"
case "tentative": case "tentative":
return icon + "~" return icon + "~"
default: default:
return icon + " " return icon + " "
}
}
} }
return " " return " "

View File

@ -5,15 +5,14 @@ import (
"time" "time"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
"google.golang.org/api/calendar/v3"
) )
type Widget struct { type Widget struct {
wtf.TextWidget wtf.TextWidget
events *calendar.Events calEvents []*CalEvent
ch chan struct{} ch chan struct{}
mutex sync.Mutex mutex sync.Mutex
} }
func NewWidget() *Widget { func NewWidget() *Widget {
@ -29,67 +28,25 @@ func NewWidget() *Widget {
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
events, _ := Fetch()
widget.events = events
widget.UpdateRefreshedAt()
widget.display()
}
func (widget *Widget) Disable() { func (widget *Widget) Disable() {
close(widget.ch) close(widget.ch)
widget.TextWidget.Disable() widget.TextWidget.Disable()
} }
/* -------------------- Unexported Functions -------------------- */ func (widget *Widget) Refresh() {
calEvents, err := Fetch()
// conflicts returns TRUE if this event conflicts with another, FALSE if it does not if err != nil {
func (widget *Widget) conflicts(event *calendar.Event, events *calendar.Events) bool { widget.calEvents = []*CalEvent{}
conflict := false
for _, otherEvent := range events.Items {
if event == otherEvent {
continue
}
eventStart, _ := time.Parse(time.RFC3339, event.Start.DateTime)
eventEnd, _ := time.Parse(time.RFC3339, event.End.DateTime)
otherEnd, _ := time.Parse(time.RFC3339, otherEvent.End.DateTime)
otherStart, _ := time.Parse(time.RFC3339, otherEvent.Start.DateTime)
if eventStart.Before(otherEnd) && eventEnd.After(otherStart) {
conflict = true
break
}
}
return conflict
}
func (widget *Widget) eventIsAllDay(event *calendar.Event) bool {
return len(event.Start.Date) > 0
}
// eventIsNow returns true if the event is happening now, false if it not
func (widget *Widget) eventIsNow(event *calendar.Event) bool {
startTime, _ := time.Parse(time.RFC3339, event.Start.DateTime)
endTime, _ := time.Parse(time.RFC3339, event.End.DateTime)
return time.Now().After(startTime) && time.Now().Before(endTime)
}
func (widget *Widget) eventIsPast(event *calendar.Event) bool {
if widget.eventIsAllDay(event) {
return false
} else { } else {
ts, _ := time.Parse(time.RFC3339, event.Start.DateTime) widget.calEvents = calEvents
return (widget.eventIsNow(event) == false) && ts.Before(time.Now())
} }
widget.UpdateRefreshedAt()
widget.display()
} }
/* -------------------- Unexported Functions -------------------- */
func updateLoop(widget *Widget) { func updateLoop(widget *Widget) {
interval := wtf.Config.UInt("wtf.mods.gcal.textInterval", 30) interval := wtf.Config.UInt("wtf.mods.gcal.textInterval", 30)
if interval == 0 { if interval == 0 {