diff --git a/i3status/utils.go b/i3status/utils.go index 2f416ce..31c684e 100644 --- a/i3status/utils.go +++ b/i3status/utils.go @@ -15,6 +15,7 @@ const ( YELLOW = "#ffff00" GREEN = "#00ff00" WHITE = "#ffffff" + BLUE = "#0000ff" ) func ReadLines(fileName string, callback func(string) bool) { diff --git a/i3status/weather.go b/i3status/weather.go new file mode 100644 index 0000000..2b36a01 --- /dev/null +++ b/i3status/weather.go @@ -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() +} diff --git a/main.go b/main.go index e0b0dc5..3debf56 100644 --- a/main.go +++ b/main.go @@ -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()) diff --git a/test/weather_test.go b/test/weather_test.go new file mode 100644 index 0000000..b462a89 --- /dev/null +++ b/test/weather_test.go @@ -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") + }) + }) + }) +}