Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

other: Make the battery dependency/features optional #570

Merged
merged 5 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,15 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: check
args: --all-targets --verbose --target=${{ matrix.triple.target }} --no-default-features
args: --all-targets --verbose --target=${{ matrix.triple.target }} --features "battery"
use-cross: ${{ matrix.triple.cross }}

- name: Check without battery feature on the main 3
if: matrix.triple.toTest == 'true'
uses: actions-rs/cargo@v1
with:
command: check
args: --all-targets --verbose --target=${{ matrix.triple.target }}
use-cross: ${{ matrix.triple.cross }}

- name: Run tests
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: build
args: --release --verbose --target=${{ matrix.triple.target }} --no-default-features
args: --release --verbose --target=${{ matrix.triple.target }} --features "battery"
use-cross: ${{ matrix.triple.cross }}

- name: Move autocomplete to working directory
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: build
args: --release --verbose --target=${{ matrix.triple.target }} --no-default-features
args: --release --verbose --target=${{ matrix.triple.target }} --features "battery"
use-cross: ${{ matrix.triple.cross }}

- name: Move autocomplete to working directory
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ opt-level = 3
codegen-units = 1

[features]
default = ["fern", "log"]
default = ["fern", "log", "battery"]

[dependencies]
anyhow = "1.0.40"
backtrace = "0.3.59"
battery = "0.7.8"
chrono = "0.4.19"
crossterm = "0.18.2"
ctrlc = { version = "3.1.9", features = ["termination"] }
Expand All @@ -60,9 +59,10 @@ typed-builder = "0.9.0"
unicode-segmentation = "1.7.1"
unicode-width = "0.1"

# For debugging only... disable on release builds with --no-default-target for no? TODO: Redo this.
# For debugging only... disable on release builds with for now? TODO: Redo this.
fern = { version = "0.6.0", optional = true }
log = { version = "0.4.14", optional = true }
battery = { version = "0.7.8", optional = true }

[target.'cfg(unix)'.dependencies]
libc = "0.2.86"
Expand Down
4 changes: 4 additions & 0 deletions docs/content/usage/widgets/battery.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Battery Widget

!!! Warning

The battery features are unavailable if the binary is compiled with the `battery` feature disabled!

The battery widget provides information about batteries on the system.

<figure>
Expand Down
22 changes: 17 additions & 5 deletions src/app/data_farmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ use once_cell::sync::Lazy;

use std::{time::Instant, vec::Vec};

#[cfg(feature = "battery")]
use crate::data_harvester::batteries;

use crate::{
data_harvester::{batteries, cpu, disks, memory, network, processes, temperature, Data},
data_harvester::{cpu, disks, memory, network, processes, temperature, Data},
utils::gen_util::{get_decimal_bytes, GIGA_LIMIT},
};
use regex::Regex;
Expand Down Expand Up @@ -60,6 +63,7 @@ pub struct DataCollection {
pub io_labels_and_prev: Vec<((u64, u64), (u64, u64))>,
pub io_labels: Vec<(String, String)>,
pub temp_harvest: Vec<temperature::TempHarvest>,
#[cfg(feature = "battery")]
pub battery_harvest: Vec<batteries::BatteryHarvest>,
}

Expand All @@ -80,6 +84,7 @@ impl Default for DataCollection {
io_labels_and_prev: Vec::default(),
io_labels: Vec::default(),
temp_harvest: Vec::default(),
#[cfg(feature = "battery")]
battery_harvest: Vec::default(),
}
}
Expand All @@ -97,7 +102,10 @@ impl DataCollection {
self.io_harvest = disks::IoHarvest::default();
self.io_labels_and_prev = Vec::default();
self.temp_harvest = Vec::default();
self.battery_harvest = Vec::default();
#[cfg(feature = "battery")]
{
self.battery_harvest = Vec::default();
}
}

pub fn set_frozen_time(&mut self) {
Expand Down Expand Up @@ -166,9 +174,12 @@ impl DataCollection {
self.eat_proc(list_of_processes);
}

// Battery
if let Some(list_of_batteries) = harvested_data.list_of_batteries {
self.eat_battery(list_of_batteries);
#[cfg(feature = "battery")]
{
// Battery
if let Some(list_of_batteries) = harvested_data.list_of_batteries {
self.eat_battery(list_of_batteries);
}
}

// And we're done eating. Update time and push the new entry!
Expand Down Expand Up @@ -312,6 +323,7 @@ impl DataCollection {
self.process_harvest = list_of_processes;
}

#[cfg(feature = "battery")]
fn eat_battery(&mut self, list_of_batteries: Vec<batteries::BatteryHarvest>) {
self.battery_harvest = list_of_batteries;
}
Expand Down
36 changes: 25 additions & 11 deletions src/app/data_harvester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use fxhash::FxHashMap;
#[cfg(not(target_os = "linux"))]
use sysinfo::{System, SystemExt};

#[cfg(feature = "battery")]
use battery::{Battery, Manager};

use crate::app::layout_manager::UsedWidgets;
Expand All @@ -16,6 +17,7 @@ use futures::join;

use super::DataFilters;

#[cfg(feature = "battery")]
pub mod batteries;
pub mod cpu;
pub mod disks;
Expand All @@ -36,6 +38,7 @@ pub struct Data {
pub list_of_processes: Option<Vec<processes::ProcessHarvest>>,
pub disks: Option<Vec<disks::DiskHarvest>>,
pub io: Option<disks::IoHarvest>,
#[cfg(feature = "battery")]
pub list_of_batteries: Option<Vec<batteries::BatteryHarvest>>,
}

Expand All @@ -52,6 +55,7 @@ impl Default for Data {
disks: None,
io: None,
network: None,
#[cfg(feature = "battery")]
list_of_batteries: None,
}
}
Expand Down Expand Up @@ -95,7 +99,9 @@ pub struct DataCollector {
total_tx: u64,
show_average_cpu: bool,
widgets_to_harvest: UsedWidgets,
#[cfg(feature = "battery")]
battery_manager: Option<Manager>,
#[cfg(feature = "battery")]
battery_list: Option<Vec<Battery>>,
filters: DataFilters,
}
Expand All @@ -122,7 +128,9 @@ impl DataCollector {
total_tx: 0,
show_average_cpu: false,
widgets_to_harvest: UsedWidgets::default(),
#[cfg(feature = "battery")]
battery_manager: None,
#[cfg(feature = "battery")]
battery_list: None,
filters,
}
Expand Down Expand Up @@ -150,13 +158,16 @@ impl DataCollector {
}
}

if self.widgets_to_harvest.use_battery {
if let Ok(battery_manager) = Manager::new() {
if let Ok(batteries) = battery_manager.batteries() {
let battery_list: Vec<Battery> = batteries.filter_map(Result::ok).collect();
if !battery_list.is_empty() {
self.battery_list = Some(battery_list);
self.battery_manager = Some(battery_manager);
#[cfg(feature = "battery")]
{
if self.widgets_to_harvest.use_battery {
if let Ok(battery_manager) = Manager::new() {
if let Ok(batteries) = battery_manager.batteries() {
let battery_list: Vec<Battery> = batteries.filter_map(Result::ok).collect();
if !battery_list.is_empty() {
self.battery_list = Some(battery_list);
self.battery_manager = Some(battery_manager);
}
}
}
}
Expand Down Expand Up @@ -235,10 +246,13 @@ impl DataCollector {
}

// Batteries
if let Some(battery_manager) = &self.battery_manager {
if let Some(battery_list) = &mut self.battery_list {
self.data.list_of_batteries =
Some(batteries::refresh_batteries(battery_manager, battery_list));
#[cfg(feature = "battery")]
{
if let Some(battery_manager) = &self.battery_manager {
if let Some(battery_list) = &mut self.battery_list {
self.data.list_of_batteries =
Some(batteries::refresh_batteries(battery_manager, battery_list));
}
}
}

Expand Down
37 changes: 31 additions & 6 deletions src/app/layout_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,10 +945,12 @@ impl std::str::FromStr for BottomWidgetType {
"temp" | "temperature" => Ok(BottomWidgetType::Temp),
"disk" => Ok(BottomWidgetType::Disk),
"empty" => Ok(BottomWidgetType::Empty),
"battery" | "batt" => Ok(BottomWidgetType::Battery),
_ => Err(BottomError::ConfigError(format!(
"\"{}\" is an invalid widget name.

"battery" | "batt" if cfg!(feature = "battery") => Ok(BottomWidgetType::Battery),
_ => {
if cfg!(feature = "battery") {
Err(BottomError::ConfigError(format!(
"\"{}\" is an invalid widget name.

Supported widget names:
+--------------------------+
| cpu |
Expand All @@ -966,8 +968,31 @@ Supported widget names:
| batt, battery |
+--------------------------+
",
s
))),
s
)))
} else {
Err(BottomError::ConfigError(format!(
"\"{}\" is an invalid widget name.

Supported widget names:
+--------------------------+
| cpu |
+--------------------------+
| mem, memory |
+--------------------------+
| net, network |
+--------------------------+
| proc, process, processes |
+--------------------------+
| temp, temperature |
+--------------------------+
| disk |
+--------------------------+
",
s
)))
}
}
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,12 @@ fn main() -> Result<()> {
}

// Battery
if app.used_widgets.use_battery {
app.canvas_data.battery_data =
convert_battery_harvest(&app.data_collection);
#[cfg(feature = "battery")]
{
if app.used_widgets.use_battery {
app.canvas_data.battery_data =
convert_battery_harvest(&app.data_collection);
}
}
}
}
Expand Down
Loading