breto

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

commit dc73ea06777b8195e53e8d924c02f65b6d24ab44
parent ceab7ae5731b5c93022ac7a3e0b1580fbe223011
Author: J. R. Swab <jrswab@protonmail.com>
Date:   Wed, 29 May 2019 02:11:10 +0000

Merge branch 'libs-routines' into 'master'

Updated libs to go routines.

See merge request jrswab/dwm-status!6
Diffstat:
Mlibs/ram/ram.go | 32+++++++++++++++++++++++---------
Mlibs/wttr/wttr.go | 41+++++++++++++++++++++++++++--------------
Mmain.go | 43+++++++++++++++++++------------------------
3 files changed, 69 insertions(+), 47 deletions(-)

diff --git a/libs/ram/ram.go b/libs/ram/ram.go @@ -1,18 +1,32 @@ package ram import ( + "fmt" "os/exec" "strings" + "time" ) -func Free() string { - // 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 { - return err.Error() +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) } - // return ramGib as string - return strings.TrimSpace(string(ramGib)) } diff --git a/libs/wttr/wttr.go b/libs/wttr/wttr.go @@ -1,24 +1,37 @@ package wttr import ( + "fmt" "io/ioutil" "net/http" "strings" + "time" ) -func Local() *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" - return &errMessage - } - defer resp.Body.Close() // close http request +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 := strings.TrimSpace(string(bodyData)) - return &weather + // 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) + } } diff --git a/main.go b/main.go @@ -4,45 +4,40 @@ import ( "fmt" "gitlab.com/jrswab/dwm-status/libs/ram" "gitlab.com/jrswab/dwm-status/libs/wttr" - "os/exec" - "strings" + //"os/exec" "time" ) func main() { // initial run - var i uint16 = 0 - weather := strings.TrimSpace(string(*wttr.Local())) - ramFree := ram.Free() + weather := "" + ramFree := "" - for i < 3700 { + // get weather data + c := make(chan string) + go wttr.Local(c) + + // get free ram data + cRam := make(chan string) + go ram.Free(cRam) + + for { // to add seconds use "Jan 02, 2006 15:04:05" hTime := time.Now().Format("Jan 02, 2006 15:04") - // time delayed retrievals: - // get weather once per hour - if i == 3600 { - weather = strings.TrimSpace(string(*wttr.Local())) - } - // get free Ram every 3 seconds - if i%3 == 0 { - ramFree = ram.Free() - } // 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(" RAM: %s free | %s | %s ", ramFree, weather, hTime) + status := fmt.Sprintf(" %s%s%s ", ramFree, weather, hTime) - cmd := exec.Command("xsetroot", "-name", status) - cmd.Run() + //cmd := exec.Command("xsetroot", "-name", status) + //cmd.Run() + fmt.Println(status) time.Sleep(1 * time.Second) - // reset i to loop for time delayed updates - if i > 3600 { - i = 0 - } else { - i++ - } + // set channel data for next loop + ramFree = <-cRam + weather = <-c } }