Skip to content

Commit

Permalink
add EWW_BATTERY support in (free|open|net)bsd
Browse files Browse the repository at this point in the history
  • Loading branch information
dangerdyke committed Dec 13, 2022
1 parent 678e4db commit f9c8385
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 @@ -8,6 +8,7 @@ All notable changes to eww will be listed here, starting at changes since versio
### Features
- Add support for safe access (`?.`) in simplexpr (By: oldwomanjosiah)
- Allow floating-point numbers in percentages for window-geometry
- Add support for the `EWW_BATTERY` magic variable in FreeBSD, OpenBSD, and NetBSD (By: dangerdyke)

## [0.4.0] (04.09.2022)

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};
use anyhow::{Context, Result};
use itertools::Itertools;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -153,8 +153,54 @@ pub fn get_battery_capacity() -> Result<String> {
Ok(json)
}

#[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 f9c8385

Please sign in to comment.