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

Calendar displays number of days/hours/minutes until the listed meeting

This commit is contained in:
Chris Cummer 2018-03-31 13:43:03 -07:00 committed by Chris Cummer
parent 693a05cf43
commit 9756e1885f

View File

@ -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]"
}