Skip to content

Commit

Permalink
add basic Weather widget (use forecast.io)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghedamat committed May 5, 2014
1 parent 62a351f commit 5a8b73d
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions i3status/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
YELLOW = "#ffff00"
GREEN = "#00ff00"
WHITE = "#ffffff"
BLUE = "#0000ff"
)

func ReadLines(fileName string, callback func(string) bool) {
Expand Down
61 changes: 61 additions & 0 deletions i3status/weather.go
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()
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func main() {
b.Add(i3status.NewPowerWidget())
b.Add(i3status.NewOnOffWidget())
b.Add(i3status.NewWlanWidget())
b.Add(i3status.NewWeatherWidget())
//b.Add(i3status.NewI3statusWidget())
//b.Add(i3status.NewEchoWidget())

Expand Down
37 changes: 37 additions & 0 deletions test/weather_test.go
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")
})
})
})
}

0 comments on commit 5a8b73d

Please sign in to comment.