breto

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

commit 6e2f521e99196f918a6efabaf6c8d2dda00ce562
parent 885929675c11586424be64790e2613f769b9fff4
Author: Jaron Swab <jrswab@tuta.io>
Date:   Thu, 10 Oct 2019 20:20:58 -0400

Merge pull request #4 from jrswab/develop

Change Variables to structs
Diffstat:
MCHANGELOG.md | 11+++++++++++
MREADME.md | 13++++++++++---
Dblocks/icons.go | 94-------------------------------------------------------------------------------
Dblocks/icons_test.go | 38--------------------------------------
Mblocks/wttr.go | 3+--
Aicons/icons.go | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aicons/icons_test.go | 41+++++++++++++++++++++++++++++++++++++++++
Mmain.go | 66++++++++++++++++++++++++++++++++++++++++++------------------------
8 files changed, 199 insertions(+), 161 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md @@ -5,6 +5,17 @@ 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 +- In code instructions to enable battery status. +- Icons package for clearer code. + +### Changed +- Commented out all battery status code in main.go +- wttr custom error message to used fmt.Errorf instead for errors.New +- Icons from calls like `blocks.VolumeIcon()` to calls like `icons.Volume()`. + +### Removed +- The errors package from wttr.go ## [0.8.1] - July 3, 2019 ## Changed diff --git a/README.md b/README.md @@ -8,7 +8,8 @@ Currently tested with DWM, i3wm, and tmux. - Date & Time - Weather via wttr.in - Total RAM not used -- Total space left in the home directory +- Total disk space left in the binary's present partition. +- Total battery percentage (commented out by default) - Icons #### Current UIs: - DWM @@ -20,7 +21,12 @@ To display icons in DWM. 1. Install FontAwesome v4 2. Set FontAwesome as the second font in `dwm/config.h` - eg. `static const char *fonts[] = { "Source Code Pro:size=13", "FontAwesome:size=14" };` - - Relaunch DWM +3. Relaunch DWM + +To display in tmux or i3wm: +1. Install FontAwesome v4. + - Keep in mind that the icons are currently very small. + - Research in progess. #### Current Icons: - Tempurature:  @@ -30,13 +36,13 @@ To display icons in DWM. - Syncthing:  - Dropbox:  - Redshift:  +- Battery:  Currently, the icons are much too small to be usefull in Tmux or i3wm without further configuration. If you have an easy way to display FontAwesome icons at the same scale as the terminal text please submit a pull request. ## 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 last line to match your UI (eg, `ui.Dwm(status)`). 4. Build the binary. @@ -69,6 +75,7 @@ If you have an easy way to display FontAwesome icons at the same scale as the te ## To-Do: 1. Get a proper name 2. Scale icons in tmux and i3wm + - [This Unix Stack Exchange post may help](http://unix.stackexchange.com/questions/49779/ddg#49823) ## License: MIT diff --git a/blocks/icons.go b/blocks/icons.go @@ -1,94 +0,0 @@ -package blocks - -import ( - "encoding/binary" - "fmt" - "os/exec" -) - -func DropboxIcon() (string, error) { - // ps ux | awk '/dropbox/ {print $11}' | grep dropbox - dbIcon := "" - dbCmd := "ps ux | gawk '/dropbox/ {print $11}' | grep dropbox" - runDbCmd, err := exec.Command("sh", "-c", dbCmd).Output() - - if err != nil && err.Error() != "exit status 1" { - return "", err - } - if string(runDbCmd) != "" { - return fmt.Sprintf("%s", dbIcon), nil - } - return "", nil -} - -func RedshiftIcon() (string, error) { - rsIcon := "" - rsCmd := "ps ux | gawk '/redshift/ {print $11}' | grep redshift" - runRsCmd, err := exec.Command("sh", "-c", rsCmd).Output() - - if err != nil && err.Error() != "exit status 1" { - return "", err - } - if string(runRsCmd) != "" { - return fmt.Sprintf("%s", rsIcon), nil - } - return "", nil -} - -func SyncthingIcon() (string, error) { - syncIcon := "" - syncCmd := "ps ux | gawk '/syncthing/ {print $11}' | grep syncthing" - runSyncCmd, err := exec.Command("sh", "-c", syncCmd).Output() - - if err != nil && err.Error() != "exit status 1" { - return "", err - } - if string(runSyncCmd) != "" { - return fmt.Sprintf("%s", syncIcon), nil - } - return "", nil -} - -func VolumeIcon() (string, error) { - volIconMute := " " - volIconLow := " " - volIconMid := " " - volIconHigh := " " - volCmd := "amixer -D pulse sget Master | awk '/Front Right:/ {print $5}' | grep -o '[0-9]*'" - runVolCmd, err := exec.Command("sh", "-c", volCmd).Output() - volValue := binary.LittleEndian.Uint16(runVolCmd) - - if err != nil && err.Error() != "exit status 1" { - return "", err - } - - switch { - case volValue == 0: - return fmt.Sprintf("%s", volIconMute), nil - case volValue < 50: - return fmt.Sprintf("%s", volIconLow), nil - case volValue >= 50 && volValue <= 74: - return fmt.Sprintf("%s", volIconMid), nil - case volValue >= 75: - return fmt.Sprintf("%s", volIconHigh), nil - default: - return "", nil - } -} - -// The following have no checks that need to be made -func DirIcon() string { - return " " -} - -func MemIcon() string { - return " " -} - -func TempIcon() string { - return " " -} - -func PowerIcon() string { - return " " -} diff --git a/blocks/icons_test.go b/blocks/icons_test.go @@ -1,38 +0,0 @@ -package blocks - -import ( - "fmt" - "testing" -) - -func TestDropboxIcon(t *testing.T) { - db, err := DropboxIcon() - if err != nil { - t.Error(err) //.Error()) - } - fmt.Println(db, err) -} - -func TestRedshiftIcon(t *testing.T) { - rs, err := RedshiftIcon() - if err != nil { - t.Error(err) - } - fmt.Println(rs, err) -} - -func TestVolumeIcon(t *testing.T) { - vi, err := VolumeIcon() - if err != nil { - t.Error(err) - } - fmt.Println(vi) -} - -func TestSyncthingIcon(t *testing.T) { - sync, err := SyncthingIcon() - if err != nil { - t.Error(err) //.Error()) - } - fmt.Println(sync, err) -} diff --git a/blocks/wttr.go b/blocks/wttr.go @@ -1,7 +1,6 @@ package blocks import ( - "errors" "fmt" "io/ioutil" "math" @@ -41,7 +40,7 @@ func Wttr(cWttr chan string, eWttr chan error) { weather = fmt.Sprintf("%s |", strings.TrimSpace(data)) cWttr <- weather } else { - eWttr <- errors.New("wttr.in overloaded") + eWttr <- fmt.Errorf("Expected temp, got: %s", data) } } } diff --git a/icons/icons.go b/icons/icons.go @@ -0,0 +1,94 @@ +package icons + +import ( + "encoding/binary" + "fmt" + "os/exec" +) + +func Dropbox() (string, error) { + // ps ux | awk '/dropbox/ {print $11}' | grep dropbox + dbIcon := "" + dbCmd := "ps ux | gawk '/dropbox/ {print $11}' | grep dropbox" + runDbCmd, err := exec.Command("sh", "-c", dbCmd).Output() + + if err != nil && err.Error() != "exit status 1" { + return "", err + } + if string(runDbCmd) != "" { + return fmt.Sprintf("%s", dbIcon), nil + } + return "", nil +} + +func Redshift() (string, error) { + rsIcon := "" + rsCmd := "ps ux | gawk '/redshift/ {print $11}' | grep redshift" + runRsCmd, err := exec.Command("sh", "-c", rsCmd).Output() + + if err != nil && err.Error() != "exit status 1" { + return "", err + } + if string(runRsCmd) != "" { + return fmt.Sprintf("%s", rsIcon), nil + } + return "", nil +} + +func Syncthing() (string, error) { + syncIcon := "" + syncCmd := "ps ux | gawk '/syncthing/ {print $11}' | grep syncthing" + runSyncCmd, err := exec.Command("sh", "-c", syncCmd).Output() + + if err != nil && err.Error() != "exit status 1" { + return "", err + } + if string(runSyncCmd) != "" { + return fmt.Sprintf("%s", syncIcon), nil + } + return "", nil +} + +func Volume() (string, error) { + volIconMute := " " + volIconLow := " " + volIconMid := " " + volIconHigh := " " + volCmd := "amixer -D pulse sget Master | awk '/Front Right:/ {print $5}' | grep -o '[0-9]*'" + runVolCmd, err := exec.Command("sh", "-c", volCmd).Output() + volValue := binary.LittleEndian.Uint16(runVolCmd) + + if err != nil && err.Error() != "exit status 1" { + return "", err + } + + switch { + case volValue == 0: + return fmt.Sprintf("%s", volIconMute), nil + case volValue < 50: + return fmt.Sprintf("%s", volIconLow), nil + case volValue >= 50 && volValue <= 74: + return fmt.Sprintf("%s", volIconMid), nil + case volValue >= 75: + return fmt.Sprintf("%s", volIconHigh), nil + default: + return "", nil + } +} + +// The following have no checks that need to be made +func Dir() string { + return " " +} + +func Mem() string { + return " " +} + +func Temp() string { + return " " +} + +func Power() string { + return " " +} diff --git a/icons/icons_test.go b/icons/icons_test.go @@ -0,0 +1,41 @@ +package icons + +import ( + "fmt" + "testing" +) + +// These tests are for icon functions that rely on other data. +// Any icon that simply displays no matter the status is now tested. + +func TestDropbox(t *testing.T) { + db, err := Dropbox() + if err != nil { + t.Error(err) //.Error()) + } + fmt.Println(db, err) +} + +func TestRedshift(t *testing.T) { + rs, err := Redshift() + if err != nil { + t.Error(err) + } + fmt.Println(rs, err) +} + +func TestVolume(t *testing.T) { + vi, err := Volume() + if err != nil { + t.Error(err) + } + fmt.Println(vi) +} + +func TestSyncthing(t *testing.T) { + sync, err := Syncthing() + if err != nil { + t.Error(err) //.Error()) + } + fmt.Println(sync, err) +} diff --git a/main.go b/main.go @@ -2,24 +2,35 @@ package main import ( "fmt" - "github.com/jrswab/go-status/blocks" // if pulled from github change to github - "github.com/jrswab/go-status/ui" // if pulled from github change to github + "github.com/jrswab/go-status/blocks" + "github.com/jrswab/go-status/icons" + "github.com/jrswab/go-status/ui" "log" - "math" "time" ) +// To Add Battery Status: +// Uncomment all lines that follow the message: +// "Uncomment for battery status" +// Also, add the correct string formatting to +// the status assignment at the end of the file and add "math" to the imports + func main() { - var status, hTime, battery string - var weather, ramFree, homeSpace string // Go routine blocks - var wttrErr, ramErr, homeErr error // Go routine errors - var rShift, dropbox, volIcon, volText, syncthing, bolt string // Icon blocks - var passed, fiveMins float64 + var weather, ramFree, homeSpace string // Go routine blocks + var wttrErr, ramErr, homeErr error // Go routine errors + var status, hTime, rShift, dropbox, volIcon, volText, syncthing string + + // Uncomment for battery status + /* + var bolt, battery string + var passed, fiveMins float64 + start := time.Now() + */ // These are static icons and only need defined at the start - homeDir := blocks.DirIcon() - memIco := blocks.MemIcon() - tempIco := blocks.TempIcon() + homeDir := icons.Dir() + memIco := icons.Mem() + tempIco := icons.Temp() // Each Go routine has it's own timer to delay the execution of the command. cWttr := make(chan string) // start weather data routine @@ -35,12 +46,15 @@ func main() { go blocks.HomeDisk(cHomeDisk, eHomeDisk) ticker := time.NewTicker(time.Second) - start := time.Now() for range ticker.C { // add year & seconds with "Jan 02, 2006 15:04:05" hTime = time.Now().Format("Jan 02 15:04") - passed = time.Since(start).Seconds() - fiveMins = math.Floor(math.Remainder(passed, 300)) + + // Uncomment for battery status + /* + passed = time.Since(start).Seconds() + fiveMins = math.Floor(math.Remainder(passed, 300)) + */ select { // update the go routine channels as they send data case weather = <-cWttr: @@ -56,20 +70,24 @@ func main() { } // Assign Icons & non Go Routine blocks every round - rShift, _ = blocks.RedshiftIcon() - dropbox, _ = blocks.DropboxIcon() + rShift, _ = icons.Redshift() + dropbox, _ = icons.Dropbox() volText, _ = blocks.VolumeText() - volIcon, _ = blocks.VolumeIcon() - syncthing, _ = blocks.SyncthingIcon() - bolt = blocks.PowerIcon() - if fiveMins == 0 || passed < 10 { - battery, _ = blocks.Battery() - } + volIcon, _ = icons.Volume() + syncthing, _ = icons.Syncthing() + + // Uncomment for battery status + /* + bolt = icons.Power() + if fiveMins == 0 || passed < 10 { + battery, _ = blocks.Battery() + } + */ // Change by editing variables & `%s` - status = fmt.Sprintf(" %s%s %s%s %s%s %s%s %s%s %s %s%s%s", + status = fmt.Sprintf(" %s%s %s%s %s%s %s%s %s %s%s%s", tempIco, weather, homeDir, homeSpace, memIco, ramFree, volIcon, volText, - bolt, battery, hTime, dropbox, syncthing, rShift) + hTime, dropbox, syncthing, rShift) ui.Dwm(status) // change this to the UI of choice } }