From 3ab8e26e77918657ae0957c3fa938daca3d87728 Mon Sep 17 00:00:00 2001 From: RaresCon Date: Wed, 11 Jan 2023 14:59:44 +0200 Subject: [PATCH 1/2] Added dynamic battery widget For bottom to know that there are no batteries on the system, I added the battery::Manager to the options.rs file because here is the first moment bottom verifies battery configuration by reading the config file, which may or may not contain the battery field, but for a better UX, it doesn't matter what bottom finds in the config file now, if it doesn't retrieve battery data, it just ignores the battery widget all together. If needed, it can be adjusted so that if the config file contains the battery field, it will still show the widget. --- docs/content/usage/widgets/battery.md | 2 +- src/options.rs | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/docs/content/usage/widgets/battery.md b/docs/content/usage/widgets/battery.md index 1859a3ba6..b31acb83c 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 d659b00ad..cbddbda4f 100644 --- a/src/options.rs +++ b/src/options.rs @@ -10,6 +10,7 @@ use clap::ArgMatches; use layout_options::*; use regex::Regex; use serde::{Deserialize, Serialize}; +use starship_battery::Manager; use typed_builder::*; use crate::{ @@ -844,6 +845,14 @@ fn get_hide_table_gap(matches: &ArgMatches, config: &Config) -> bool { } fn get_use_battery(matches: &ArgMatches, config: &Config) -> bool { + 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; From 41e180780eac26e5165efeee82959c0a408a697f Mon Sep 17 00:00:00 2001 From: NitrogenDev <44950964+NitrogenDev@users.noreply.github.com> Date: Fri, 13 Jan 2023 17:55:06 +0200 Subject: [PATCH 2/2] CFG guarding for BATTERY module I guarded the options.rs in two places for battery module that can be missing in the feature list. --- src/options.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/options.rs b/src/options.rs index cbddbda4f..5c1603101 100644 --- a/src/options.rs +++ b/src/options.rs @@ -10,7 +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::{ @@ -845,6 +848,7 @@ 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 {