breto

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

commit 63cafdcaf65557bcceb89935cb4341c69884fdf4
parent c9981e63c3e06a0a500b4ea15f4e5db6bf3ea824
Author: J. R. Swab <jrswab@protonmail.com>
Date:   Wed,  5 Jun 2019 01:10:32 +0000

Merge branch 'moarBlocks' into 'master'

Broke down the blocks into more modular pieces.

See merge request jrswab/go-status!14
Diffstat:
Ablocks/disk.go | 33+++++++++++++++++++++++++++++++++
Ablocks/disk_test.go | 27+++++++++++++++++++++++++++
Mblocks/icons.go | 32++++++++++++++++++++++----------
Mblocks/ram.go | 2+-
Mblocks/volume.go | 2+-
Mblocks/wttr.go | 5++---
Mmain.go | 21+++++++++++++++++----
7 files changed, 103 insertions(+), 19 deletions(-)

diff --git a/blocks/disk.go b/blocks/disk.go @@ -0,0 +1,33 @@ +package blocks + +import ( + "fmt" + "math" + "os/exec" + "strings" + "time" +) + +func HomeDisk(cHomeDisk chan string, eHomeDisk chan error) { + // df -h | awk '/home/ {print $4} + var passed, hour float64 + var homeFree string + homeCmd := "df -h | awk '/home/ {print $4}'" + start := time.Now() + ticker := time.NewTicker(time.Second) + + for range ticker.C { + passed = time.Since(start).Seconds() + hour = math.Floor(math.Remainder(passed, 3600)) + + if passed < 5 || hour == 0 { + homeOut, err := exec.Command("sh", "-c", homeCmd).Output() + if err != nil { + eHomeDisk <- err + } + + homeFree = fmt.Sprintf("%s |", strings.TrimSpace(string(homeOut))) + cHomeDisk <- homeFree + } + } +} diff --git a/blocks/disk_test.go b/blocks/disk_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 TestHomeDisk(t *testing.T) { + cHomeDisk := make(chan string) + eHomeDisk := make(chan error) + var homeFree string + var runError error + + go HomeDisk(cHomeDisk, eHomeDisk) + + select { + case homeFree = <-cHomeDisk: + case runError = <-eHomeDisk: + } + + if runError != nil { + t.Error("Expected Free Home Directory Space got:", runError.Error()) + } + fmt.Println(homeFree) +} diff --git a/blocks/icons.go b/blocks/icons.go @@ -16,7 +16,7 @@ func DropboxIcon() (string, error) { return "", err } if string(runDbCmd) != "" { - return fmt.Sprintf("%s ", dbIcon), nil + return fmt.Sprintf("%s", dbIcon), nil } return "", nil } @@ -30,16 +30,16 @@ func RedshiftIcon() (string, error) { return "", err } if string(runRsCmd) != "" { - return fmt.Sprintf("%s ", rsIcon), nil + return fmt.Sprintf("%s", rsIcon), nil } return "", nil } func VolumeIcon() (string, error) { - volIconMute := "" - volIconLow := "" - volIconMid := "" - volIconHigh := "" + 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) @@ -50,14 +50,26 @@ func VolumeIcon() (string, error) { switch { case volValue == 0: - return fmt.Sprintf("%s ", volIconMute), nil + return fmt.Sprintf("%s", volIconMute), nil case volValue < 50: - return fmt.Sprintf("%s ", volIconLow), nil + return fmt.Sprintf("%s", volIconLow), nil case volValue >= 50 && volValue <= 74: - return fmt.Sprintf("%s ", volIconMid), nil + return fmt.Sprintf("%s", volIconMid), nil case volValue >= 75: - return fmt.Sprintf("%s ", volIconHigh), nil + return fmt.Sprintf("%s", volIconHigh), nil default: return "", nil } } + +func DirIcon() string { + return " " +} + +func MemIcon() string { + return " " +} + +func TempIcon() string { + return " " +} diff --git a/blocks/ram.go b/blocks/ram.go @@ -26,7 +26,7 @@ func FreeRam(cRam chan string, eRam chan error) { eRam <- err } - ramFree = fmt.Sprintf("Ram: %s free | ", + ramFree = fmt.Sprintf("%s |", strings.TrimSpace(string(ramGib))) // set string cRam <- ramFree // send string diff --git a/blocks/volume.go b/blocks/volume.go @@ -14,5 +14,5 @@ func VolumeText() (string, error) { } percent := "%" - return fmt.Sprintf("%s%s | ", strings.TrimSpace(string(runVol)), percent), nil + return fmt.Sprintf("%s%s |", strings.TrimSpace(string(runVol)), percent), nil } diff --git a/blocks/wttr.go b/blocks/wttr.go @@ -22,7 +22,7 @@ func Wttr(cWttr chan string, eWttr chan error) { if passed < 10 || hour == 0 { // for options see https://wttr.in/:help - resp, err := http.Get("https://wttr.in/?format=%t+%w") + resp, err := http.Get("https://wttr.in/?format=%t") if err != nil { eWttr <- err } @@ -33,8 +33,7 @@ func Wttr(cWttr chan string, eWttr chan error) { } // convert responce to string for go channel data = string(bodyData) - weather := fmt.Sprintf("%s | ", - strings.TrimSpace(data)) + weather := fmt.Sprintf("%s |", strings.TrimSpace(data)) resp.Body.Close() cWttr <- weather } diff --git a/main.go b/main.go @@ -10,8 +10,10 @@ import ( func main() { // Other Blocks: - var status, hTime, weather, ramFree, rShift, dropbox, volIcon, volText string - var wttrErr, ramErr error + var status, hTime string + var weather, ramFree, homeSpace string // Go Routine Blocks + var tempIco, memIco, rShift, dropbox, volIcon, volText, homeDir string // Icon Blocks + var wttrErr, ramErr, homeErr error // Go routine errors cWttr := make(chan string) // start weather data routine eWttr := make(chan error) @@ -21,6 +23,10 @@ func main() { eRam := make(chan error) go blocks.FreeRam(cRam, eRam) + cHomeDisk := make(chan string) // start free home dir space routine + eHomeDisk := make(chan error) + go blocks.HomeDisk(cHomeDisk, eHomeDisk) + ticker := time.NewTicker(time.Second) for range ticker.C { // add seconds with "Jan 02, 2006 15:04:05" @@ -33,6 +39,9 @@ func main() { case ramFree = <-cRam: case ramErr = <-eRam: log.Println(ramErr.Error()) + case homeSpace = <-cHomeDisk: + case homeErr = <-eHomeDisk: + log.Println(homeErr.Error()) default: } @@ -41,10 +50,14 @@ func main() { dropbox, _ = blocks.DropboxIcon() volText, _ = blocks.VolumeText() volIcon, _ = blocks.VolumeIcon() + homeDir = blocks.DirIcon() + memIco = blocks.MemIcon() + tempIco = blocks.TempIcon() // Change by editing variables & `%s` - status = fmt.Sprintf(" %s%s%s%s%s %s%s", - ramFree, weather, volIcon, volText, hTime, dropbox, rShift) + status = fmt.Sprintf(" %s%s %s%s %s%s %s%s %s %s%s", + tempIco, weather, homeDir, homeSpace, memIco, ramFree, volIcon, volText, + hTime, dropbox, rShift) ui.Dwm(status) // change this to the UI of choice } }