breto

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

commit f16282378c2c5f09735945230598abb8656511c9
parent cb8f9063931c6fbbf961c6bc87b937fc5cd9c364
Author: J. R. Swab <jrswab@protonmail.com>
Date:   Sun,  2 Jun 2019 15:11:41 +0000

Merge branch 'appLibs' into 'master'

App libs

See merge request jrswab/go-status!11
Diffstat:
MCHANGELOG.md | 5+++++
MREADME.md | 67++++++++++++++++++++++++++++++++++++-------------------------------
Ablocks/ram.go | 35+++++++++++++++++++++++++++++++++++
Ablocks/ram_test.go | 27+++++++++++++++++++++++++++
Ablocks/wttr.go | 36++++++++++++++++++++++++++++++++++++
Ablocks/wttr_test.go | 30++++++++++++++++++++++++++++++
Mmain.go | 19+++++++++----------
Dram/ram.go | 35-----------------------------------
Dram/ram_test.go | 27---------------------------
Aui/ui.go | 31+++++++++++++++++++++++++++++++
Aui/ui_test.go | 15+++++++++++++++
Dwttr/wttr.go | 36------------------------------------
Dwttr/wttr_test.go | 28----------------------------
13 files changed, 224 insertions(+), 167 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- UI package to allow for easy switching between user interfaces. + +### Changed +- Moved weather and ram into a 'blocks' package. ## [0.4.0] - May 31, 2019 ### Added diff --git a/README.md b/README.md @@ -1,41 +1,46 @@ # Go Status Bar -This is a script written with Go to display information. Currently tested with DWM and Tmux +This is a script written with Go to display information. Currently tested with DWM, i3wm, and tmux. -## Running in DWM: -1. Download the source -1. Edit as desired -1. `go build` -2. Execute the new binary +## Current Features: +### Master Branch: +#### Blocks: +- Date & Time +- Weather via wttr.in +- Total RAM not used +#### Current UIs: +- DWM +- i3wm +- tmux + +## How To Use: +1. Open main.go in a text editor. +1. If cloned from Github change the custom package directories from Gitlab to Github. +2. Edit the last `status` variable to contain the blocks you wish to use. +3. Change the laste line to match your UI (eg, `ui.Dwm(status)`). +4. Build the binary. +5. Edit your config file to use the new binary. -### DWM Optionals: -1. Set DWM to run the new binary automatically. -2. Set keycombo to run the binary. +### Adding to DWM: +- Simply set `ui.Dwm(status)`, compile, and execute. +- If you already have a startup script for DWM just add a new line for go-status -## Running in Tmux: -1. Comment out or remove: - - `os/exec` - - `var cmd *exec.Cmd` - - `cmd = exec.Command("xsetroot", "-name", status)` - - `cmd.Run()` -2. Add `fmt.Println(status)` to the end of `for range ticker.C` -3. Save and run `go build` within the project directory -4. Add to Tmux: - - `set -g status-right "#($HOME/PATHTO/tmux-status)"` - - if you have colors in this setting add the path at the end of the string - - Be sure to use the correct path and name of the file you built with GO. - - Running `mv dwm-status ~/tmux-status` will allow you to use `"($HOME/tmux-status)" in your config. - - `set -g status-right-length 53` - - if you are not using all the custom packages this number can be lower - - 53 worked well for my setup but if you notice the status getting cut off just increase the number and reload tmux. +### Addng to Tmux: +- `set -g status-right "#($HOME/PATHTO/tmux-status)"` + - If you have colors in this setting add the path at the end of the string + - Be sure to use the correct path and name of the file you built with GO. + - Running `mv go-status ~/tmux-status` will allow you to use `"($HOME/tmux-status)" in your config. +- `set -g status-right-length 53` + - If you are not using all the custom packages this number can be lower + - This also will vary based on screen size. 53 is the minimum that worke for all current blocks + - If you notice the status getting cut off just increase the number and reload tmux. +### Adding to i3wm: +- Change `status_command ...` to `status_command PATH/TO/go-status` -## Wttr.in Optionals: +## Wttr.in Options: 1. Add your area to the weather function - Area Code: 'wttr.in/~00000?format=2' - City: 'wttr.in/~Paris?format=2' + - More information can be found at [wttr.in/:help](https://wttr.in/:help) +2. Add tweaks to `blocks/wttr.go` -## Current Features: -### Master Branch: -- Date & Time -- Weather via wttr.in -- Total RAM not used diff --git a/blocks/ram.go b/blocks/ram.go @@ -0,0 +1,35 @@ +package blocks + +import ( + "fmt" + "math" + "os/exec" + "strings" + "time" +) + +func FreeRam(cRam chan string, eRam chan 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 { + eRam <- err + } + + ramFree = fmt.Sprintf("Ram: %s free | ", + strings.TrimSpace(string(ramGib))) // set string + + cRam <- ramFree // send string + } + } +} diff --git a/blocks/ram_test.go b/blocks/ram_test.go @@ -0,0 +1,27 @@ +package blocks + +import ( + "fmt" + "testing" +) + +// A basic test to make sure the function runs +// and outputs data instead of an error +func TestFreeRam(t *testing.T) { + cRam := make(chan string) + eRam := make(chan error) + var ram string + var runError error + + go FreeRam(cRam, eRam) + + select { + case ram = <-cRam: + case runError = <-eRam: + } + + if runError != nil { + t.Error("Expected Free Ram got:", runError.Error()) + } + fmt.Println(ram) +} diff --git a/blocks/wttr.go b/blocks/wttr.go @@ -0,0 +1,36 @@ +package blocks + +import ( + "fmt" + "io/ioutil" + "math" + "net/http" + "strings" + "time" +) + +func Wttr(cWttr chan string, eWttr chan error) { + 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 { + // for options see https://wttr.in/:help + resp, err := http.Get("https://wttr.in/?format=%t+%w") + if err != nil { + eWttr <- err + } + + bodyData, _ := ioutil.ReadAll(resp.Body) + // convert responce to string for go channel + weather := fmt.Sprintf("%s | ", + strings.TrimSpace(string(bodyData))) + resp.Body.Close() + cWttr <- weather + } + } +} diff --git a/blocks/wttr_test.go b/blocks/wttr_test.go @@ -0,0 +1,30 @@ +package blocks + +import ( + "fmt" + "testing" +) + +// A basic test to make sure the function runs +// and outputs data over instead of an error +func TestWttr(t *testing.T) { + cWttr := make(chan string) + eWttr := make(chan error) + var weather string + var wttrError error + + go Wttr(cWttr, eWttr) + + // wait for weather or error + select { + case weather = <-cWttr: + case wttrError = <-eWttr: + } + + if wttrError != nil { + t.Error("Expected Weather Data got:", wttrError.Error()) + } else if weather == " | " { + t.Error("Weather channel sent an empty string.") + } + fmt.Println(weather) +} diff --git a/main.go b/main.go @@ -2,29 +2,28 @@ package main import ( "fmt" - "gitlab.com/jrswab/dwm-status/ram" - "gitlab.com/jrswab/dwm-status/wttr" + "gitlab.com/jrswab/go-status/blocks" // if pulled from github change to github + "gitlab.com/jrswab/go-status/ui" // if pulled from github change to github "log" - "os/exec" "time" ) func main() { var status, hTime, weather, ramFree string var wttrErr, ramErr error - var cmd *exec.Cmd cWttr := make(chan string) // start weather data routine eWttr := make(chan error) - go wttr.Local(cWttr, eWttr) + go blocks.Wttr(cWttr, eWttr) cRam := make(chan string) // start free ram data routine eRam := make(chan error) - go ram.Free(cRam, eRam) + go blocks.FreeRam(cRam, eRam) ticker := time.NewTicker(time.Second) for range ticker.C { - hTime = time.Now().Format("Jan 02, 2006 15:04") // add seconds with "Jan 02, 2006 15:04:05" + // add seconds with "Jan 02, 2006 15:04:05" + hTime = time.Now().Format("Jan 02, 2006 15:04") select { // update the go routine channels as they send data case weather = <-cWttr: @@ -36,8 +35,8 @@ func main() { default: } - status = fmt.Sprintf(" %s%s%s ", ramFree, weather, hTime) // Change by editing variables & `%s` - cmd = exec.Command("xsetroot", "-name", status) - cmd.Run() + // Change by editing variables & `%s` + status = fmt.Sprintf(" %s%s%s ", ramFree, weather, hTime) + ui.Dwm(status) // change this to the UI of choice } } diff --git a/ram/ram.go b/ram/ram.go @@ -1,35 +0,0 @@ -package ram - -import ( - "fmt" - "math" - "os/exec" - "strings" - "time" -) - -func Free(cRam chan string, eRam chan 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 { - eRam <- err - } - - ramFree = fmt.Sprintf("Ram: %s free | ", - strings.TrimSpace(string(ramGib))) // set string - - cRam <- ramFree // send string - } - } -} diff --git a/ram/ram_test.go b/ram/ram_test.go @@ -1,27 +0,0 @@ -package ram - -import ( - "fmt" - "testing" -) - -// A basic test to make sure the function runs -// and outputs data instead of an error -func TestFree(t *testing.T) { - cRam := make(chan string) - eRam := make(chan error) - var ram string - var runError error - - go Free(cRam, eRam) - - select { // grab first avalible channel - case ram = <-cRam: - case runError = <-eRam: - } - - if runError != nil { - t.Error("Expected Free Ram got:", runError.Error()) - } - fmt.Println(ram) -} diff --git a/ui/ui.go b/ui/ui.go @@ -0,0 +1,31 @@ +package ui + +import ( + "fmt" + "os/exec" +) + +// To have the status blocks appear in dwm we have to run xsetroot -name [status] +// where [status] contains the data blocks +func Dwm(status string) error { + cmd := exec.Command("xsetroot", "-name", status) + err := cmd.Run() + return err +} + +// For tmux we only need to output the block to stdout +// Add the following to your tmux config: +// set -g status-right "#($HOME/PATHTO/go-status)" +// where `go-status` is the compiled binary +func Tmux(status string) { + fmt.Println(status) +} + +// For i3wm we only need to output the block to stdout as we do with tmux. +// Make sure to update the `bar {}` section in your i3wm +// config file to the following: +// status_command $HOME/PATH/TO/go-status +// where `go-status` is the compiled binary +func I3wm(status string) { + fmt.Println(status) +} diff --git a/ui/ui_test.go b/ui/ui_test.go @@ -0,0 +1,15 @@ +package ui + +import ( + "fmt" + "testing" +) + +// Test DWM xsetroot -name command +func TestDwm(t *testing.T) { + status := Dwm("Test Status") + if status != nil { + t.Error("Expected 'Test Status' but got:", status.Error()) + } + fmt.Println(status) +} diff --git a/wttr/wttr.go b/wttr/wttr.go @@ -1,36 +0,0 @@ -package wttr - -import ( - "fmt" - "io/ioutil" - "math" - "net/http" - "strings" - "time" -) - -func Local(cWttr chan string, eWttr chan error) { - 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 { - // for options see https://wttr.in/:help - resp, err := http.Get("https://wttr.in/?format=%t+%w") - if err != nil { - eWttr <- err - } - - bodyData, _ := ioutil.ReadAll(resp.Body) - // convert responce to string for go channel - weather := fmt.Sprintf("%s | ", - strings.TrimSpace(string(bodyData))) - resp.Body.Close() - cWttr <- weather - } - } -} diff --git a/wttr/wttr_test.go b/wttr/wttr_test.go @@ -1,28 +0,0 @@ -package wttr - -import ( - "testing" -) - -// A basic test to make sure the function runs -// and outputs data over instead of an error -func TestLocal(t *testing.T) { - cWttr := make(chan string) - eWttr := make(chan error) - var weather string - var wttrError error - - go Local(cWttr, eWttr) - - // wait for weather or error - select { - case weather = <-cWttr: - case wttrError = <-eWttr: - } - - if wttrError != nil { - t.Error("Expected Weather Data got:", wttrError.Error()) - } else if weather == " | " { - t.Error("Weather channel sent an empty string.") - } -}