breto

A status bar written in Go.
Log | Files | Refs | README | LICENSE

commit c36668b046ef2b029b653bb4775473f5b288b713
parent 34c5462a7be90b8d6905f308581e769075d7ba54
Author: J. R. Swab <jrswab@protonmail.com>
Date:   Thu, 23 May 2019 00:53:59 +0000

Merge branch 'weather' into 'master'

Weather Status

See merge request jrswab/dwm-status!2
Diffstat:
M.gitignore | 36+++++++++++++++++++++++++++++++++++-
DdwmStatusBar | 0
Mstatus.go | 39+++++++++++++++++++++++++++++++--------
3 files changed, 66 insertions(+), 9 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1 +1,35 @@ -./dwmStatusBar +# Created by https://www.gitignore.io/api/go,linux +# Edit at https://www.gitignore.io/?templates=go,linux + +### Go ### +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +### Go Patch ### +/vendor/ +/Godeps/ + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* diff --git a/dwmStatusBar b/dwmStatusBar Binary files differ. diff --git a/status.go b/status.go @@ -7,23 +7,45 @@ import ( "time" ) +func getWttr() string { + wttrCmd := "curl -s 'wttr.in/?format=2'" + wttrRun, err := exec.Command("sh", "-c", wttrCmd).Output() + if err != nil { + fmt.Println(err) + } + return strings.TrimSpace(string(wttrRun)) +} + +func getRam() string { + ramCmd := "free -h | gawk '/Mem:/ {print $4}'" + ramGib, err := exec.Command("sh", "-c", ramCmd).Output() + if err != nil { + fmt.Println(err) + } + return strings.TrimSpace(string(ramGib)) +} + func main() { + // initial run i := 0 + wttr := getWttr() + ramFree := getRam() for i < 3700 { hTime := time.Now().Format("Jan 02, 2006 15:04") - // get free Ram using Posix compliant shell command - ramCmd := "free -h | gawk '/Mem:/ {print $4}'" - ramGib, err := exec.Command("sh", "-c", ramCmd).Output() - if err != nil { - fmt.Println(err) + // get weather once per hour + if i == 3600 { + wttr = getWttr() + } + + // get free Ram every 3 seconds + if i%3 == 0 { + ramFree = getRam() } - ramFree := strings.TrimSpace(string(ramGib)) // storing desired items as strings - cat := []string{"RAM:", ramFree, - "free", "|", hTime} + cat := []string{"RAM:", ramFree, "free", "|", wttr, "|", hTime} // concatinate all strings to one line for output status := strings.Join(cat, " ") @@ -32,6 +54,7 @@ func main() { cmd.Run() time.Sleep(1 * time.Second) + // reset i to loop and reset wttr counter if i > 3600 { i = 0 } else {