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)
|
||||
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":
|
||||
|
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 {
|
||||
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)))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user