mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
All widgets now refresh their own data using their own internal go routine. This allows them to set their own update schedule (where RefreshInterval is the time in seconds between refreshes). The app uses a goroutine to redraw itself once a second.
106 lines
1.9 KiB
Go
106 lines
1.9 KiB
Go
package gcal
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/rivo/tview"
|
|
"google.golang.org/api/calendar/v3"
|
|
)
|
|
|
|
type Widget struct {
|
|
RefreshedAt time.Time
|
|
RefreshInterval int
|
|
View *tview.TextView
|
|
}
|
|
|
|
func NewWidget() *Widget {
|
|
widget := Widget{
|
|
RefreshedAt: time.Now(),
|
|
RefreshInterval: 1800,
|
|
}
|
|
|
|
widget.addView()
|
|
go widget.refresher()
|
|
|
|
return &widget
|
|
}
|
|
|
|
/* -------------------- Exported Functions -------------------- */
|
|
|
|
func (widget *Widget) Refresh() {
|
|
events := Fetch()
|
|
|
|
widget.View.SetTitle(" 🐸 Calendar ")
|
|
widget.RefreshedAt = time.Now()
|
|
|
|
fmt.Fprintf(widget.View, "%s", widget.contentFrom(events))
|
|
}
|
|
|
|
/* -------------------- Unexported Functions -------------------- */
|
|
|
|
func (widget *Widget) addView() {
|
|
view := tview.NewTextView()
|
|
|
|
view.SetBorder(true)
|
|
view.SetDynamicColors(true)
|
|
view.SetTitle(" Calendar ")
|
|
|
|
widget.View = view
|
|
}
|
|
|
|
func (widget *Widget) contentFrom(events *calendar.Events) string {
|
|
str := ""
|
|
|
|
for _, item := range events.Items {
|
|
ts, _ := time.Parse(time.RFC3339, item.Start.DateTime)
|
|
timestamp := ts.Format("Mon Jan _2 15:04:05 2006")
|
|
|
|
str = str + fmt.Sprintf(" [%s]%s[white]\n [%s]%s[white]\n\n", titleColor(item), item.Summary, descriptionColor(item), timestamp)
|
|
}
|
|
|
|
return str
|
|
}
|
|
|
|
func titleColor(item *calendar.Event) string {
|
|
ts, _ := time.Parse(time.RFC3339, item.Start.DateTime)
|
|
|
|
color := "red"
|
|
if strings.Contains(item.Summary, "1on1") {
|
|
color = "green"
|
|
}
|
|
|
|
if ts.Before(time.Now()) {
|
|
color = "grey"
|
|
}
|
|
|
|
return color
|
|
}
|
|
|
|
func descriptionColor(item *calendar.Event) string {
|
|
ts, _ := time.Parse(time.RFC3339, item.Start.DateTime)
|
|
|
|
color := "white"
|
|
if ts.Before(time.Now()) {
|
|
color = "grey"
|
|
}
|
|
|
|
return color
|
|
}
|
|
|
|
func (widget *Widget) refresher() {
|
|
tick := time.NewTicker(time.Duration(widget.RefreshInterval) * time.Second)
|
|
quit := make(chan struct{})
|
|
|
|
for {
|
|
select {
|
|
case <-tick.C:
|
|
widget.Refresh()
|
|
case <-quit:
|
|
tick.Stop()
|
|
return
|
|
}
|
|
}
|
|
}
|