Skip to content

Commit

Permalink
basic wlan widget (requires nm-tool)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghedamat committed Apr 21, 2014
1 parent 01bf14f commit a5dbf39
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
72 changes: 72 additions & 0 deletions i3status/wlan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package i3status

import (
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
)

type WlanInfo struct {
NetworkStatus string
NetworkName string
color string
}

type WlanWidget struct {
BaseWidget
}

func NewWlanWidget() *WlanWidget {
instanceCount++
w := WlanWidget{
BaseWidget: *NewBaseWidget(),
}
return &w
}

func (w *WlanWidget) basicLoop() {
msg := NewMessage()
msg.Name = "Wlan"
msg.Color = "#ffffff"
msg.Instance = strconv.Itoa(w.Instance)
for {
msg.FullText, msg.Color = w.getStatus()
w.Output <- *msg
time.Sleep(5000 * time.Millisecond)
}
}

func (w *WlanWidget) getStatus() (string, string) {
info := WlanInfo{}
cmd := exec.Command("nm-tool")
str, _ := cmd.Output()
text := fmt.Sprintf("%s", str)
lines := strings.Split(text, "\n")
re := regexp.MustCompile("[a-zA-Z]+\\s*([a-z]+)")
info.NetworkStatus = re.FindAllString(lines[3], -1)[1]

if info.NetworkStatus == "connecting" {
info.color = YELLOW
}

if info.NetworkStatus == "connected" {
info.color = GREEN
}

if info.NetworkStatus == "disconnected" {
info.color = RED
} else {
re = regexp.MustCompile("\\[.*\\]")
info.NetworkName = re.FindAllString(lines[5], -1)[0]
}

return fmt.Sprintf("WLAN: %s %s", info.NetworkStatus, info.NetworkName), info.color
}

func (w *WlanWidget) Start() {
go w.basicLoop()
go w.readLoop()
}
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ func main() {
fmt.Println("[")
b := i3status.NewBar()

//b.Add(i3status.NewDateWidget())
b.Add(i3status.NewDateWidget())
b.Add(i3status.NewTimerWidget())
b.Add(i3status.NewPowerWidget())
b.Add(i3status.NewOnOffWidget())
b.Add(i3status.NewI3statusWidget())
b.Add(i3status.NewWlanWidget())
//b.Add(i3status.NewI3statusWidget())
//b.Add(i3status.NewEchoWidget())

for {
Expand Down
37 changes: 37 additions & 0 deletions test/wlan_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 TestWlanWidgetConstructor(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.NewWlanWidget()
w.SetChannels(c, i)
Convey("output channel is available", func() {
So(w.Output, ShouldEqual, c)
})
})
})
}

func TestWlanWidgetHasMessage(t *testing.T) {
Convey("Given a widget", t, func() {
c := make(chan i3status.Message)
i := make(chan i3status.Entry)
w := i3status.NewWlanWidget()
w.SetChannels(c, i)
Convey("When wlan is started", func() {
w.Start()
Convey("output message is available", func() {
msg := <-c
So(msg.FullText, ShouldContainSubstring, "WLAN")
})
})
})
}

0 comments on commit a5dbf39

Please sign in to comment.