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

WIP Extending weather to be a self-contained, updatable widget

This commit is contained in:
Chris Cummer 2018-03-29 04:01:23 -07:00 committed by Chris Cummer
parent 57b0e96d64
commit c56e99fcec
2 changed files with 44 additions and 10 deletions

View File

@ -2,31 +2,62 @@ package weather
import (
"fmt"
"time"
owm "github.com/briandowns/openweathermap"
"github.com/rivo/tview"
)
func Widget() tview.Primitive {
type Widget struct {
RefreshedAt time.Time
View *tview.TextView
}
func NewWidget() *Widget {
widget := Widget{
RefreshedAt: time.Now(),
}
widget.addView()
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
data := Fetch()
widget := tview.NewTextView()
widget.SetBorder(true)
widget.SetDynamicColors(true)
widget.SetTitle(fmt.Sprintf(" %s Weather - %s ", icon(data), data.Name))
widget.View.SetTitle(fmt.Sprintf(" %s Weather - %s ", icon(data), data.Name))
fmt.Fprintf(widget.View, " %s ", widget.content(data))
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) addView() {
view := tview.NewTextView()
view.SetBorder(true)
view.SetDynamicColors(true)
view.SetTitle("Weather")
widget.View = view
}
func (widget *Widget) content(data *owm.CurrentWeatherData) string {
str := fmt.Sprintf("\n")
for _, weather := range data.Weather {
str = str + fmt.Sprintf("%16s\n\n", weather.Description)
str = str + fmt.Sprintf(" %16s\n\n", weather.Description)
}
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)
str = str + "\n\n\n\n"
str = str + fmt.Sprintf(" Refreshed at %s", widget.RefreshedAt)
fmt.Fprintf(widget, " %s ", str)
return widget
return str
}
// icon returns an emoji for the current weather

5
wtf.go
View File

@ -9,6 +9,9 @@ import (
)
func main() {
weather := weather.NewWidget()
weather.Refresh()
app := tview.NewApplication()
grid := tview.NewGrid()
@ -19,7 +22,7 @@ func main() {
grid.AddItem(bamboohr.Widget(), 0, 0, 1, 1, 0, 0, false)
grid.AddItem(gcal.Widget(), 1, 0, 1, 1, 0, 0, false)
grid.AddItem(status.Widget(), 2, 0, 2, 3, 0, 0, false)
grid.AddItem(weather.Widget(), 0, 1, 1, 1, 0, 0, false)
grid.AddItem(weather.View, 0, 1, 1, 1, 0, 0, false)
if err := app.SetRoot(grid, true).Run(); err != nil {
panic(err)