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 battery finder #303

Merged
merged 2 commits into from
Jul 22, 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
Cargo.lock
node_modules/
xephyr.log

# Ignore nix user files
**/*.nix
**/flake.lock
38 changes: 38 additions & 0 deletions crates/penrose_ui/src/bar/widgets/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,44 @@
pub mod helpers {
use penrose::util::{spawn_for_output, spawn_for_output_with_args};
use std::fs;
use std::path::PathBuf;

/// This finds the first battery (BAT) file it finds; so far only
/// confirmed working on Linux.
pub fn battery_file_search() -> Option<String> {
let battery_paths = vec![
// Linux
"/sys/class/power_supply",
// OpenBSD
"/var/run/apm",
// FreeBSD and DragonFlyBSD
"/dev",
// illumos
"/dev/battery",
];

battery_paths
.into_iter()
.filter_map(|base_path| {
let base_path = PathBuf::from(base_path);
if base_path.exists() && base_path.is_dir() {
fs::read_dir(base_path).ok()
} else {
None
}
})
.flat_map(|read_dir| read_dir.filter_map(Result::ok))
.filter_map(|entry| {
let file_name = entry.file_name();
let file_name_str = file_name.to_str()?;
if file_name_str.starts_with("BAT") && file_name_str[3..].parse::<u32>().is_ok() {
Some(file_name_str.to_string())
} else {
None
}
})
.next()
}

/// Fetch the requested battery's charge as a percentage of its total along with an indicator
/// of whether it is charging or discharging.
Expand Down
Loading