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:
parent
57b0e96d64
commit
c56e99fcec
@ -2,31 +2,62 @@ package weather
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
owm "github.com/briandowns/openweathermap"
|
owm "github.com/briandowns/openweathermap"
|
||||||
"github.com/rivo/tview"
|
"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()
|
data := Fetch()
|
||||||
|
|
||||||
widget := tview.NewTextView()
|
widget.View.SetTitle(fmt.Sprintf(" %s Weather - %s ", icon(data), data.Name))
|
||||||
widget.SetBorder(true)
|
|
||||||
widget.SetDynamicColors(true)
|
|
||||||
widget.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")
|
str := fmt.Sprintf("\n")
|
||||||
for _, weather := range data.Weather {
|
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\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", "High", data.Main.TempMax)
|
||||||
str = str + fmt.Sprintf("%10s: %4.1f° C\n", "Low", data.Main.TempMin)
|
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 str
|
||||||
|
|
||||||
return widget
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// icon returns an emoji for the current weather
|
// icon returns an emoji for the current weather
|
||||||
|
5
wtf.go
5
wtf.go
@ -9,6 +9,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
weather := weather.NewWidget()
|
||||||
|
weather.Refresh()
|
||||||
|
|
||||||
app := tview.NewApplication()
|
app := tview.NewApplication()
|
||||||
|
|
||||||
grid := tview.NewGrid()
|
grid := tview.NewGrid()
|
||||||
@ -19,7 +22,7 @@ func main() {
|
|||||||
grid.AddItem(bamboohr.Widget(), 0, 0, 1, 1, 0, 0, false)
|
grid.AddItem(bamboohr.Widget(), 0, 0, 1, 1, 0, 0, false)
|
||||||
grid.AddItem(gcal.Widget(), 1, 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(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 {
|
if err := app.SetRoot(grid, true).Run(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user