Skip to content

Commit

Permalink
docs: update docs on filter (#1519)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ClementTsang authored Jul 31, 2024
1 parent 2c0fb2a commit a6e1ea3
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 12 deletions.
8 changes: 4 additions & 4 deletions docs/content/configuration/config-file/data-filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions sample_configs/default_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -109,7 +109,7 @@

# Temperature widget configuration
#[temperature]
#[temperature.sensor_filter]
#[sensor_filter]
#is_list_ignored = true
#list = ["cpu", "wifi"]
#regex = false
Expand All @@ -118,7 +118,7 @@

# Network widget configuration
#[network]
#[network.interface_filter]
#[interface_filter]
#is_list_ignored = true
#list = ["virbr0.*"]
#regex = true
Expand Down
8 changes: 4 additions & 4 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/options/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FlagConfig>,
pub(crate) styles: Option<StyleConfig>,
Expand Down
1 change: 1 addition & 0 deletions src/options/config/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/options/config/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IgnoreList>,
Expand Down
1 change: 1 addition & 0 deletions src/options/config/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>,
pub(crate) dot_marker: Option<bool>,
Expand Down
1 change: 1 addition & 0 deletions src/options/config/ignore_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/options/config/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>,
Expand Down Expand Up @@ -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 {
Expand All @@ -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<u32>,
#[serde(rename = "type")]
Expand Down
1 change: 1 addition & 0 deletions src/options/config/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IgnoreList>,
Expand Down
1 change: 1 addition & 0 deletions src/options/config/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions src/options/config/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/options/config/temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IgnoreList>,
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/valid_config_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
}
30 changes: 30 additions & 0 deletions tests/valid_configs/filtering.toml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a6e1ea3

Please sign in to comment.