Skip to content

Commit

Permalink
Keep working with incompatible batteries
Browse files Browse the repository at this point in the history
  • Loading branch information
pepa65 committed Sep 27, 2023
1 parent 0edcb16 commit ea00829
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![GoDoc](https://godoc.org/github.com/pepa65/bat?status.svg)](https://godoc.org/github.com/pepa65/bat)
![Continuous Integration](https://github.com/pepa65/bat/actions/workflows/ci.yaml/badge.svg)

# bat v0.12.2
# bat v0.13.0
**Manage battery charge limit**

* Repo: github.com/pepa65/bat
Expand All @@ -11,7 +11,7 @@
* Required: Linux-5.4-rc1+ systemd-244+

```
bat v0.12.2 - Manage battery charge limit
bat v0.13.0 - Manage battery charge limit
Repo: github.com/pepa65/bat
Usage: bat <option>
Options (every option except 's[tatus]' needs root privileges):
Expand Down
20 changes: 10 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

const (
version = "0.12.2"
version = "0.13.0"
years = "2023"
prefix = "chargelimit-"
syspath = "/sys/class/power_supply/"
Expand Down Expand Up @@ -100,23 +100,23 @@ func main() {
batglob = "BAT?"
batselect = ""
}
batteries, err := filepath.Glob(filepath.Join(syspath + batglob, threshold))
batteries, err := filepath.Glob(syspath + batglob)
if err != nil || len(batteries) == 0 {
bat = batglob
errexit("No compatible battery device found")
errexit("No battery device found")
}

// Ignoring other compatible batteries!
batpath, _ = filepath.Split(batteries[0])
bat = batpath[len(batpath)-5:len(batpath)-1]
// Ignoring any other batteries!
batpath = batteries[0]
bat = batpath[len(batpath)-4:]
if len(batteries) > 1 {
fmt.Printf("More than 1 battery device found:")
corr := len(threshold) - 1
for _, battery := range batteries {
fmt.Printf(" %s", battery[len(battery)-3-corr:len(battery)-corr])
fmt.Printf(" %s", battery[len(battery)-4:])
}
fmt.Println("")
}
thresholdpath := filepath.Join(batpath, threshold)
switch command {
case "s", "status", "-s", "--status":
fmt.Printf("[%s]\n", bat)
Expand Down Expand Up @@ -210,7 +210,7 @@ func main() {
}

defer f.Close()
_, err = f.WriteString(fmt.Sprintf(unitfile, event, event, shell, current, batteries[0], event))
_, err = f.WriteString(fmt.Sprintf(unitfile, event, event, shell, current, thresholdpath, event))
if err != nil {
errexit("could not instantiate systemd unit file '" + service + "'")
}
Expand Down Expand Up @@ -267,7 +267,7 @@ func main() {
ilimit = 100
}
l := []byte(fmt.Sprintf("%d", ilimit))
err = os.WriteFile(batteries[0], l, 0o644)
err = os.WriteFile(thresholdpath, l, 0o644)
if err != nil {
if errors.Is(err, syscall.EACCES) {
errexit("insufficient permissions, run with root privileges")
Expand Down

0 comments on commit ea00829

Please sign in to comment.