hbspbar

[WIP] bspwm status bar
git clone https://hhvn.uk/hbspbar
git clone git://hhvn.uk/hbspbar
Log | Files | Refs

commit a7e98ba0835c8c7278f298f34fecf6749432fc25
parent 3ad7ace9c1cc88b4ec4eed8c4eee801e1f659110
Author: hhvn <dev@hhvn.uk>
Date:   Sun, 19 Nov 2023 13:31:03 +0000

Status error handling

Diffstat:
Mcommon/common.go | 14++++++++++++++
Mstatus/00-status.go | 20+++++++++++++++++++-
Mstatus/01-bat.go | 133++++++++++++++++++++++++++++++++++++++-----------------------------------------
3 files changed, 97 insertions(+), 70 deletions(-)

diff --git a/common/common.go b/common/common.go @@ -2,8 +2,10 @@ package common // import "hhvn.uk/hbspbar/common" import ( "os" + "io" "fmt" "strconv" + "strings" ) func Perror(function string, err error) error { @@ -19,3 +21,15 @@ func Intify(s string) (int, error) { i, err := strconv.ParseInt(s, 0, 0) return int(i), err } + +func FileAsLines(file string) ([]string, error) { + f, err := os.Open(file) + if err != nil { return nil, err } + + content, err := io.ReadAll(f) + if err != nil { return nil, err } + + lines := strings.Split(string(content), "\n") + + return lines, nil +} diff --git a/status/00-status.go b/status/00-status.go @@ -1,12 +1,14 @@ package status // import "hhvn.uk/hbspbar/status" import ( + "fmt" "time" "sync" "image" - "hhvn.uk/hbspbar/config" "hhvn.uk/hbspbar/drw" + "hhvn.uk/hbspbar/config" + "hhvn.uk/hbspbar/common" ) var Status map[int]*status @@ -75,10 +77,26 @@ func newUpdate(name string) (*status) { return &s } +func checkError(name string, err error) { + if err == nil { return } + + common.Error("status \"%s\": %s\n", name, err) + + u := newUpdate(name) + u.drawText(0, Red, fmt.Sprintf("[err: %s]", name)) + updates <- u + + sleep(5) +} + func sleep(s int) { time.Sleep(time.Duration(s) * time.Second) } +func blendGYR(percent int) uint32 { + return drw.Blend3(Green, Yellow, Red, percent) +} + func (s *status) furthest(x int) { if x > s.W { s.W = x diff --git a/status/01-bat.go b/status/01-bat.go @@ -2,8 +2,8 @@ package status // import "hhvn.uk/hbspbar/status" import ( "os" - "io" "path" + "errors" "strings" // "hhvn.uk/hbspbar/drw" @@ -11,22 +11,7 @@ import ( "hhvn.uk/hbspbar/common" ) -func read(file string) (string, error) { - f, err := os.Open(file) - if err != nil { return "", err } - - content, err := io.ReadAll(f) - if err != nil { return "", err } - - str := strings.TrimSuffix(string(content), "\n") - - return str, nil -} - -func init() { - name := "bat" - register(name) - +func bat(name string) error { dir := "/sys/class/power_supply" const ( @@ -35,75 +20,85 @@ func init() { discharging ) - go func(){ - for { - u := newUpdate(name) + u := newUpdate(name) + + dirs, err := os.ReadDir(dir) + if dirs == nil { return err } - dirs, _ := os.ReadDir(dir) - if dirs == nil { return } + totalcap := 0 + usedcap := 0 - totalcap := 0 - usedcap := 0 + stati := nothing - stati := nothing + for _, d := range dirs { + if !strings.HasPrefix(d.Name(), "BAT") { continue } + + caps, err := common.FileAsLines(path.Join(dir, d.Name(), "capacity")) + if err != nil { return err } - for _, d := range dirs { - if !strings.HasPrefix(d.Name(), "BAT") { continue } - - caps, err := read(path.Join(dir, d.Name(), "capacity")) - if err != nil { return } + capi, err := common.Intify(caps[0]) + if err != nil { return err } - capi, err := common.Intify(caps) - if err != nil { return } + totalcap += 100 + usedcap += capi - totalcap += 100 - usedcap += capi + stats, err := common.FileAsLines(path.Join(dir, d.Name(), "status")) + if err != nil { return err } - stats, err := read(path.Join(dir, d.Name(), "status")) - if err != nil { return } + // Doesn't deal with 1 battery charging, and another discharging but w/e + switch stats[0] { + case "Charging": if charging > stati { stati = charging } + case "Discharging": if discharging > stati { stati = discharging} + } + } - // Doesn't deal with 1 battery charging, and another discharging but w/e - switch stats { - case "Charging": if charging > stati { stati = charging } - case "Discharging": if discharging > stati { stati = discharging} - } - } + if totalcap == 0 { + return errors.New("no batteries") + } - if totalcap == 0 { return } - - avgcap := (usedcap * 100) / totalcap + avgcap := (usedcap * 100) / totalcap - var c uint32 + var c uint32 - // Colour of outline = charge indicator - switch stati { - case nothing: c = config.Fg - case charging: c = Green - case discharging: c = Red - } + // Colour of outline = charge indicator + switch stati { + case nothing: c = config.Fg + case charging: c = Green + case discharging: c = Red + } - var iw int = 20 - var ih int = int(config.H) - 8 - var w int = iw * avgcap / 100 + var iw int = 20 + var ih int = int(config.H) - 8 + var w int = iw * avgcap / 100 - // Draw nose - u.drawRect(2, int((config.H - 5) / 2), 2, 5, c, true) + // Draw nose + u.drawRect(2, int((config.H - 5) / 2), 2, 5, c, true) - // Draw border - u.drawRect(4, 3, iw + 2, ih + 2, c, false) + // Draw border + u.drawRect(4, 3, iw + 2, ih + 2, c, false) - switch { - case avgcap < 25: c = Red - case avgcap >= 99: c = Green - default: c = config.FgDark - } + switch { + case avgcap < 25: c = Red + case avgcap >= 99: c = Green + default: c = config.FgDark + } - // Fill - u.drawRect(4 + iw - w, 4, w + 1, ih, c, true) + // Fill + u.drawRect(4 + iw - w, 4, w + 1, ih, c, true) - updates <- u - sleep(1) - } + updates <- u + sleep(1) + + return nil +} + +func init() { + name := "bat" + register(name) + go func(){ + for { + checkError(name, bat(name)) + } }() }