Skip to content

Commit

Permalink
Proper handling of battery health
Browse files Browse the repository at this point in the history
  • Loading branch information
pepa65 committed Jan 18, 2025
1 parent 42aa76a commit 5c3667c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "batlimit"
version = "0.9.1"
version = "0.9.2"
edition = "2021"
authors = ["github.com/pepa65 <pepa65@passchier.net>"]
description = "Set battery charge limit on supported laptops on Linux with CLI"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![License](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
[![Downloads](https://img.shields.io/crates/d/batlimit.svg)](https://crates.io/crates/batlimit)

# batlimit 0.9.1
# batlimit 0.9.2
**Set battery charge limit on supported laptops on Linux with CLI**

It is now widely acknowledged that the life span of Li-ion batteries is extended by not charging them to the max.
Expand Down Expand Up @@ -50,7 +50,7 @@ An often recommended battery charge limit is 80.
## Installation
### Download static single-binary
```
wget https://github.com/pepa65/batlimit/releases/download/0.9.1/batlimit
wget https://github.com/pepa65/batlimit/releases/download/0.9.2/batlimit
sudo mv batlimit /usr/local/bin/
sudo chown root:root /usr/local/bin/batlimit
sudo chmod +x /usr/local/bin/batlimit
Expand Down Expand Up @@ -94,7 +94,7 @@ Install the musl binary: `cargo-binstall batlimit`

## Usage
```
batlimit 0.9.1 - Set battery charge limit on supported laptops on Linux with CLI
batlimit 0.9.2 - Set battery charge limit on supported laptops on Linux with CLI
Usage: batlimit [COMMAND]
Commands:
info Print battery info (default command)
Expand Down
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,18 @@ impl Battery {
println!("{persiststr:<pad_size$} INCONSISTENT");
}
let healthstr = "health";
if info.len() > 6 {
// position of Current Max. Capacity and Current Design Capacity in info depends on which keys are found
let health = 100 * info[5].1.parse::<u32>().unwrap_or(0) / info[6].1.parse::<u32>().unwrap_or(1);
let mut cur = String::new();
let mut des = String::new();
for triple in &info {
if triple.0 == INFO[5].1 {
cur = triple.1.clone();
};
if triple.0 == INFO[9].1 {
des = triple.1.clone();
};
}
if !cur.is_empty() && !des.is_empty() {
let health = 100 * cur.parse::<u32>().unwrap_or(0) / des.parse::<u32>().unwrap_or(1);
println!("{healthstr:<pad_size$} {health}%");
} else {
println!("{healthstr:<pad_size$} NO INFO");
Expand Down

0 comments on commit 5c3667c

Please sign in to comment.