Skip to content

Commit

Permalink
other: Dynamic battery widget (#975)
Browse files Browse the repository at this point in the history
* 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.

* CFG guarding for BATTERY module

I guarded the options.rs in two places for battery module that can be missing in the feature list.

Co-authored-by: RaresCon <RaresCon>
Co-authored-by: NitrogenDev <44950964+NitrogenDev@users.noreply.github.com>
  • Loading branch information
RaresCon and NitrogenDev authored Jan 15, 2023
1 parent 571ec08 commit f8db340
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/content/usage/widgets/battery.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
13 changes: 13 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f8db340

Please sign in to comment.