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

Merge branch 'baustinanki-gcal-format'

This commit is contained in:
Chris Cummer 2018-06-29 15:24:42 -07:00
commit 0d713325dc
5 changed files with 349 additions and 249 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
* https://developers.google.com/calendar/quickstart/go
*
* With some changes by me to improve things a bit.
*/
package gcal
@ -27,7 +29,7 @@ import (
/* -------------------- Exported Functions -------------------- */
func Fetch() (*calendar.Events, error) {
func Fetch() ([]*CalEvent, error) {
ctx := context.Background()
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)
}
}
sort.Slice(events.Items, func(i, j int) bool {
dateA, _ := timeDateChooser(events.Items[i])
dateB, _ := timeDateChooser(events.Items[j])
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 -------------------- */

217
gcal/display.go Normal file
View File

@ -0,0 +1,217 @@
package gcal
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/senorprogrammer/wtf/wtf"
)
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() {
if widget.calEvents == nil || len(widget.calEvents) == 0 {
return
}
widget.mutex.Lock()
defer widget.mutex.Unlock()
_, timedEvents := widget.sortedEvents()
widget.View.SetText(widget.contentFrom(timedEvents))
}
func (widget *Widget) contentFrom(calEvents []*CalEvent) string {
if (calEvents == nil) || (len(calEvents) == 0) {
return ""
}
var str string
var prevEvent *CalEvent
for _, calEvent := range calEvents {
timestamp := fmt.Sprintf("[%s]%s", widget.descriptionColor(calEvent), calEvent.Timestamp())
title := fmt.Sprintf("[%s]%s",
widget.titleColor(calEvent),
widget.eventSummary(calEvent, calEvent.ConflictsWith(calEvents)),
)
lineOne := fmt.Sprintf(
"%s %s %s %s[white]\n",
widget.dayDivider(calEvent, prevEvent),
widget.responseIcon(calEvent),
timestamp,
title,
)
str = str + fmt.Sprintf("%s %s%s\n",
lineOne,
widget.location(calEvent),
widget.timeUntil(calEvent),
)
if (widget.location(calEvent) != "") || (widget.timeUntil(calEvent) != "") {
str = str + "\n"
}
prevEvent = calEvent
}
return str
}
func (widget *Widget) dayDivider(event, prevEvent *CalEvent) string {
var prevStartTime time.Time
if prevEvent != nil {
prevStartTime = prevEvent.Start()
}
if event.Start().Day() != prevStartTime.Day() {
return fmt.Sprintf("[%s::b]",
wtf.Config.UString("wtf.mods.gcal.colors.day", "forestgreen")) +
event.Start().Format(wtf.FullDateFormat) +
"\n"
}
return ""
}
func (widget *Widget) descriptionColor(calEvent *CalEvent) string {
if calEvent.Past() {
return wtf.Config.UString("wtf.mods.gcal.colors.past", "gray")
} else {
return wtf.Config.UString("wtf.mods.gcal.colors.description", "white")
}
}
func (widget *Widget) eventSummary(calEvent *CalEvent, conflict bool) string {
summary := calEvent.event.Summary
if calEvent.Now() {
summary = fmt.Sprintf(
"%s %s",
wtf.Config.UString("wtf.mods.gcal.currentIcon", "🔸"),
summary,
)
}
if conflict {
return fmt.Sprintf("%s %s", wtf.Config.UString("wtf.mods.gcal.conflictIcon", "🚨"), summary)
} else {
return summary
}
}
// timeUntil returns the number of hours or days until the event
// If the event is in the past, returns nil
func (widget *Widget) timeUntil(calEvent *CalEvent) string {
duration := time.Until(calEvent.Start()).Round(time.Minute)
if duration < 0 {
return ""
}
days := duration / (24 * time.Hour)
duration -= days * (24 * time.Hour)
hours := duration / time.Hour
duration -= hours * time.Hour
mins := duration / time.Minute
untilStr := ""
color := "[lightblue]"
if days > 0 {
untilStr = fmt.Sprintf("%dd", days)
} else if hours > 0 {
untilStr = fmt.Sprintf("%dh", hours)
} else {
untilStr = fmt.Sprintf("%dm", mins)
if mins < 30 {
color = "[red]"
}
}
return color + untilStr + "[white]"
}
func (widget *Widget) titleColor(calEvent *CalEvent) string {
color := wtf.Config.UString("wtf.mods.gcal.colors.title", "white")
for _, untypedArr := range wtf.Config.UList("wtf.mods.gcal.colors.highlights") {
highlightElements := wtf.ToStrs(untypedArr.([]interface{}))
match, _ := regexp.MatchString(
strings.ToLower(highlightElements[0]),
strings.ToLower(calEvent.event.Summary),
)
if match == true {
color = highlightElements[1]
}
}
if calEvent.Past() {
color = wtf.Config.UString("wtf.mods.gcal.colors.past", "gray")
}
return color
}
func (widget *Widget) location(calEvent *CalEvent) string {
if wtf.Config.UBool("wtf.mods.gcal.displayLocation", true) == false {
return ""
}
if calEvent.event.Location == "" {
return ""
}
return fmt.Sprintf(
"[%s]%s ",
widget.descriptionColor(calEvent),
calEvent.event.Location,
)
}
func (widget *Widget) responseIcon(calEvent *CalEvent) string {
if false == wtf.Config.UBool("wtf.mods.gcal.displayResponseStatus", true) {
return ""
}
icon := "[gray]"
switch calEvent.ResponseFor(wtf.Config.UString("wtf.mods.gcal.email")) {
case "accepted":
return icon + "✔︎"
case "declined":
return icon + "✘"
case "needsAction":
return icon + "?"
case "tentative":
return icon + "~"
default:
return icon + " "
}
return " "
}

View File

@ -1,22 +1,18 @@
package gcal
import (
"fmt"
"regexp"
"strings"
"sync"
"time"
"github.com/senorprogrammer/wtf/wtf"
"google.golang.org/api/calendar/v3"
)
type Widget struct {
wtf.TextWidget
events *calendar.Events
ch chan struct{}
mutex sync.Mutex
calEvents []*CalEvent
ch chan struct{}
mutex sync.Mutex
}
func NewWidget() *Widget {
@ -32,253 +28,25 @@ func NewWidget() *Widget {
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
events, _ := Fetch()
widget.events = events
widget.UpdateRefreshedAt()
widget.display()
}
func (widget *Widget) Disable() {
close(widget.ch)
widget.TextWidget.Disable()
}
func (widget *Widget) Refresh() {
calEvents, err := Fetch()
if err != nil {
widget.calEvents = []*CalEvent{}
} else {
widget.calEvents = calEvents
}
widget.UpdateRefreshedAt()
widget.display()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) display() {
if widget.events == nil || len(widget.events.Items) == 0 {
return
}
widget.mutex.Lock()
defer widget.mutex.Unlock()
widget.View.SetText(widget.contentFrom(widget.events))
}
// conflicts returns TRUE if this event conflicts with another, FALSE if it does not
func (widget *Widget) conflicts(event *calendar.Event, events *calendar.Events) bool {
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) contentFrom(events *calendar.Events) string {
if events == nil {
return ""
}
var prevEvent *calendar.Event
str := ""
for _, event := range events.Items {
conflict := widget.conflicts(event, events)
str = str + fmt.Sprintf(
"%s %s[%s]%s[white]\n %s[%s]%s %s[white]\n\n",
widget.dayDivider(event, prevEvent),
widget.responseIcon(event),
widget.titleColor(event),
widget.eventSummary(event, conflict),
widget.location(event),
widget.descriptionColor(event),
widget.eventTimestamp(event),
widget.until(event),
)
prevEvent = event
}
return str
}
func (widget *Widget) dayDivider(event, prevEvent *calendar.Event) string {
if prevEvent != nil {
prevStartTime, _ := time.Parse(time.RFC3339, prevEvent.Start.DateTime)
currStartTime, _ := time.Parse(time.RFC3339, event.Start.DateTime)
if currStartTime.Day() != prevStartTime.Day() {
return "\n"
}
}
return ""
}
func (widget *Widget) descriptionColor(event *calendar.Event) string {
color := wtf.Config.UString("wtf.mods.gcal.colors.description", "white")
if widget.eventIsPast(event) {
color = wtf.Config.UString("wtf.mods.gcal.colors.past", "gray")
}
return color
}
func (widget *Widget) eventSummary(event *calendar.Event, conflict bool) string {
summary := event.Summary
if widget.eventIsNow(event) {
summary = fmt.Sprintf(
"%s %s",
wtf.Config.UString("wtf.mods.gcal.currentIcon", "🔸"),
event.Summary,
)
}
if conflict {
return fmt.Sprintf("%s %s", wtf.Config.UString("wtf.mods.gcal.conflictIcon", "🚨"), summary)
} else {
return summary
}
}
func (widget *Widget) eventTimestamp(event *calendar.Event) string {
if len(event.Start.Date) > 0 {
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.FriendlyDateTimeFormat)
}
}
// 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 {
ts, _ := time.Parse(time.RFC3339, event.Start.DateTime)
return (widget.eventIsNow(event) == false) && ts.Before(time.Now())
}
func (widget *Widget) titleColor(event *calendar.Event) string {
color := wtf.Config.UString("wtf.mods.gcal.colors.title", "white")
for _, untypedArr := range wtf.Config.UList("wtf.mods.gcal.colors.highlights") {
highlightElements := wtf.ToStrs(untypedArr.([]interface{}))
match, _ := regexp.MatchString(
strings.ToLower(highlightElements[0]),
strings.ToLower(event.Summary),
)
if match == true {
color = highlightElements[1]
}
}
if widget.eventIsPast(event) {
color = wtf.Config.UString("wtf.mods.gcal.colors.past", "gray")
}
return color
}
func (widget *Widget) location(event *calendar.Event) string {
if wtf.Config.UBool("wtf.mods.gcal.displayLocation", true) == false {
return ""
}
if event.Location == "" {
return ""
}
return fmt.Sprintf(
"[%s]%s\n ",
widget.descriptionColor(event),
event.Location,
)
}
func (widget *Widget) responseIcon(event *calendar.Event) string {
if false == wtf.Config.UBool("wtf.mods.gcal.displayResponseStatus", true) {
return ""
}
response := ""
for _, attendee := range event.Attendees {
if attendee.Email == wtf.Config.UString("wtf.mods.gcal.email") {
response = attendee.ResponseStatus
break
}
}
icon := "[gray]"
switch response {
case "accepted":
icon = icon + "✔︎ "
case "declined":
icon = icon + "✘ "
case "needsAction":
icon = icon + "? "
case "tentative":
icon = icon + "~ "
default:
icon = icon + ""
}
return icon
}
// until returns the number of hours or days until the event
// If the event is in the past, returns nil
func (widget *Widget) until(event *calendar.Event) string {
startTime, _ := time.Parse(time.RFC3339, event.Start.DateTime)
duration := time.Until(startTime)
duration = duration.Round(time.Minute)
if duration < 0 {
return ""
}
days := duration / (24 * time.Hour)
duration -= days * (24 * time.Hour)
hours := duration / time.Hour
duration -= hours * time.Hour
mins := duration / time.Minute
untilStr := ""
if days > 0 {
untilStr = fmt.Sprintf("%dd", days)
} else if hours > 0 {
untilStr = fmt.Sprintf("%dh", hours)
} else {
untilStr = fmt.Sprintf("%dm", mins)
}
return "[lightblue]" + untilStr + "[white]"
}
func updateLoop(widget *Widget) {
interval := wtf.Config.UInt("wtf.mods.gcal.textInterval", 30)
if interval == 0 {

View File

@ -12,6 +12,8 @@ import (
const SimpleDateFormat = "Jan 2"
const SimpleTimeFormat = "15:04 MST"
const MinimumTimeFormat = "15:04"
const FullDateFormat = "Monday, Jan 2"
const FriendlyDateFormat = "Mon, Jan 2"
const FriendlyDateTimeFormat = "Mon, Jan 2, 15:04"
const TimestampFormat = "2006-01-02T15:04:05-0700"