From 69e00348715180703f06ff92c5b92f528e698040 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Thu, 29 Mar 2018 22:26:59 -0700 Subject: [PATCH] Status and weather refresh on a global interval --- status/widget.go | 5 ++++- weather/widget.go | 11 ++++++++++- wtf.go | 11 ++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/status/widget.go b/status/widget.go index 72e51743..bfd63c84 100644 --- a/status/widget.go +++ b/status/widget.go @@ -2,6 +2,7 @@ package status import ( "fmt" + "math/rand" "time" "github.com/rivo/tview" @@ -28,6 +29,7 @@ func (widget *Widget) Refresh() { widget.View.SetTitle(" 🦊 Status ") widget.RefreshedAt = time.Now() + widget.View.Clear() fmt.Fprintf(widget.View, "%s", widget.contentFrom()) } @@ -44,5 +46,6 @@ func (widget *Widget) addView() { } 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)) } diff --git a/weather/widget.go b/weather/widget.go index fe8a8c8d..e6410c9d 100644 --- a/weather/widget.go +++ b/weather/widget.go @@ -2,6 +2,7 @@ package weather import ( "fmt" + "strings" "time" owm "github.com/briandowns/openweathermap" @@ -31,6 +32,7 @@ func (widget *Widget) Refresh() { widget.View.SetTitle(fmt.Sprintf(" %s Weather - %s ", icon(data), data.Name)) widget.RefreshedAt = time.Now() + widget.View.Clear() fmt.Fprintf(widget.View, "%s", widget.contentFrom(data)) } @@ -53,10 +55,13 @@ func centerText(str string, width int) string { func (widget *Widget) contentFrom(data *owm.CurrentWeatherData) string { str := fmt.Sprintf("\n") + descs := []string{} for _, weather := range data.Weather { - str = str + fmt.Sprintf(" %s\n\n", weather.Description) + descs = append(descs, fmt.Sprintf(" %s", weather.Description)) } + str = str + strings.Join(descs, ",") + "\n\n" + str = str + fmt.Sprintf("%10s: %4.1f° C\n\n", "Current", data.Main.Temp) str = str + fmt.Sprintf("%10s: %4.1f° C\n", "High", data.Main.TempMax) str = str + fmt.Sprintf("%10s: %4.1f° C\n", "Low", data.Main.TempMin) @@ -76,6 +81,8 @@ func icon(data *owm.CurrentWeatherData) string { icon = "☀️" case "cloudy": icon = "⛅️" + case "fog": + icon = "🌫" case "heavy rain": icon = "💦" case "heavy snow": @@ -86,6 +93,8 @@ func icon(data *owm.CurrentWeatherData) string { icon = "🌦" case "light snow": icon = "🌨" + case "mist": + icon = "🌬" case "moderate rain": icon = "🌧" case "moderate snow": diff --git a/wtf.go b/wtf.go index db828c45..0a29939c 100644 --- a/wtf.go +++ b/wtf.go @@ -1,7 +1,7 @@ package main import ( - //"time" + "time" "github.com/rivo/tview" "github.com/senorprogrammer/wtf/bamboohr" @@ -35,6 +35,15 @@ 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 + stat.Refresh() + weather.Refresh() + app.Draw() + } + }() + if err := app.SetRoot(grid, true).Run(); err != nil { panic(err) }