breto

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

commit 345fb387cfebe98186ac8bc73c16e56cac147900
parent fc03cd3328ddc965ce82f2930ffa7040d338631e
Author: J. R. Swab <jrswab@protonmail.com>
Date:   Thu, 30 May 2019 01:45:37 +0000

Merge branch 'timers-in-libs' into 'master'

Added tickers to go libs

See merge request jrswab/dwm-status!8
Diffstat:
Mlibs/ram/ram.go | 34++++++++++++++++++++++++----------
Mlibs/wttr/wttr.go | 37++++++++++++++++++++++++-------------
Mmain.go | 45+++++++++------------------------------------
3 files changed, 57 insertions(+), 59 deletions(-)

diff --git a/libs/ram/ram.go b/libs/ram/ram.go @@ -2,20 +2,34 @@ package ram import ( "fmt" + "math" "os/exec" "strings" + "time" ) func Free(cRam chan string) { - ramFree := "" - // set shell command - ramCmd := "free -h | gawk '/Mem:/ {print $4}'" - // run and save the output of ramCmd to ramGib - ramGib, err := exec.Command("sh", "-c", ramCmd).Output() - if err != nil { - cRam <- err.Error() + var passed, tenSecs float64 + start := time.Now() // set to determine seconds passed + ticker := time.NewTicker(time.Second) + + for range ticker.C { // run every 10 seconds + passed = time.Since(start).Seconds() // get total seconds passed + tenSecs = math.Floor(math.Remainder(passed, 10)) + + if passed < 5 || tenSecs == 0 { // trigger: asap or divisible by ten + ramFree := "" + ramCmd := "free -h | gawk '/Mem:/ {print $4}'" // set shell command + + ramGib, err := exec.Command("sh", "-c", ramCmd).Output() // run and save the output + if err != nil { + cRam <- err.Error() + } + + ramFree = fmt.Sprintf("Ram: %s free | ", + strings.TrimSpace(string(ramGib))) // set string + + cRam <- ramFree // send string + } } - // return ramGib as string - ramFree = fmt.Sprintf("Ram: %s free | ", strings.TrimSpace(string(ramGib))) - cRam <- ramFree } diff --git a/libs/wttr/wttr.go b/libs/wttr/wttr.go @@ -3,23 +3,34 @@ package wttr import ( "fmt" "io/ioutil" + "math" "net/http" "strings" + "time" ) func Local(cWttr chan string) { - // get temp(%t) and wind direction/speed (%w) - // for exact location add postal code - wttr.in/~15222?format... - // for more wttr options see https://wttr.in/:help - resp, err := http.Get("https://wttr.in/?format=%t+%w") - if err != nil { - errMessage := "wttr connection issue" - cWttr <- errMessage - } + var passed, hour float64 + start := time.Now() // to determine seconds passed + ticker := time.NewTicker(time.Second) + + for range ticker.C { + passed = time.Since(start).Seconds() // total seconds passed + hour = math.Floor(math.Remainder(passed, 3600)) + if passed < 10 || hour == 0 { + // get temp(%t) and wind direction/speed (%w) + // for exact location add postal code - wttr.in/~15222?format... + // for more wttr options see https://wttr.in/:help + resp, err := http.Get("https://wttr.in/?format=%t+%w") + if err != nil { + errMessage := "wttr connection issue" + cWttr <- errMessage + } - // convert responce to string for return - bodyData, _ := ioutil.ReadAll(resp.Body) - weather := fmt.Sprintf("%s | ", strings.TrimSpace(string(bodyData))) - cWttr <- weather - resp.Body.Close() // close http request + bodyData, _ := ioutil.ReadAll(resp.Body) + weather := fmt.Sprintf("%s | ", + strings.TrimSpace(string(bodyData))) // convert responce to string for return + cWttr <- weather + } + } } diff --git a/main.go b/main.go @@ -4,64 +4,37 @@ import ( "fmt" "gitlab.com/jrswab/dwm-status/libs/ram" "gitlab.com/jrswab/dwm-status/libs/wttr" - "math" "os/exec" "time" ) func main() { // initial run - start := time.Now() - var weather string - var ramFree string + var weather, ramFree string hTime := time.Now().Format("Jan 02, 2006 15:04") status := fmt.Sprintf(" %s ", hTime) - // run first execution without the go routines + cmd := exec.Command("xsetroot", "-name", status) - cmd.Run() + cmd.Run() // run first execution without the go routines - // get weather data - cWttr := make(chan string) + cWttr := make(chan string) // start weather data routine go wttr.Local(cWttr) - // get free ram data - cRam := make(chan string) + cRam := make(chan string) // start free ram data routine go ram.Free(cRam) - ticker := time.NewTicker(1 * time.Second) + ticker := time.NewTicker(time.Second) for range ticker.C { - // get number of seconds since execution - tPassed := time.Since(start).Seconds() - // to add seconds use "Jan 02, 2006 15:04:05" - hTime := time.Now().Format("Jan 02, 2006 15:04") + hTime = time.Now().Format("Jan 02, 2006 15:04") // add seconds with "Jan 02, 2006 15:04:05" - // Change what is displayed by adding or removing variables (eg. weather). - // Change the text output by editing bewteen the `""`. - // Make sure to have the same number of `%s` as variables. - // Using select here to update the go routine channels as they send data - select { + select { // update the go routine channels as they send data case weather = <-cWttr: - status = fmt.Sprintf(" %s%s%s ", ramFree, weather, hTime) case ramFree = <-cRam: - status = fmt.Sprintf(" %s%s%s ", ramFree, weather, hTime) default: - status = fmt.Sprintf(" %s%s%s ", ramFree, weather, hTime) } - // Update output + status = fmt.Sprintf(" %s%s%s ", ramFree, weather, hTime) // Change by editing variables & `%s` cmd = exec.Command("xsetroot", "-name", status) cmd.Run() - - // round down to nearest int, and use the number to set refresh rate per - // library. This is needed to not over use wttr.in - fiveSec := math.Floor(math.Remainder(tPassed, 5)) - hourPassed := math.Floor(math.Remainder(tPassed, 3600)) - - if fiveSec == 0 { - go ram.Free(cRam) - } - if hourPassed == 0 { - go wttr.Local(cWttr) - } } }