Skip to content

Commit

Permalink
limit command optional
Browse files Browse the repository at this point in the history
  • Loading branch information
pepa65 committed Sep 26, 2023
1 parent 1044b78 commit 9687335
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 25 deletions.
40 changes: 30 additions & 10 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.10.0
# bat v0.11.0
**Manage battery charge limit**

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

```
bat v0.10.0 - Manage battery charge limit
bat v0.11.0 - Manage battery charge limit
Repo: github.com/pepa65/bat
Usage: bat <option>
Options (every option except 's[tatus]' needs root privileges):
[s[tatus]] Display charge level, limit, health & persist status.
l[imit] <int> Set the charge limit to <int> percent.
p[ersist] Install and enable the persist systemd unit files.
r[emove] Remove the persist systemd unit files.
h[elp] Just display this help text.
v[ersion] Just display version information.
[s[tatus]] Display charge level, limit, health & persist status.
[l[imit]] <int> Set the charge limit to <int> percent.
p[ersist] Install and enable the persist systemd unit files.
r[emove] Remove the persist systemd unit files.
h[elp] Just display this help text.
v[ersion] Just display version information.
```

## About
Expand Down Expand Up @@ -67,14 +67,34 @@ Persist: yes
```

### Set a battery charge limit in percentage points (requires privileges):
`sudo bat limit 80`
`sudo bat 80`

Sample output:
```
Charge limit set, to make it persist, run:
bat persist
```

### Undo the battery charge limit (requires privileges):
`sudo bat limit 100`
`sudo bat 0`

Sample output:
```
Charge limit unset
```

### Persist the currently set charge limit after restart/hibernation/wake-up (requires privileges):
`sudo bat persist`

Sample output:
```
Persistence enabled for charge limit: 80
```

### Remove the persist config settings (requires privileges):
`sudo bat remove`

Output:
```
Persistence of charge limit removed
```
12 changes: 6 additions & 6 deletions help.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ bat v%s - Manage battery charge limit
Repo: github.com/pepa65/bat
Usage: bat <option>
Options (every option except 's[tatus]' needs root privileges):
[s[tatus]] Display charge level, limit, health & persist status.
l[imit] <int> Set the charge limit to <int> percent.
p[ersist] Install and enable the persist systemd unit files.
r[emove] Remove the persist systemd unit files.
h[elp] Just display this help text.
v[ersion] Just display version information.
[s[tatus]] Display charge level, limit, health & persist status.
[l[imit]] <int> Set the charge limit to <int> percent.
p[ersist] Install and enable the persist systemd unit files.
r[emove] Remove the persist systemd unit files.
h[elp] Just display this help text.
v[ersion] Just display version information.
32 changes: 23 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

const (
version = "0.10.0"
version = "0.11.0"
years = "2023"
prefix = "chargelimit-"
pathglob = "/sys/class/power_supply/BAT?"
Expand Down Expand Up @@ -77,6 +77,11 @@ func main() {
fmt.Printf(versionmsg, version, years)
os.Exit(0)
}
var limit string
if len(command) > 0 && command[0] >= '0' && command[0] <= '9' {
limit = command
command = "limit"
}

batteries, err := filepath.Glob(filepath.Join(pathglob, threshold))
if err != nil || len(batteries) == 0 {
Expand Down Expand Up @@ -173,7 +178,7 @@ func main() {
f, err := os.Create(file)
if err != nil {
if errors.Is(err, syscall.EACCES) {
errexit("insufficient permissions, try running with root privileges")
errexit("insufficient permissions, run with root privileges")
}

errexit("could not create systemd unit file '" + file + "'")
Expand Down Expand Up @@ -209,7 +214,7 @@ func main() {
case strings.Contains(message, "does not exist"):
continue
case strings.Contains(message, "Access denied"):
errexit("insufficient permissions, try running with root privileges")
errexit("insufficient permissions, run with root privileges")
default:
errexit("failure to disable unit file '" + service + "'")
}
Expand All @@ -221,27 +226,36 @@ func main() {
}
fmt.Println("Persistence of charge limit removed")
case "l", "limit", "-l", "--limit":
limit := os.Args[2]
if limit == "" {
errexit("Argument to 'limit' missing")
limit = os.Args[2]
if limit == "" {
errexit("Argument to 'limit' missing")
}
}

ilimit, err := strconv.Atoi(limit)
if err != nil || ilimit < 1 || ilimit > 100 {
errexit("argument to limit must be an integer between 1 and 100")
if err != nil || ilimit < 0 || ilimit > 100 {
errexit("argument to limit must be an integer between 0 and 100")
}

if ilimit == 0 {
ilimit = 100
}
l := []byte(fmt.Sprintf("%d", ilimit))
err = os.WriteFile(batteries[0], l, 0o644)
if err != nil {
if errors.Is(err, syscall.EACCES) {
errexit("insufficient permissions, try running with root privileges")
errexit("insufficient permissions, run with root privileges")
}

errexit("could not set battery charge limit")
}

fmt.Println("Charging threshold set (run 'bat persist' to make it persist)")
if ilimit == 100 {
fmt.Println("Charge limit unset")
} else {
fmt.Println("Charge limit set, to make it persist, run:\nbat persist")
}
default:
usage()
errexit("argument '" + command + "' invalid")
Expand Down

0 comments on commit 9687335

Please sign in to comment.