diff --git a/docs/content/usage/widgets/battery.md b/docs/content/usage/widgets/battery.md index 1859a3ba..b31acb83 100644 --- a/docs/content/usage/widgets/battery.md +++ b/docs/content/usage/widgets/battery.md @@ -2,7 +2,7 @@ !!! Warning - The battery features are unavailable if the binary is compiled with the `battery` feature disabled! + The battery features are unavailable if the binary is compiled with the `battery` feature disabled or if there are no batteries on the system! The battery widget provides information about batteries on the system. diff --git a/src/options.rs b/src/options.rs index d659b00a..5c160310 100644 --- a/src/options.rs +++ b/src/options.rs @@ -10,6 +10,10 @@ use clap::ArgMatches; use layout_options::*; use regex::Regex; use serde::{Deserialize, Serialize}; + +#[cfg(feature = "battery")] +use starship_battery::Manager; + use typed_builder::*; use crate::{ @@ -844,6 +848,15 @@ fn get_hide_table_gap(matches: &ArgMatches, config: &Config) -> bool { } fn get_use_battery(matches: &ArgMatches, config: &Config) -> bool { + #[cfg(feature = "battery")] + if let Ok(battery_manager) = Manager::new() { + if let Ok(batteries) = battery_manager.batteries() { + if batteries.count() == 0 { + return false; + } + } + } + if cfg!(feature = "battery") { if matches.is_present("battery") { return true;