Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add EWW_BATTERY support in (free|open|net)bsd #645

Merged
merged 4 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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(),
Comment on lines +238 to +240
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't programmed in months, but I wrote magic vars.
Maybe better to use ? instead of unwrap. Magic variables are a extra, and if they break, they shouldn't take down all of eww.
And it sounds like this is going to break on a lot of bsd distributions (which is fine! Something is better than nothing here)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fine in this case, given that regex should guarantee that there will always be three captures, when the regex pattern has three capture groups, no? Or am I missing something...

))
}

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
Loading