From ac6db6c9c392bc35fceb9b8123a3619c18a9b242 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Thu, 13 Jan 2022 11:31:15 +0100 Subject: Add support for specifying the batteries to display --- statusbar.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/statusbar.go b/statusbar.go index 8480995..e5cf559 100644 --- a/statusbar.go +++ b/statusbar.go @@ -13,6 +13,7 @@ import ( "time" "flag" "errors" + "path/filepath" "github.com/BurntSushi/toml" "github.com/google/shlex" @@ -79,6 +80,7 @@ type ClockConfig struct { type BatteryConfig struct { Label string SubLabel string `toml:"sublabel"` + Batteries []string `toml:"batteries"` } type HostnameConfig struct { @@ -272,15 +274,26 @@ func (mocp MOCP) status() int { func battery(config BatteryConfig) string { var b strings.Builder const batteryPath = "/sys/class/power_supply" - files, err := ioutil.ReadDir(batteryPath) - if err != nil { - panic(err) + var batteries []string + if config.Batteries == nil { + files, err := ioutil.ReadDir(batteryPath) + if err != nil { + panic(err) + } + for _, file := range files { + path := filepath.Join(batteryPath, file.Name(), "capacity") + batteries = append(batteries, path) + } + } else { + for _, battery := range config.Batteries { + path := filepath.Join(batteryPath, battery, "capacity") + batteries = append(batteries, path) + } } batteryNb := 0 separator := "" - for _, file := range files { + for _, filePath := range batteries { b.WriteString(separator) - filePath := fmt.Sprintf("%v/%v/%v", batteryPath, file.Name(), "capacity") var capacity string content, err := ioutil.ReadFile(filePath) if err != nil { -- cgit v1.2.3