mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Added PrettyWeather mod
This commit is contained in:
parent
5d30e09821
commit
21ee436e8c
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
||||
.DS_Store
|
||||
gcal/client_secret.json
|
||||
#intellij idea
|
||||
.idea/
|
||||
|
66
prettyweather/widget.go
Normal file
66
prettyweather/widget.go
Normal file
@ -0,0 +1,66 @@
|
||||
package prettyweather
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/olebedev/config"
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Config is a pointer to the global config object
|
||||
var Config *config.Config
|
||||
|
||||
type Widget struct {
|
||||
wtf.TextWidget
|
||||
result string
|
||||
unit string
|
||||
city string
|
||||
}
|
||||
|
||||
func NewWidget() *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget("Pretty Weather", "prettyweather", false),
|
||||
}
|
||||
|
||||
widget.View.SetWrap(true)
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
if widget.Disabled() {
|
||||
return
|
||||
}
|
||||
|
||||
widget.UpdateRefreshedAt()
|
||||
widget.prettyWeather()
|
||||
widget.View.Clear()
|
||||
widget.View.SetTitle(fmt.Sprintf(" %s ", widget.Name))
|
||||
|
||||
fmt.Fprintf(widget.View, "%s", widget.result)
|
||||
}
|
||||
|
||||
//this method reads the config and calls wttr.in for pretty weather
|
||||
func (widget *Widget) prettyWeather() {
|
||||
client := &http.Client{}
|
||||
widget.unit, widget.city = Config.UString("wtf.mods.prettyweather.unit", "m"), Config.UString("wtf.mods.prettyweather.city", "")
|
||||
req, err := http.NewRequest("GET", "https://wttr.in/"+widget.city+"?0"+"?"+widget.unit, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "curl")
|
||||
response, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Printf("%s", err)
|
||||
} else {
|
||||
defer response.Body.Close()
|
||||
contents, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
fmt.Printf("%s", err)
|
||||
}
|
||||
widget.result = fmt.Sprintf("%s", strings.TrimSpace(string(contents)))
|
||||
}
|
||||
}
|
5
wtf.go
5
wtf.go
@ -17,10 +17,12 @@ import (
|
||||
"github.com/senorprogrammer/wtf/git"
|
||||
"github.com/senorprogrammer/wtf/github"
|
||||
"github.com/senorprogrammer/wtf/help"
|
||||
"github.com/senorprogrammer/wtf/ipinfo"
|
||||
"github.com/senorprogrammer/wtf/jira"
|
||||
"github.com/senorprogrammer/wtf/newrelic"
|
||||
"github.com/senorprogrammer/wtf/opsgenie"
|
||||
"github.com/senorprogrammer/wtf/power"
|
||||
"github.com/senorprogrammer/wtf/prettyweather"
|
||||
"github.com/senorprogrammer/wtf/security"
|
||||
"github.com/senorprogrammer/wtf/status"
|
||||
"github.com/senorprogrammer/wtf/system"
|
||||
@ -28,7 +30,6 @@ import (
|
||||
"github.com/senorprogrammer/wtf/todo"
|
||||
"github.com/senorprogrammer/wtf/weather"
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
"github.com/senorprogrammer/wtf/ipinfo"
|
||||
)
|
||||
|
||||
/* -------------------- Functions -------------------- */
|
||||
@ -170,6 +171,7 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) {
|
||||
textfile.Config = Config
|
||||
todo.Config = Config
|
||||
weather.Config = Config
|
||||
prettyweather.Config = Config
|
||||
wtf.Config = Config
|
||||
|
||||
Widgets = []wtf.Wtfable{
|
||||
@ -190,6 +192,7 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) {
|
||||
textfile.NewWidget(app, pages),
|
||||
todo.NewWidget(app, pages),
|
||||
weather.NewWidget(app, pages),
|
||||
prettyweather.NewWidget(),
|
||||
}
|
||||
|
||||
FocusTracker = wtf.FocusTracker{
|
||||
|
Loading…
x
Reference in New Issue
Block a user