diff --git a/main.go b/main.go index 7eb2fc04..621e302e 100644 --- a/main.go +++ b/main.go @@ -265,7 +265,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w settings := power.NewSettingsFromYAML(wtf.Config) widget = power.NewWidget(app, settings) case "prettyweather": - widget = prettyweather.NewWidget(app) + settings := prettyweather.NewSettingsFromYAML(wtf.Config) + widget = prettyweather.NewWidget(app, settings) case "resourceusage": widget = resourceusage.NewWidget(app) case "security": diff --git a/modules/weatherservices/prettyweather/settings.go b/modules/weatherservices/prettyweather/settings.go new file mode 100644 index 00000000..24fe08b7 --- /dev/null +++ b/modules/weatherservices/prettyweather/settings.go @@ -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 +} diff --git a/modules/weatherservices/prettyweather/widget.go b/modules/weatherservices/prettyweather/widget.go index eb1963a7..6faee73d 100644 --- a/modules/weatherservices/prettyweather/widget.go +++ b/modules/weatherservices/prettyweather/widget.go @@ -11,16 +11,16 @@ import ( type Widget struct { wtf.TextWidget + result string - unit string - city string - view string - language string + settings *Settings } -func NewWidget(app *tview.Application) *Widget { +func NewWidget(app *tview.Application, settings *Settings) *Widget { widget := Widget{ TextWidget: wtf.NewTextWidget(app, "Pretty Weather", "prettyweather", false), + + settings: settings, } return &widget @@ -35,17 +35,18 @@ func (widget *Widget) Refresh() { //this method reads the config and calls wttr.in for pretty weather func (widget *Widget) prettyWeather() { client := &http.Client{} - widget.unit = wtf.Config.UString("wtf.mods.prettyweather.unit", "m") - widget.city = wtf.Config.UString("wtf.mods.prettyweather.city", "") - widget.view = wtf.Config.UString("wtf.mods.prettyweather.view", "0") - widget.language = wtf.Config.UString("wtf.mods.prettyweather.language", "en") - req, err := http.NewRequest("GET", "https://wttr.in/"+widget.city+"?"+widget.view+"?"+widget.unit, nil) + + city := widget.settings.city + unit := widget.settings.unit + view := widget.settings.view + + req, err := http.NewRequest("GET", "https://wttr.in/"+city+"?"+view+"?"+unit, nil) if err != nil { widget.result = err.Error() return } - req.Header.Set("Accept-Language", widget.language) + req.Header.Set("Accept-Language", widget.settings.language) req.Header.Set("User-Agent", "curl") response, err := client.Do(req) if err != nil { @@ -61,6 +62,5 @@ func (widget *Widget) prettyWeather() { return } - //widget.result = strings.TrimSpace(string(contents)) widget.result = strings.TrimSpace(wtf.ASCIItoTviewColors(string(contents))) }