#[cfg(target_os = "macos")]
use std::process::Command;
pub(crate) fn is_system_power_saving_active() -> bool {
if let Some(active) = env_override() {
return active;
}
platform_power_saving_active()
}
fn env_override() -> Option<bool> {
let value = std::env::var("MOADIM_POWER_SAVING_ACTIVE").ok()?;
match value.trim().to_ascii_lowercase().as_str() {
"1" | "true" | "yes" | "on" => Some(true),
"0" | "false" | "no" | "off" => Some(false),
_ => None,
}
}
#[cfg(target_os = "macos")]
fn platform_power_saving_active() -> bool {
macos_power_saving_active()
}
#[cfg(not(target_os = "macos"))]
const fn platform_power_saving_active() -> bool {
false
}
#[cfg(target_os = "macos")]
fn macos_power_saving_active() -> bool {
pmset_battery_power() || pmset_low_power_mode()
}
#[cfg(target_os = "macos")]
fn pmset_battery_power() -> bool {
command_stdout(&pmset_bin(), &["-g", "batt"])
.is_some_and(|stdout| stdout.contains("'Battery Power'"))
}
#[cfg(target_os = "macos")]
fn pmset_low_power_mode() -> bool {
command_stdout(&pmset_bin(), &["-g", "custom"]).is_some_and(|stdout| {
stdout
.lines()
.any(|line| line.split_whitespace().collect::<Vec<_>>() == ["lowpowermode", "1"])
})
}
#[cfg(target_os = "macos")]
fn pmset_bin() -> String {
std::env::var("MOADIM_PMSET_BIN").unwrap_or_else(|_| "pmset".to_string())
}
#[cfg(target_os = "macos")]
fn command_stdout(program: &str, args: &[&str]) -> Option<String> {
let output = Command::new(program).args(args).output().ok()?;
if !output.status.success() {
return None;
}
String::from_utf8(output.stdout).ok()
}
#[cfg(test)]
#[path = "system_power_tests.rs"]
mod system_power_tests;