breto

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

commit 300b05cd7b8babf472cef46b834fcaaf9715ad25
parent e701b28100643d6f13aa8e5e32ce342957495bf4
Author: J. R. Swab <jrswab@protonmail.com>
Date:   Thu, 23 May 2019 23:22:59 +0000

Merge branch 'custom-packages' into 'master'

Moved functions to their own libraries

See merge request jrswab/dwm-status!3
Diffstat:
Alibs/ram/ram.go | 15+++++++++++++++
Alibs/wttr/wttr.go | 15+++++++++++++++
Mmain.go | 43+++++++++++++++----------------------------
3 files changed, 45 insertions(+), 28 deletions(-)

diff --git a/libs/ram/ram.go b/libs/ram/ram.go @@ -0,0 +1,15 @@ +package ram + +import ( + "os/exec" + "strings" +) + +func Free() string { + ramCmd := "free -h | gawk '/Mem:/ {print $4}'" + ramGib, err := exec.Command("sh", "-c", ramCmd).Output() + if err != nil { + return "Error in 'free -h' command." + } + return strings.TrimSpace(string(ramGib)) +} diff --git a/libs/wttr/wttr.go b/libs/wttr/wttr.go @@ -0,0 +1,15 @@ +package wttr + +import ( + "os/exec" + "strings" +) + +func Local() string { + wttrCmd := "curl -s 'wttr.in/?format=%t+%w'" + wttrRun, err := exec.Command("sh", "-c", wttrCmd).Output() + if err != nil { + return "wttr command returned error!" + } + return strings.TrimSpace(string(wttrRun)) +} diff --git a/main.go b/main.go @@ -1,60 +1,47 @@ package main import ( - "fmt" + "./libs/ram" + "./libs/wttr" "os/exec" "strings" "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() + weather := wttr.Local() + ramFree := ram.Free() for i < 3700 { hTime := time.Now().Format("Jan 02, 2006 15:04") - + // time delayed retrievals: // get weather once per hour if i == 3600 { - wttr = getWttr() + weather = wttr.Local() } - // get free Ram every 3 seconds if i%3 == 0 { - ramFree = getRam() + ramFree = ram.Free() } - // storing desired items as strings - cat := []string{"RAM:", ramFree, "free", "|", wttr, "|", hTime} + // store desired items as strings + // delete or comment out a line to remove from status bar + cat := []string{" ", + "RAM:", ramFree, "free", "|", + weather, "|", + hTime} // concatinate all strings to one line for output status := strings.Join(cat, " ") cmd := exec.Command("xsetroot", "-name", status) cmd.Run() + time.Sleep(1 * time.Second) - // reset i to loop wttr updates + // reset i to loop for time delayed updates if i > 3600 { i = 0 } else {