-
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.
basic wlan widget (requires nm-tool)
- Loading branch information
Showing
3 changed files
with
112 additions
and
2 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
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() | ||
} |
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 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") | ||
}) | ||
}) | ||
}) | ||
} |