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

Configuration used throughout the app

This commit is contained in:
Chris Cummer
2018-04-04 14:30:05 -07:00
committed by Chris Cummer
parent 45d88c6700
commit c3f1d7ee36
13 changed files with 102 additions and 83 deletions

View File

@@ -8,17 +8,15 @@ import (
/* -------------------- Exported Functions -------------------- */
func Fetch() *owm.CurrentWeatherData {
func Fetch(cityID int) *owm.CurrentWeatherData {
apiKey := os.Getenv("WTF_OWM_API_KEY")
vancouver := 6173331
return currentWeather(apiKey, vancouver)
return currentWeather(apiKey, cityID)
}
/* -------------------- Unexported Functions -------------------- */
func currentWeather(apiKey string, cityCode int) *owm.CurrentWeatherData {
weather, err := owm.NewCurrent("C", "EN", apiKey)
weather, err := owm.NewCurrent(Config.UString("wtf.weather.tempUnit", "C"), Config.UString("wtf.weather.language", "EN"), apiKey)
if err != nil {
panic(err)
}

View File

@@ -7,10 +7,13 @@ import (
owm "github.com/briandowns/openweathermap"
"github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf"
)
var Config *config.Config
type Widget struct {
wtf.BaseWidget
View *tview.TextView
@@ -21,7 +24,7 @@ func NewWidget() *Widget {
BaseWidget: wtf.BaseWidget{
Name: "Weather",
RefreshedAt: time.Now(),
RefreshInt: 900,
RefreshInt: Config.UInt("wtf.weather.refreshInterval", 900),
},
}
@@ -34,7 +37,7 @@ func NewWidget() *Widget {
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
data := Fetch()
data := Fetch(Config.UInt("wtf.weather.cityId", 6176823))
widget.View.SetTitle(fmt.Sprintf(" %s Weather - %s ", icon(data), data.Name))
widget.RefreshedAt = time.Now()
@@ -66,15 +69,18 @@ func (widget *Widget) contentFrom(data *owm.CurrentWeatherData) string {
str = str + strings.Join(descs, ",") + "\n\n"
str = str + fmt.Sprintf("%10s: %4.1f° C\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)
tempUnit := Config.UString("wtf.weather.tempUnit", "C")
str = str + fmt.Sprintf("%10s: %4.1f° %s\n", "Current", data.Main.Temp, tempUnit)
str = str + fmt.Sprintf("%10s: %4.1f° %s\n", "High", data.Main.TempMax, tempUnit)
str = str + fmt.Sprintf("%10s: %4.1f° %s\n", "Low", data.Main.TempMin, tempUnit)
return str
}
// icon returns an emoji for the current weather
// src: https://github.com/chubin/wttr.in/blob/master/share/translations/en.txt
// Note: these only work for English weather status. Sorry about that
func icon(data *owm.CurrentWeatherData) string {
var icon string