feat: detect battery mode

This commit is contained in:
Myriad-Dreamin 2025-11-16 07:36:15 +08:00
parent 55d53ecd71
commit 7b8c44ef4c
9 changed files with 412 additions and 7 deletions

View file

@ -33,6 +33,7 @@ time = { workspace = true, features = ["formatting"] }
lsp-types.workspace = true
tempfile = { workspace = true, optional = true }
same-file = { workspace = true, optional = true }
battery = { workspace = true, optional = true }
# feature = "web"
js-sys = { workspace = true, optional = true }
@ -78,7 +79,7 @@ rkyv-validation = ["rkyv/validation"]
__web = ["wasm-bindgen", "js-sys", "web-time"]
web = ["__web"]
system = ["tempfile", "same-file"]
system = ["tempfile", "same-file", "battery"]
bi-hash = []
[lints]

View file

@ -0,0 +1,30 @@
//! Battery-related utilities.
#[cfg(feature = "system")]
mod system {
/// Detects if the system is currently in power saving mode.
pub fn is_power_saving() -> bool {
::battery::Manager::new()
.ok()
.and_then(|manager| manager.batteries().ok())
.map(|mut batteries| {
batteries.any(|battery| match battery {
Ok(bat) => matches!(bat.state(), ::battery::State::Discharging),
Err(_) => false,
})
})
.unwrap_or(false)
}
}
#[cfg(feature = "system")]
pub use system::*;
#[cfg(not(feature = "system"))]
mod other {
/// Detects if the system is currently in power saving mode.
pub fn is_power_saving() -> bool {
false
}
}
#[cfg(not(feature = "system"))]
pub use other::*;

View file

@ -1,6 +1,7 @@
//! Additional functions wrapping Rust's standard library.
pub mod adt;
pub mod battery;
pub mod error;
pub mod fs;
pub mod hash;

View file

@ -378,12 +378,13 @@ impl Config {
}
};
self.syntax_only = match try_(|| update.get("syntaxOnly")?.as_str()) {
Some("enable" | "onPowerSaving") => true,
Some("onPowerSaving") => tinymist_std::battery::is_power_saving(),
Some("enable") => true,
Some("disable" | "auto") | None => false,
Some(value) => {
self.warnings.push(tinymist_l10n::t!(
"tinymist.config.badCompileStatus",
"compileStatus must be either `\"enable\"`, `\"disable\", `\"onPowerSaving\"`, or `\"auto\"`, got {value}",
"tinymist.config.badSyntaxOnly",
"syntaxOnly must be either `\"enable\"`, `\"disable\", `\"onPowerSaving\"`, or `\"auto\"`, got {value}",
value = value.debug_l10n(),
));