From 9756e1885fce885964e4766cf9b8c5f88135856b Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sat, 31 Mar 2018 13:43:03 -0700 Subject: [PATCH] Calendar displays number of days/hours/minutes until the listed meeting --- gcal/widget.go | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/gcal/widget.go b/gcal/widget.go index 24cffdec..8158eefd 100644 --- a/gcal/widget.go +++ b/gcal/widget.go @@ -58,10 +58,11 @@ func (widget *Widget) contentFrom(events *calendar.Events) string { str := "\n" for _, item := range events.Items { - ts, _ := time.Parse(time.RFC3339, item.Start.DateTime) - timestamp := ts.Format("Mon, Jan 2 - 15:04") + startTime, _ := time.Parse(time.RFC3339, item.Start.DateTime) + timestamp := startTime.Format("Mon, Jan 2, 15:04") + until := widget.until(startTime) - str = str + fmt.Sprintf(" [%s]%s[white]\n [%s]%s[white]\n\n", titleColor(item), item.Summary, descriptionColor(item), timestamp) + str = str + fmt.Sprintf(" [%s]%s[white]\n [%s]%s %s[white]\n\n", titleColor(item), item.Summary, descriptionColor(item), timestamp, until) } return str @@ -107,3 +108,31 @@ func (widget *Widget) refresher() { } } } + +// until returns the number of hours or days until the event +// If the event is in the past, returns nil +func (widget *Widget) until(start time.Time) string { + duration := time.Until(start) + + duration = duration.Round(time.Minute) + + 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 "[grey]" + untilStr + "[white]" +}