Skip to content

Commit

Permalink
add EWW_BATTERY support in (free|open|net)bsd (#645)
Browse files Browse the repository at this point in the history
* add `EWW_BATTERY` support in (free|open|net)bsd

* add `EWW_BATTERY` support in (free|open|net)bsd

---------

Co-authored-by: ElKowar <[email protected]>
  • Loading branch information
dangerdyke and elkowar authored Aug 25, 2024
1 parent 96529d3 commit ee937b9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ All notable changes to eww will be listed here, starting at changes since versio
- Made `and`, `or` and `?:` lazily evaluated in simplexpr (By: ModProg)
- Add Vanilla CSS support (By: Ezequiel Ramis)
- Add `jq` function, offering jq-style json processing
- Add support for the `EWW_BATTERY` magic variable in FreeBSD, OpenBSD, and NetBSD (By: dangerdyke)
- Add `justify` property to the label widget, allowing text justification (By: n3oney)
- Add `EWW_TIME` magic variable (By: Erenoit)
- Add trigonometric functions (`sin`, `cos`, `tan`, `cot`) and degree/radian conversions (`degtorad`, `radtodeg`) (By: end-4)
Expand Down
48 changes: 47 additions & 1 deletion crates/eww/src/config/system_stats.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::util::IterAverage;
use crate::{regex, util::IterAverage};

Check warning on line 1 in crates/eww/src/config/system_stats.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `regex`

Check warning on line 1 in crates/eww/src/config/system_stats.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `regex`

Check warning on line 1 in crates/eww/src/config/system_stats.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `regex`

Check warning on line 1 in crates/eww/src/config/system_stats.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `regex`

Check warning on line 1 in crates/eww/src/config/system_stats.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `regex`
use anyhow::{Context, Result};
use once_cell::sync::Lazy;
use std::{fs::read_to_string, sync::Mutex};
Expand Down Expand Up @@ -202,8 +202,54 @@ pub fn get_battery_capacity() -> Result<String> {
Ok(serde_json::to_string(&(Data { batteries, total_avg: (current / total) * 100_f64 })).unwrap())
}

#[cfg(any(target_os = "netbsd", target_os = "freebsd", target_os = "openbsd"))]
pub fn get_battery_capacity() -> Result<String> {
let batteries = String::from_utf8(
// I have only tested `apm` on FreeBSD, but it *should* work on all of the listed targets,
// based on what I can tell from their online man pages.
std::process::Command::new("apm")
.output()
.context("\nError while getting the battery values on bsd, with `apm`: ")?
.stdout,
)?;

// `apm` output should look something like this:
// $ apm
// ...
// Remaining battery life: 87%
// Remaining battery time: unknown
// Number of batteries: 1
// Battery 0
// Battery Status: charging
// Remaining battery life: 87%
// Remaining battery time: unknown
// ...
// last 4 lines are repeated for each battery.
// see also:
// https://www.freebsd.org/cgi/man.cgi?query=apm&manpath=FreeBSD+13.1-RELEASE+and+Ports
// https://man.openbsd.org/amd64/apm.8
// https://man.netbsd.org/apm.8
let mut json = String::from('{');
let re_total = regex!(r"(?m)^Remaining battery life: (\d+)%");
let re_single = regex!(r"(?sm)^Battery (\d+):.*?Status: (\w+).*?(\d+)%");
for bat in re_single.captures_iter(&batteries) {
json.push_str(&format!(
r#""BAT{}": {{ "status": "{}", "capacity": {} }}, "#,
bat.get(1).unwrap().as_str(),
bat.get(2).unwrap().as_str(),
bat.get(3).unwrap().as_str(),
))
}

json.push_str(&format!(r#""total_avg": {}}}"#, re_total.captures(&batteries).unwrap().get(1).unwrap().as_str()));
Ok(json)
}

#[cfg(not(target_os = "macos"))]
#[cfg(not(target_os = "linux"))]
#[cfg(not(target_os = "netbsd"))]
#[cfg(not(target_os = "freebsd"))]
#[cfg(not(target_os = "openbsd"))]
pub fn get_battery_capacity() -> Result<String> {
Err(anyhow::anyhow!("Eww doesn't support your OS for getting the battery capacity"))
}
Expand Down

0 comments on commit ee937b9

Please sign in to comment.