summaryrefslogtreecommitdiffstats
path: root/src/options.rs
diff options
context:
space:
mode:
authorClement Tsang <34804052+ClementTsang@users.noreply.github.com>2020-09-02 22:02:49 -0400
committerGitHub <noreply@github.com>2020-09-02 22:02:49 -0400
commitcef3166cf87d0a769fb886049b9bf1e5faf75e32 (patch)
treef54fab112ed2ed18d39af1baaa15ede155de0c5a /src/options.rs
parenta94907372839c7fd79bbd0e37d9cab8ccfa40790 (diff)
feature: Add ability to filter out disks and temp (#220)
You can now filter out disks and temp sensors by name via config.
Diffstat (limited to 'src/options.rs')
-rw-r--r--src/options.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/options.rs b/src/options.rs
index e1a6e226..a8536d8a 100644
--- a/src/options.rs
+++ b/src/options.rs
@@ -1,3 +1,4 @@
+use regex::Regex;
use serde::Deserialize;
use std::collections::{HashMap, HashSet};
use std::time::Instant;
@@ -19,6 +20,8 @@ pub struct Config {
pub flags: Option<ConfigFlags>,
pub colors: Option<ConfigColours>,
pub row: Option<Vec<Row>>,
+ pub disk_filter: Option<IgnoreList>,
+ pub temp_filter: Option<IgnoreList>,
}
#[derive(Default, Deserialize)]
@@ -69,6 +72,14 @@ pub struct ConfigColours {
pub battery_colors: Option<Vec<String>>,
}
+#[derive(Default, Deserialize)]
+pub struct IgnoreList {
+ pub is_list_ignored: bool,
+ pub list: Vec<String>,
+ pub regex: Option<bool>,
+ pub case_sensitive: Option<bool>,
+}
+
pub fn build_app(
matches: &clap::ArgMatches<'static>, config: &Config, widget_layout: &BottomLayout,
default_widget_id: u64, default_widget_type_option: &Option<BottomWidgetType>,
@@ -249,6 +260,11 @@ pub fn build_app(
use_battery: used_widget_set.get(&Battery).is_some(),
};
+ let disk_filter =
+ get_ignore_list(&config.disk_filter).context("Update 'disk_filter' in your config file")?;
+ let temp_filter =
+ get_ignore_list(&config.temp_filter).context("Update 'temp_filter' in your config file")?;
+
Ok(App::builder()
.app_config_fields(app_config_fields)
.cpu_state(CpuState::init(cpu_state_map))
@@ -262,6 +278,10 @@ pub fn build_app(
.current_widget(widget_map.get(&initial_widget_id).unwrap().clone()) // I think the unwrap is fine here
.widget_map(widget_map)
.used_widgets(used_widgets)
+ .filters(DataFilters {
+ disk_filter,
+ temp_filter,
+ })
.build())
}
@@ -665,3 +685,45 @@ pub fn get_use_battery(matches: &clap::ArgMatches<'static>, config: &Config) ->
}
false
}
+
+pub fn get_ignore_list(ignore_list: &Option<IgnoreList>) -> error::Result<Option<Filter>> {
+ if let Some(ignore_list) = ignore_list {
+ let list: Result<Vec<_>, _> = ignore_list
+ .list
+ .iter()
+ .map(|name| {
+ let use_regex = if let Some(use_regex) = ignore_list.regex {
+ use_regex
+ } else {
+ false
+ };
+ let use_cs = if let Some(use_cs) = ignore_list.case_sensitive {
+ use_cs
+ } else {
+ false
+ };
+
+ let escaped_string: String;
+ let res = format!(
+ "{}{}",
+ if use_cs { "" } else { "(?i)" },
+ if use_regex {
+ name
+ } else {
+ escaped_string = regex::escape(name);
+ &escaped_string
+ }
+ );
+
+ Regex::new(&res)
+ })
+ .collect();
+
+ Ok(Some(Filter {
+ list: list?,
+ is_list_ignored: ignore_list.is_list_ignored,
+ }))
+ } else {
+ Ok(None)
+ }
+}