-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basic Weather widget (use forecast.io)
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package i3status | ||
|
||
import ( | ||
"fmt" | ||
forecast "github.com/mlbright/forecast/v2" | ||
"io/ioutil" | ||
"log" | ||
"os/user" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type WeatherWidget struct { | ||
BaseWidget | ||
} | ||
|
||
func NewWeatherWidget() *WeatherWidget { | ||
instanceCount++ | ||
w := WeatherWidget{ | ||
BaseWidget: *NewBaseWidget(), | ||
} | ||
return &w | ||
} | ||
|
||
func (w *WeatherWidget) basicLoop() { | ||
msg := NewMessage() | ||
msg.Name = "Wlan" | ||
msg.Color = "#ffffff" | ||
msg.Instance = strconv.Itoa(w.Instance) | ||
usr, _ := user.Current() | ||
keybytes, err := ioutil.ReadFile(usr.HomeDir + "/.forecast.io.rc") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
key := string(keybytes) | ||
key = strings.TrimSpace(key) | ||
|
||
for { | ||
msg.FullText, msg.Color = w.getStatus(key) | ||
w.Output <- *msg | ||
time.Sleep(5000 * time.Millisecond) | ||
} | ||
} | ||
|
||
func (w *WeatherWidget) getStatus(key string) (string, string) { | ||
|
||
lat := "43.6595" | ||
long := "-79.3433" | ||
|
||
f, err := forecast.Get(key, lat, long, "now", forecast.CA) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return fmt.Sprintf("%s: %s %.2f C", f.Timezone, f.Currently.Summary, f.Currently.Temperature), BLUE | ||
} | ||
|
||
func (w *WeatherWidget) Start() { | ||
go w.basicLoop() | ||
go w.readLoop() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package i3status_test | ||
|
||
import ( | ||
"github.com/ghedamat/go-i3status/i3status" | ||
. "github.com/smartystreets/goconvey/convey" | ||
"testing" | ||
) | ||
|
||
func TestWeatherWidgetConstructor(t *testing.T) { | ||
Convey("Given two channels", t, func() { | ||
c := make(chan i3status.Message) | ||
i := make(chan i3status.Entry) | ||
Convey("When wlan is created", func() { | ||
w := i3status.NewWeatherWidget() | ||
w.SetChannels(c, i) | ||
Convey("output channel is available", func() { | ||
So(w.Output, ShouldEqual, c) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
func TestWeatherWidgetHasMessage(t *testing.T) { | ||
Convey("Given a widget", t, func() { | ||
c := make(chan i3status.Message) | ||
i := make(chan i3status.Entry) | ||
w := i3status.NewWeatherWidget() | ||
w.SetChannels(c, i) | ||
Convey("When wlan is started", func() { | ||
w.Start() | ||
Convey("output message is available", func() { | ||
msg := <-c | ||
So(msg.FullText, ShouldContainSubstring, "C") | ||
}) | ||
}) | ||
}) | ||
} |