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

Better weather formatting

This commit is contained in:
Chris Cummer 2018-03-28 20:19:01 -07:00 committed by Chris Cummer
parent b424655785
commit 07059749d6

View File

@ -1,22 +1,13 @@
package weather
import (
"bytes"
//"bytes"
"fmt"
"text/template"
//"text/template"
"github.com/rivo/tview"
)
const weatherTemplate = `
{{range .Weather}}{{.Description}}{{end}}
Current: {{.Main.Temp}}° C
High: {{.Main.TempMax}}° C
Low: {{.Main.TempMin}}° C
`
func Widget() tview.Primitive {
data := Fetch()
@ -25,13 +16,16 @@ func Widget() tview.Primitive {
widget.SetDynamicColors(true)
widget.SetTitle(fmt.Sprintf(" 🌤 Weather - %s ", data.Name))
var tpl bytes.Buffer
tmpl, _ := template.New("weather").Parse(weatherTemplate)
if err := tmpl.Execute(&tpl, data); err != nil {
panic(err)
str := fmt.Sprintf("\n")
for _, weather := range data.Weather {
str = str + fmt.Sprintf("%16s\n\n", weather.Description)
}
fmt.Fprintf(widget, " %s ", tpl.String())
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)
fmt.Fprintf(widget, " %s ", str)
return widget
}