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

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.
This commit is contained in:
Chris Cummer 2018-03-30 10:10:21 -07:00 committed by Chris Cummer
parent 25898dcb24
commit ab4774c86a
5 changed files with 103 additions and 25 deletions

View File

@ -9,15 +9,18 @@ import (
type Widget struct { type Widget struct {
RefreshedAt time.Time RefreshedAt time.Time
RefreshInterval int
View *tview.TextView View *tview.TextView
} }
func NewWidget() *Widget { func NewWidget() *Widget {
widget := Widget{ widget := Widget{
RefreshedAt: time.Now(), RefreshedAt: time.Now(),
RefreshInterval: 3600,
} }
widget.addView() widget.addView()
go widget.refresher()
return &widget return &widget
} }
@ -66,3 +69,18 @@ func (widget *Widget) display(item Item) string {
return str 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
}
}
}

View File

@ -11,15 +11,18 @@ import (
type Widget struct { type Widget struct {
RefreshedAt time.Time RefreshedAt time.Time
RefreshInterval int
View *tview.TextView View *tview.TextView
} }
func NewWidget() *Widget { func NewWidget() *Widget {
widget := Widget{ widget := Widget{
RefreshedAt: time.Now(), RefreshedAt: time.Now(),
RefreshInterval: 1800,
} }
widget.addView() widget.addView()
go widget.refresher()
return &widget return &widget
} }
@ -84,5 +87,19 @@ func descriptionColor(item *calendar.Event) string {
} }
return color 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
}
}
} }

View File

@ -10,15 +10,18 @@ import (
type Widget struct { type Widget struct {
RefreshedAt time.Time RefreshedAt time.Time
RefreshInterval int
View *tview.TextView View *tview.TextView
} }
func NewWidget() *Widget { func NewWidget() *Widget {
widget := Widget{ widget := Widget{
RefreshedAt: time.Now(), RefreshedAt: time.Now(),
RefreshInterval: 1,
} }
widget.addView() widget.addView()
go widget.refresher()
return &widget return &widget
} }
@ -49,3 +52,18 @@ func (widget *Widget) contentFrom() string {
//return "cats and gods\ndogs and tacs" //return "cats and gods\ndogs and tacs"
return fmt.Sprint(rand.Intn(100)) 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
}
}
}

View File

@ -11,15 +11,18 @@ import (
type Widget struct { type Widget struct {
RefreshedAt time.Time RefreshedAt time.Time
RefreshInterval int
View *tview.TextView View *tview.TextView
} }
func NewWidget() *Widget { func NewWidget() *Widget {
widget := Widget{ widget := Widget{
RefreshedAt: time.Now(), RefreshedAt: time.Now(),
RefreshInterval: 600,
} }
widget.addView() widget.addView()
go widget.refresher()
return &widget return &widget
} }
@ -118,6 +121,21 @@ func icon(data *owm.CurrentWeatherData) string {
return icon 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 { func (widget *Widget) refreshedAt() string {
return widget.RefreshedAt.Format("15:04:05") return widget.RefreshedAt.Format("15:04:05")
} }

31
wtf.go
View File

@ -10,6 +10,21 @@ import (
"github.com/senorprogrammer/wtf/weather" "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() { func main() {
bamboo := bamboohr.NewWidget() bamboo := bamboohr.NewWidget()
bamboo.Refresh() bamboo.Refresh()
@ -23,8 +38,6 @@ func main() {
weather := weather.NewWidget() weather := weather.NewWidget()
weather.Refresh() weather.Refresh()
app := tview.NewApplication()
grid := tview.NewGrid() grid := tview.NewGrid()
grid.SetRows(14, 36, 4) // How _high_ the row is, in terminal rows 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 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(stat.View, 2, 0, 2, 3, 0, 0, false)
grid.AddItem(weather.View, 0, 1, 1, 1, 0, 0, false) grid.AddItem(weather.View, 0, 1, 1, 1, 0, 0, false)
go func() { app := tview.NewApplication()
for {
time.Sleep(900 * time.Second) // 15 minutes // Loop in a routine to redraw the screen
bamboo.Refresh() go refresher(stat, app)
cal.Refresh()
stat.Refresh()
weather.Refresh()
app.Draw()
}
}()
if err := app.SetRoot(grid, true).Run(); err != nil { if err := app.SetRoot(grid, true).Run(); err != nil {
panic(err) panic(err)