mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
WTF-400 PrettyWeather extracted to new config format
This commit is contained in:
parent
a8e9f69c22
commit
e55cf754b3
3
main.go
3
main.go
@ -265,7 +265,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w
|
|||||||
settings := power.NewSettingsFromYAML(wtf.Config)
|
settings := power.NewSettingsFromYAML(wtf.Config)
|
||||||
widget = power.NewWidget(app, settings)
|
widget = power.NewWidget(app, settings)
|
||||||
case "prettyweather":
|
case "prettyweather":
|
||||||
widget = prettyweather.NewWidget(app)
|
settings := prettyweather.NewSettingsFromYAML(wtf.Config)
|
||||||
|
widget = prettyweather.NewWidget(app, settings)
|
||||||
case "resourceusage":
|
case "resourceusage":
|
||||||
widget = resourceusage.NewWidget(app)
|
widget = resourceusage.NewWidget(app)
|
||||||
case "security":
|
case "security":
|
||||||
|
30
modules/weatherservices/prettyweather/settings.go
Normal file
30
modules/weatherservices/prettyweather/settings.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package prettyweather
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/olebedev/config"
|
||||||
|
"github.com/wtfutil/wtf/cfg"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Settings struct {
|
||||||
|
common *cfg.Common
|
||||||
|
|
||||||
|
city string
|
||||||
|
unit string
|
||||||
|
view string
|
||||||
|
language string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSettingsFromYAML(ymlConfig *config.Config) *Settings {
|
||||||
|
localConfig, _ := ymlConfig.Get("wtf.mods.prettyweather")
|
||||||
|
|
||||||
|
settings := Settings{
|
||||||
|
common: cfg.NewCommonSettingsFromYAML(ymlConfig),
|
||||||
|
|
||||||
|
city: localConfig.UString("city", "Barcelona"),
|
||||||
|
language: localConfig.UString("language", "en"),
|
||||||
|
unit: localConfig.UString("unit", "m"),
|
||||||
|
view: localConfig.UString("view", "0"),
|
||||||
|
}
|
||||||
|
|
||||||
|
return &settings
|
||||||
|
}
|
@ -11,16 +11,16 @@ import (
|
|||||||
|
|
||||||
type Widget struct {
|
type Widget struct {
|
||||||
wtf.TextWidget
|
wtf.TextWidget
|
||||||
|
|
||||||
result string
|
result string
|
||||||
unit string
|
settings *Settings
|
||||||
city string
|
|
||||||
view string
|
|
||||||
language string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWidget(app *tview.Application) *Widget {
|
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||||
widget := Widget{
|
widget := Widget{
|
||||||
TextWidget: wtf.NewTextWidget(app, "Pretty Weather", "prettyweather", false),
|
TextWidget: wtf.NewTextWidget(app, "Pretty Weather", "prettyweather", false),
|
||||||
|
|
||||||
|
settings: settings,
|
||||||
}
|
}
|
||||||
|
|
||||||
return &widget
|
return &widget
|
||||||
@ -35,17 +35,18 @@ func (widget *Widget) Refresh() {
|
|||||||
//this method reads the config and calls wttr.in for pretty weather
|
//this method reads the config and calls wttr.in for pretty weather
|
||||||
func (widget *Widget) prettyWeather() {
|
func (widget *Widget) prettyWeather() {
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
widget.unit = wtf.Config.UString("wtf.mods.prettyweather.unit", "m")
|
|
||||||
widget.city = wtf.Config.UString("wtf.mods.prettyweather.city", "")
|
city := widget.settings.city
|
||||||
widget.view = wtf.Config.UString("wtf.mods.prettyweather.view", "0")
|
unit := widget.settings.unit
|
||||||
widget.language = wtf.Config.UString("wtf.mods.prettyweather.language", "en")
|
view := widget.settings.view
|
||||||
req, err := http.NewRequest("GET", "https://wttr.in/"+widget.city+"?"+widget.view+"?"+widget.unit, nil)
|
|
||||||
|
req, err := http.NewRequest("GET", "https://wttr.in/"+city+"?"+view+"?"+unit, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
widget.result = err.Error()
|
widget.result = err.Error()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Accept-Language", widget.language)
|
req.Header.Set("Accept-Language", widget.settings.language)
|
||||||
req.Header.Set("User-Agent", "curl")
|
req.Header.Set("User-Agent", "curl")
|
||||||
response, err := client.Do(req)
|
response, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -61,6 +62,5 @@ func (widget *Widget) prettyWeather() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//widget.result = strings.TrimSpace(string(contents))
|
|
||||||
widget.result = strings.TrimSpace(wtf.ASCIItoTviewColors(string(contents)))
|
widget.result = strings.TrimSpace(wtf.ASCIItoTviewColors(string(contents)))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user