From a6e1ea3bd8fc9f6f4a8f4e0096619b383624a269 Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Wed, 31 Jul 2024 06:41:30 +0000 Subject: [PATCH] docs: update docs on filter (#1519) Updates some outdated docs on filtering, and adds some tests as well. In particular, this also adds a cfg_attr on tests to try and catch unknown fields; we'll be more lenient in prod builds though and allow them. --- .../config-file/data-filtering.md | 8 ++--- sample_configs/default_config.toml | 8 ++--- src/constants.rs | 8 ++--- src/options/config.rs | 1 + src/options/config/cpu.rs | 1 + src/options/config/disk.rs | 1 + src/options/config/flags.rs | 1 + src/options/config/ignore_list.rs | 1 + src/options/config/layout.rs | 3 ++ src/options/config/network.rs | 1 + src/options/config/process.rs | 1 + src/options/config/style.rs | 1 + src/options/config/temperature.rs | 1 + tests/integration/valid_config_tests.rs | 5 ++++ tests/valid_configs/filtering.toml | 30 +++++++++++++++++++ 15 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 tests/valid_configs/filtering.toml diff --git a/docs/content/configuration/config-file/data-filtering.md b/docs/content/configuration/config-file/data-filtering.md index bbc57468..478996dc 100644 --- a/docs/content/configuration/config-file/data-filtering.md +++ b/docs/content/configuration/config-file/data-filtering.md @@ -4,7 +4,7 @@ This section is in progress, and is just copied from the old documentation. -You can hide specific disks, temperature sensors, and networks by name in the config file via `disk_filter` and `mount_filter`, `temp_filter`, and `net_filter` respectively. Regex (`regex = true`), case-sensitivity (`case_sensitive = true`), and matching only if the entire word matches (`whole_word = true`) are supported, but are off by default. Filters default to denying entries that match and can be toggled by setting `is_list_ignored` to `false` in the config file. +You can hide specific disks, temperature sensors, and networks by name in the config file via `disk.name_filter` and `disk.mount_filter`, `temperature.sensor_filter`, and `network.interface_filter` respectively. Regex (`regex = true`), case-sensitivity (`case_sensitive = true`), and matching only if the entire word matches (`whole_word = true`) are supported, but are off by default. Filters default to denying entries that match and can be toggled by setting `is_list_ignored` to `false` in the config file. For example, here's the disk widget with no filter: @@ -13,7 +13,7 @@ For example, here's the disk widget with no filter: The following in the config file would filter out some entries by disk name: ```toml -[disk_filter] +[disk.name_filter] is_list_ignored = true list = ["/dev/sda"] regex = true @@ -26,14 +26,14 @@ whole_word = false If there are two potentially conflicting filters (i.e. when you are using both a disk and mount filter), the filter that explicitly allows an entry takes precedence over a filter that explicitly denies one. So for example, let's say we set a disk filter accepting anything with `/dev/sda`, but deny anything with `/mnt/.*` or `/`. So to do so, we write in the config file: ```toml -[disk_filter] +[disk.name_filter] is_list_ignored = false list = ["/dev/sda"] regex = true case_sensitive = false whole_word = false -[mount_filter] +[disk.mount_filter] is_list_ignored = true list = ["/mnt/.*", "/"] regex = true diff --git a/sample_configs/default_config.toml b/sample_configs/default_config.toml index 4db7acde..af98fccd 100644 --- a/sample_configs/default_config.toml +++ b/sample_configs/default_config.toml @@ -93,14 +93,14 @@ # Disk widget configuration #[disk] -#[disk.name_filter] +#[name_filter] #is_list_ignored = true #list = ["/dev/sda\\d+", "/dev/nvme0n1p2"] #regex = true #case_sensitive = false #whole_word = false -#[disk.mount_filter] +#[mount_filter] #is_list_ignored = true #list = ["/mnt/.*", "/boot"] #regex = true @@ -109,7 +109,7 @@ # Temperature widget configuration #[temperature] -#[temperature.sensor_filter] +#[sensor_filter] #is_list_ignored = true #list = ["cpu", "wifi"] #regex = false @@ -118,7 +118,7 @@ # Network widget configuration #[network] -#[network.interface_filter] +#[interface_filter] #is_list_ignored = true #list = ["virbr0.*"] #regex = true diff --git a/src/constants.rs b/src/constants.rs index d8d9a3ad..75358234 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -357,14 +357,14 @@ pub const CONFIG_TEXT: &str = r#"# This is a default config file for bottom. Al # Disk widget configuration #[disk] -#[disk.name_filter] +#[name_filter] #is_list_ignored = true #list = ["/dev/sda\\d+", "/dev/nvme0n1p2"] #regex = true #case_sensitive = false #whole_word = false -#[disk.mount_filter] +#[mount_filter] #is_list_ignored = true #list = ["/mnt/.*", "/boot"] #regex = true @@ -373,7 +373,7 @@ pub const CONFIG_TEXT: &str = r#"# This is a default config file for bottom. Al # Temperature widget configuration #[temperature] -#[temperature.sensor_filter] +#[sensor_filter] #is_list_ignored = true #list = ["cpu", "wifi"] #regex = false @@ -382,7 +382,7 @@ pub const CONFIG_TEXT: &str = r#"# This is a default config file for bottom. Al # Network widget configuration #[network] -#[network.interface_filter] +#[interface_filter] #is_list_ignored = true #list = ["virbr0.*"] #regex = true diff --git a/src/options/config.rs b/src/options/config.rs index d27af4b7..258943fa 100644 --- a/src/options/config.rs +++ b/src/options/config.rs @@ -24,6 +24,7 @@ use self::{cpu::CpuConfig, layout::Row, process::ProcessesConfig}; derive(schemars::JsonSchema), schemars(title = "Schema for bottom's configs (nightly)") )] +#[cfg_attr(test, serde(deny_unknown_fields))] pub struct Config { pub(crate) flags: Option, pub(crate) styles: Option, diff --git a/src/options/config/cpu.rs b/src/options/config/cpu.rs index ff471768..c9ba2529 100644 --- a/src/options/config/cpu.rs +++ b/src/options/config/cpu.rs @@ -15,6 +15,7 @@ pub enum CpuDefault { /// CPU column settings. #[derive(Clone, Debug, Default, Deserialize)] #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, serde(deny_unknown_fields))] pub struct CpuConfig { #[serde(default)] pub default: CpuDefault, diff --git a/src/options/config/disk.rs b/src/options/config/disk.rs index cc9f3319..13373a11 100644 --- a/src/options/config/disk.rs +++ b/src/options/config/disk.rs @@ -5,6 +5,7 @@ use super::IgnoreList; /// Disk configuration. #[derive(Clone, Debug, Default, Deserialize)] #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, serde(deny_unknown_fields))] pub struct DiskConfig { /// A filter over the disk names. pub name_filter: Option, diff --git a/src/options/config/flags.rs b/src/options/config/flags.rs index a2ad4dd7..00bdb334 100644 --- a/src/options/config/flags.rs +++ b/src/options/config/flags.rs @@ -4,6 +4,7 @@ use super::StringOrNum; #[derive(Clone, Debug, Default, Deserialize, Serialize)] #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, serde(deny_unknown_fields))] pub(crate) struct FlagConfig { pub(crate) hide_avg_cpu: Option, pub(crate) dot_marker: Option, diff --git a/src/options/config/ignore_list.rs b/src/options/config/ignore_list.rs index 839d8c49..5950cbb4 100644 --- a/src/options/config/ignore_list.rs +++ b/src/options/config/ignore_list.rs @@ -7,6 +7,7 @@ fn default_as_true() -> bool { #[derive(Clone, Debug, Default, Deserialize, Serialize)] #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, serde(deny_unknown_fields))] pub struct IgnoreList { #[serde(default = "default_as_true")] // TODO: Deprecate and/or rename, current name sounds awful. diff --git a/src/options/config/layout.rs b/src/options/config/layout.rs index 1348ad33..d68b2958 100644 --- a/src/options/config/layout.rs +++ b/src/options/config/layout.rs @@ -6,6 +6,7 @@ use crate::{app::layout_manager::*, options::OptionResult}; /// of children. #[derive(Clone, Deserialize, Debug, Serialize)] #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, serde(deny_unknown_fields))] #[serde(rename = "row")] pub struct Row { pub ratio: Option, @@ -217,6 +218,7 @@ impl Row { #[derive(Clone, Deserialize, Debug, Serialize)] #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] #[serde(untagged)] +#[cfg_attr(test, serde(deny_unknown_fields))] pub enum RowChildren { Widget(FinalWidget), Col { @@ -228,6 +230,7 @@ pub enum RowChildren { /// Represents a widget. #[derive(Clone, Deserialize, Debug, Serialize)] #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, serde(deny_unknown_fields))] pub struct FinalWidget { pub ratio: Option, #[serde(rename = "type")] diff --git a/src/options/config/network.rs b/src/options/config/network.rs index bd52b63d..2d8bc6f9 100644 --- a/src/options/config/network.rs +++ b/src/options/config/network.rs @@ -5,6 +5,7 @@ use super::IgnoreList; /// Network configuration. #[derive(Clone, Debug, Default, Deserialize)] #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, serde(deny_unknown_fields))] pub struct NetworkConfig { /// A filter over the network interface names. pub interface_filter: Option, diff --git a/src/options/config/process.rs b/src/options/config/process.rs index 978c1bac..8f34fd6d 100644 --- a/src/options/config/process.rs +++ b/src/options/config/process.rs @@ -5,6 +5,7 @@ use crate::widgets::ProcWidgetColumn; /// Process configuration. #[derive(Clone, Debug, Default, Deserialize)] #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, serde(deny_unknown_fields))] pub struct ProcessesConfig { /// A list of process widget columns. #[serde(default)] diff --git a/src/options/config/style.rs b/src/options/config/style.rs index f57d8f0c..921e1cce 100644 --- a/src/options/config/style.rs +++ b/src/options/config/style.rs @@ -35,6 +35,7 @@ pub(crate) struct ColorStr(Cow<'static, str>); #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(untagged)] #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, serde(deny_unknown_fields))] pub(crate) enum TextStyleConfig { Colour(ColorStr), TextStyle { diff --git a/src/options/config/temperature.rs b/src/options/config/temperature.rs index 9423242d..430d6d61 100644 --- a/src/options/config/temperature.rs +++ b/src/options/config/temperature.rs @@ -5,6 +5,7 @@ use super::IgnoreList; /// Temperature configuration. #[derive(Clone, Debug, Default, Deserialize)] #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, serde(deny_unknown_fields))] pub struct TempConfig { /// A filter over the sensor names. pub sensor_filter: Option, diff --git a/tests/integration/valid_config_tests.rs b/tests/integration/valid_config_tests.rs index eeae0109..e56f7f56 100644 --- a/tests/integration/valid_config_tests.rs +++ b/tests/integration/valid_config_tests.rs @@ -82,3 +82,8 @@ fn test_styling_sanity_check() { fn test_styling_sanity_check_2() { run_and_kill(&["-C", "./tests/valid_configs/styling_2.toml"]); } + +#[test] +fn test_filtering() { + run_and_kill(&["-C", "./tests/valid_configs/filtering.toml"]); +} diff --git a/tests/valid_configs/filtering.toml b/tests/valid_configs/filtering.toml new file mode 100644 index 00000000..52e84b86 --- /dev/null +++ b/tests/valid_configs/filtering.toml @@ -0,0 +1,30 @@ +[disk] +[name_filter] +is_list_ignored = true +list = ["/dev/sda\\d+", "/dev/nvme0n1p2"] +regex = true +case_sensitive = false +whole_word = false + +[mount_filter] +is_list_ignored = true +list = ["/mnt/.*", "/boot"] +regex = true +case_sensitive = false +whole_word = false + +[temperature] +[sensor_filter] +is_list_ignored = true +list = ["cpu", "wifi"] +regex = false +case_sensitive = false +whole_word = false + +[network] +[interface_filter] +is_list_ignored = true +list = ["virbr0.*"] +regex = true +case_sensitive = false +whole_word = false