mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Remove complexity from a lot of string display statements
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package prettyweather
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -29,7 +28,7 @@ func (widget *Widget) Refresh() {
|
||||
widget.UpdateRefreshedAt()
|
||||
widget.prettyWeather()
|
||||
|
||||
widget.View.SetText(fmt.Sprintf("%s", widget.result))
|
||||
widget.View.SetText(widget.result)
|
||||
}
|
||||
|
||||
//this method reads the config and calls wttr.in for pretty weather
|
||||
@@ -40,21 +39,24 @@ func (widget *Widget) prettyWeather() {
|
||||
widget.view = wtf.Config.UString("wtf.mods.prettyweather.view", "0")
|
||||
req, err := http.NewRequest("GET", "https://wttr.in/"+widget.city+"?"+widget.view+"?"+widget.unit, nil)
|
||||
if err != nil {
|
||||
widget.result = fmt.Sprintf("%s", err.Error())
|
||||
widget.result = err.Error()
|
||||
return
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "curl")
|
||||
response, err := client.Do(req)
|
||||
if err != nil {
|
||||
widget.result = fmt.Sprintf("%s", err.Error())
|
||||
widget.result = err.Error()
|
||||
return
|
||||
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
contents, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
widget.result = fmt.Sprintf("%s", err.Error())
|
||||
widget.result = err.Error()
|
||||
return
|
||||
}
|
||||
widget.result = fmt.Sprintf("%s", strings.TrimSpace(string(contents)))
|
||||
|
||||
widget.result = strings.TrimSpace(string(contents))
|
||||
}
|
||||
|
||||
@@ -9,32 +9,31 @@ import (
|
||||
)
|
||||
|
||||
func (widget *Widget) display() {
|
||||
widget.View.Clear()
|
||||
|
||||
if widget.apiKeyValid() == false {
|
||||
fmt.Fprintf(widget.View, "%s", " Environment variable WTF_OWM_API_KEY is not set")
|
||||
widget.View.SetText(" Environment variable WTF_OWM_API_KEY is not set")
|
||||
return
|
||||
}
|
||||
|
||||
cityData := widget.currentData()
|
||||
if cityData == nil {
|
||||
fmt.Fprintf(widget.View, "%s", " Weather data is unavailable (1)")
|
||||
widget.View.SetText(" Weather data is unavailable: no city data")
|
||||
return
|
||||
}
|
||||
|
||||
if len(cityData.Weather) == 0 {
|
||||
fmt.Fprintf(widget.View, "%s", " Weather data is unavailable (2)")
|
||||
widget.View.SetText(" Weather data is unavailable: no weather data")
|
||||
return
|
||||
}
|
||||
|
||||
widget.View.SetTitle(widget.title(cityData))
|
||||
|
||||
str := wtf.SigilStr(len(widget.Data), widget.Idx, widget.View) + "\n"
|
||||
str = str + widget.description(cityData) + "\n\n"
|
||||
str = str + widget.temperatures(cityData) + "\n"
|
||||
str = str + widget.sunInfo(cityData)
|
||||
content := wtf.SigilStr(len(widget.Data), widget.Idx, widget.View) + "\n"
|
||||
content = content + widget.description(cityData) + "\n\n"
|
||||
content = content + widget.temperatures(cityData) + "\n"
|
||||
content = content + widget.sunInfo(cityData)
|
||||
|
||||
fmt.Fprintf(widget.View, "%s", str)
|
||||
widget.View.SetText(content)
|
||||
}
|
||||
|
||||
func (widget *Widget) description(cityData *owm.CurrentWeatherData) string {
|
||||
@@ -73,5 +72,5 @@ func (widget *Widget) temperatures(cityData *owm.CurrentWeatherData) string {
|
||||
}
|
||||
|
||||
func (widget *Widget) title(cityData *owm.CurrentWeatherData) string {
|
||||
return fmt.Sprintf(" %s %s ", widget.icon(cityData), cityData.Name)
|
||||
return fmt.Sprintf(" %s %s ", widget.icon(cityData), cityData.Name)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user