From 42ce7f4353eac3f951407052798743cd2644820d Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sat, 31 Mar 2018 08:17:57 -0700 Subject: [PATCH] Display current wifi network name --- security/client.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++ security/widget.go | 15 +++++++++--- weather/widget.go | 2 ++ 3 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 security/client.go diff --git a/security/client.go b/security/client.go new file mode 100644 index 00000000..de634eb9 --- /dev/null +++ b/security/client.go @@ -0,0 +1,58 @@ +package security + +import ( + "io/ioutil" + "os/exec" + "regexp" + //"runtime" + //"strings" +) + +const fwGlobalState = "/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate" +const fwStealthMode = "/usr/libexec/ApplicationFirewall/socketfilterfw --getstealthmode" + +// https://github.com/yelinaung/wifi-name/blob/master/wifi-name.go +const osxCmd = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport" +const osxArgs = "-I" + +func Fetch() map[string]string { + data := make(map[string]string) + + data["Wifi"] = WifiName() + + return data +} + +func WifiName() string { + cmd := exec.Command(osxCmd, osxArgs) + + stdout, err := cmd.StdoutPipe() + panicIf(err) + + // start the command after having set up the pipe + if err := cmd.Start(); err != nil { + panic(err) + } + + var str string + + if b, err := ioutil.ReadAll(stdout); err == nil { + str += (string(b) + "\n") + } + + r := regexp.MustCompile(`s*SSID: (.+)s*`) + + name := r.FindAllStringSubmatch(str, -1) + + if len(name) <= 1 { + return "" + } else { + return name[1][1] + } +} + +func panicIf(err error) { + if err != nil { + panic(err) + } +} diff --git a/security/widget.go b/security/widget.go index e3270b30..27668da5 100644 --- a/security/widget.go +++ b/security/widget.go @@ -31,15 +31,14 @@ func NewWidget() *Widget { /* -------------------- Exported Functions -------------------- */ func (widget *Widget) Refresh() { - //data := Fetch() + data := Fetch() widget.View.SetTitle(" 🐼 Security") widget.RefreshedAt = time.Now() widget.View.Clear() - fmt.Fprintf(widget.View, "%s", "cats and dogs") - //fmt.Fprintf(widget.View, "%s", widget.contentFrom(data)) + fmt.Fprintf(widget.View, "%s", widget.contentFrom(data)) } /* -------------------- Unexported Functions -------------------- */ @@ -54,6 +53,16 @@ func (widget *Widget) addView() { widget.View = view } +func (widget *Widget) contentFrom(data map[string]string) string { + str := "\n" + + for key, val := range data { + str = str + fmt.Sprintf("%8s: %8s\n", key, val) + } + + return str +} + func (widget *Widget) refresher() { tick := time.NewTicker(time.Duration(widget.RefreshInterval) * time.Second) quit := make(chan struct{}) diff --git a/weather/widget.go b/weather/widget.go index 1a65c7ca..f7e3b3bf 100644 --- a/weather/widget.go +++ b/weather/widget.go @@ -87,6 +87,8 @@ func icon(data *owm.CurrentWeatherData) string { icon = "☁️" case "clear": icon = "☀️" + case "clear sky": + icon = "☀️" case "cloudy": icon = "⛅️" case "few clouds":