breto

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

commit fc03cd3328ddc965ce82f2930ffa7040d338631e
parent dc73ea06777b8195e53e8d924c02f65b6d24ab44
Author: J. R. Swab <jrswab@protonmail.com>
Date:   Wed, 29 May 2019 19:46:01 +0000

Merge branch 'lib-timers' into 'master'

Moved to a 'ticker' instead of counting rounds with an 'i' variable.

See merge request jrswab/dwm-status!7
Diffstat:
Mlibs/ram/ram.go | 29+++++++++--------------------
Mlibs/wttr/wttr.go | 40++++++++++++++--------------------------
Mmain.go | 52++++++++++++++++++++++++++++++++++++++--------------
3 files changed, 61 insertions(+), 60 deletions(-)

diff --git a/libs/ram/ram.go b/libs/ram/ram.go @@ -4,29 +4,18 @@ import ( "fmt" "os/exec" "strings" - "time" ) func Free(cRam chan string) { ramFree := "" - for i := 0; i < 5; { - if i == 0 || i == 3 { - // 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() - } - // return ramGib as string - ramFree = fmt.Sprintf("Ram: %s free | ", strings.TrimSpace(string(ramGib))) - } - cRam <- ramFree - if i == 3 { - i = 0 - } else { - i++ - } - time.Sleep(1 * time.Second) + // 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() } + // 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 @@ -5,33 +5,21 @@ import ( "io/ioutil" "net/http" "strings" - "time" ) -func Local(c chan string) { - weather := "" - for i := 0; i < 3636; { - // 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 - if i == 0 || i == 3600 { - resp, err := http.Get("https://wttr.in/?format=%t+%w") - if err != nil { - errMessage := "wttr connection issue" - c <- errMessage - } - defer resp.Body.Close() // close http request - - // convert responce to string for return - bodyData, _ := ioutil.ReadAll(resp.Body) - weather = fmt.Sprintf("%s | ", strings.TrimSpace(string(bodyData))) - } - c <- weather - if i == 3600 { - i = 0 - } else { - i++ - } - time.Sleep(1 * time.Second) +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 } + + // 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 } diff --git a/main.go b/main.go @@ -4,40 +4,64 @@ import ( "fmt" "gitlab.com/jrswab/dwm-status/libs/ram" "gitlab.com/jrswab/dwm-status/libs/wttr" - //"os/exec" + "math" + "os/exec" "time" ) func main() { // initial run - weather := "" - ramFree := "" + start := time.Now() + var weather string + var 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() // get weather data - c := make(chan string) - go wttr.Local(c) + cWttr := make(chan string) + go wttr.Local(cWttr) // get free ram data cRam := make(chan string) go ram.Free(cRam) - for { + ticker := time.NewTicker(1 * 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") // 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. - status := fmt.Sprintf(" %s%s%s ", ramFree, weather, hTime) + // Using select here to update the go routine channels as they send data + select { + 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) + } - //cmd := exec.Command("xsetroot", "-name", status) - //cmd.Run() - fmt.Println(status) + // Update output + cmd = exec.Command("xsetroot", "-name", status) + cmd.Run() - time.Sleep(1 * time.Second) + // 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)) - // set channel data for next loop - ramFree = <-cRam - weather = <-c + if fiveSec == 0 { + go ram.Free(cRam) + } + if hourPassed == 0 { + go wttr.Local(cWttr) + } } }