From ab4774c86a675f4226471c98d981590d12524b87 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Fri, 30 Mar 2018 10:10:21 -0700 Subject: [PATCH] Widgets refresh via goroutine 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. --- bamboohr/widget.go | 24 +++++++++++++++++++++--- gcal/widget.go | 25 +++++++++++++++++++++---- status/widget.go | 24 +++++++++++++++++++++--- weather/widget.go | 24 +++++++++++++++++++++--- wtf.go | 31 +++++++++++++++++++------------ 5 files changed, 103 insertions(+), 25 deletions(-) diff --git a/bamboohr/widget.go b/bamboohr/widget.go index cfd4c0cc..e69435d5 100644 --- a/bamboohr/widget.go +++ b/bamboohr/widget.go @@ -8,16 +8,19 @@ import ( ) type Widget struct { - RefreshedAt time.Time - View *tview.TextView + RefreshedAt time.Time + RefreshInterval int + View *tview.TextView } func NewWidget() *Widget { widget := Widget{ - RefreshedAt: time.Now(), + RefreshedAt: time.Now(), + RefreshInterval: 3600, } widget.addView() + go widget.refresher() return &widget } @@ -66,3 +69,18 @@ func (widget *Widget) display(item Item) string { return str } + +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 + } + } +} diff --git a/gcal/widget.go b/gcal/widget.go index 8b343529..edb6bb8c 100644 --- a/gcal/widget.go +++ b/gcal/widget.go @@ -10,16 +10,19 @@ import ( ) type Widget struct { - RefreshedAt time.Time - View *tview.TextView + RefreshedAt time.Time + RefreshInterval int + View *tview.TextView } func NewWidget() *Widget { widget := Widget{ - RefreshedAt: time.Now(), + RefreshedAt: time.Now(), + RefreshInterval: 1800, } widget.addView() + go widget.refresher() return &widget } @@ -84,5 +87,19 @@ func descriptionColor(item *calendar.Event) string { } 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 + } + } } diff --git a/status/widget.go b/status/widget.go index bfd63c84..bd0002bc 100644 --- a/status/widget.go +++ b/status/widget.go @@ -9,16 +9,19 @@ import ( ) type Widget struct { - RefreshedAt time.Time - View *tview.TextView + RefreshedAt time.Time + RefreshInterval int + View *tview.TextView } func NewWidget() *Widget { widget := Widget{ - RefreshedAt: time.Now(), + RefreshedAt: time.Now(), + RefreshInterval: 1, } widget.addView() + go widget.refresher() return &widget } @@ -49,3 +52,18 @@ func (widget *Widget) contentFrom() string { //return "cats and gods\ndogs and tacs" return fmt.Sprint(rand.Intn(100)) } + +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 + } + } +} diff --git a/weather/widget.go b/weather/widget.go index 2fc0b64d..8318d0b8 100644 --- a/weather/widget.go +++ b/weather/widget.go @@ -10,16 +10,19 @@ import ( ) type Widget struct { - RefreshedAt time.Time - View *tview.TextView + RefreshedAt time.Time + RefreshInterval int + View *tview.TextView } func NewWidget() *Widget { widget := Widget{ - RefreshedAt: time.Now(), + RefreshedAt: time.Now(), + RefreshInterval: 600, } widget.addView() + go widget.refresher() return &widget } @@ -118,6 +121,21 @@ func icon(data *owm.CurrentWeatherData) string { return icon } +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 + } + } +} + func (widget *Widget) refreshedAt() string { return widget.RefreshedAt.Format("15:04:05") } diff --git a/wtf.go b/wtf.go index 439b70b3..86af9d71 100644 --- a/wtf.go +++ b/wtf.go @@ -10,6 +10,21 @@ import ( "github.com/senorprogrammer/wtf/weather" ) +func refresher(stat *status.Widget, app *tview.Application) { + tick := time.NewTicker(1 * time.Second) + quit := make(chan struct{}) + + for { + select { + case <-tick.C: + app.Draw() + case <-quit: + tick.Stop() + return + } + } +} + func main() { bamboo := bamboohr.NewWidget() bamboo.Refresh() @@ -23,8 +38,6 @@ func main() { weather := weather.NewWidget() weather.Refresh() - app := tview.NewApplication() - grid := tview.NewGrid() grid.SetRows(14, 36, 4) // How _high_ the row is, in terminal rows grid.SetColumns(40, 40) // How _wide_ the column is, in terminal columns @@ -35,16 +48,10 @@ func main() { grid.AddItem(stat.View, 2, 0, 2, 3, 0, 0, false) grid.AddItem(weather.View, 0, 1, 1, 1, 0, 0, false) - go func() { - for { - time.Sleep(900 * time.Second) // 15 minutes - bamboo.Refresh() - cal.Refresh() - stat.Refresh() - weather.Refresh() - app.Draw() - } - }() + app := tview.NewApplication() + + // Loop in a routine to redraw the screen + go refresher(stat, app) if err := app.SetRoot(grid, true).Run(); err != nil { panic(err)