breto

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

commit c86c4537dc85d98882234da7e15fea5f585fe048
parent 3d8578a2d8f18178a918335ac0a48f2f7f79b1f9
Author: Jaron Swab <jrswab@gmail.com>
Date:   Wed,  3 Jul 2019 20:31:10 -0400

Merge pull request #1 from jrswab/batteryInfo


Diffstat:
M.gitignore | 1+
MCHANGELOG.md | 8++++++++
Ablocks/battery.go | 19+++++++++++++++++++
Mblocks/disk.go | 2+-
Mblocks/icons.go | 4++++
Mmain.go | 36+++++++++++++++++++++++-------------
6 files changed, 56 insertions(+), 14 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -8,6 +8,7 @@ *.dll *.so *.dylib +go-status # Test binary, built with `go test -c` *.test diff --git a/CHANGELOG.md b/CHANGELOG.md @@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.8.0] - July 2, 2019 +### Added +- Battery percentage for laptops +- Batter power icon +### Changed +- Shell command for disk space from "home" to the partition the binary is run within. +- Month, day, year, time to month, day, time + ## [0.7.2] - June 7, 2019 ### Added - contributing.md diff --git a/blocks/battery.go b/blocks/battery.go @@ -0,0 +1,19 @@ +package blocks + +import ( + "fmt" + "os/exec" + "strings" +) + +func Battery() (string, error) { + cmd := "cat /sys/class/power_supply/BAT0/capacity" + runCmd, err := exec.Command("sh", "-c", cmd).Output() + if err != nil && err.Error() != "exit status 1" { + return "", err + } + + perc := "%" + level := fmt.Sprintf("%s%s |", strings.TrimSpace(string(runCmd)), perc) + return level, nil +} diff --git a/blocks/disk.go b/blocks/disk.go @@ -12,7 +12,7 @@ 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}'" + homeCmd := "df -Ph .| awk '/d*G/ {print $4}'" start := time.Now() ticker := time.NewTicker(time.Second) diff --git a/blocks/icons.go b/blocks/icons.go @@ -88,3 +88,7 @@ func MemIcon() string { func TempIcon() string { return " " } + +func PowerIcon() string { + return " " +} diff --git a/main.go b/main.go @@ -2,23 +2,26 @@ package main import ( "fmt" - "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 + "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 "log" + "math" "time" ) func main() { - // Other Blocks: - var status, hTime string - var weather, ramFree, homeSpace string // Go routine blocks - var wttrErr, ramErr, homeErr error // Go routine errors - var rShift, dropbox, volIcon, volText, syncthing string // Icon blocks + 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 - homeDir := blocks.DirIcon() // These icons are static and - memIco := blocks.MemIcon() // only need defined once + // These are static icons and only need defined at the start + homeDir := blocks.DirIcon() + memIco := blocks.MemIcon() tempIco := blocks.TempIcon() + // Each Go routine has it's own timer to delay the execution of the command. cWttr := make(chan string) // start weather data routine eWttr := make(chan error) go blocks.Wttr(cWttr, eWttr) @@ -32,9 +35,12 @@ func main() { go blocks.HomeDisk(cHomeDisk, eHomeDisk) ticker := time.NewTicker(time.Second) + start := time.Now() for range ticker.C { - // add seconds with "Jan 02, 2006 15:04:05" - hTime = time.Now().Format("Jan 02, 2006 15:04") + // 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)) select { // update the go routine channels as they send data case weather = <-cWttr: @@ -55,11 +61,15 @@ func main() { volText, _ = blocks.VolumeText() volIcon, _ = blocks.VolumeIcon() syncthing, _ = blocks.SyncthingIcon() + bolt = blocks.PowerIcon() + 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", + status = fmt.Sprintf(" %s%s %s%s %s%s %s%s %s%s %s %s%s%s", tempIco, weather, homeDir, homeSpace, memIco, ramFree, volIcon, volText, - hTime, dropbox, syncthing, rShift) + bolt, battery, hTime, dropbox, syncthing, rShift) ui.Dwm(status) // change this to the UI of choice } }