summaryrefslogtreecommitdiffstats
path: root/src/options/config/process.rs
blob: 878abc68dd27ff7bb4ea9c0754443180cf730160 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use indoc::indoc;
use serde::Deserialize;

use crate::{args::ProcessArgs, widgets::ProcWidgetColumn};

use super::DefaultConfig;

/// Process column settings.
#[derive(Clone, Debug, Default, Deserialize)]
pub(crate) struct ProcessConfig {
    #[serde(flatten)]
    pub(crate) args: ProcessArgs,
    pub(crate) columns: Option<Vec<ProcWidgetColumn>>,
}

impl DefaultConfig for ProcessConfig {
    fn default_config() -> String {
        let s = indoc! {r##"
            # Enables case sensitivity by default when searching for a process.
            # case_sensitive = false

            # Calculates process CPU usage as a percentage of current usage rather than total usage.
            # current_usage = false

            # Hides advanced process stopping options on Unix-like systems. Signal 15 (TERM) will be sent when stopping a process.
            # disable_advanced_kill = false

            # Groups processes with the same name by default.
            # group_processes = false

            # Defaults to showing process memory usage by value. Otherwise, it defaults to showing it by percentage.
            # mem_as_value = false

            # Shows the full command name instead of just the process name by default.
            # process_command = false

            # Enables regex by default while searching.
            # regex = false

            # Makes the process widget use tree mode by default.
            # tree = false

            # Show process CPU% usage without averaging over the number of CPU cores.
            # unnormalized_cpu = false

            # Enables whole-word matching by default while searching.
            # whole_word = false
        "##};

        s.to_string()
    }
}

#[cfg(test)]
mod test {
    use super::ProcessConfig;
    use crate::widgets::ProcWidgetColumn;

    #[test]
    fn empty_column_setting() {
        let config = "";
        let generated: ProcessConfig = toml_edit::de::from_str(config).unwrap();
        assert!(generated.columns.is_none());
    }

    #[test]
    fn process_column_settings() {
        let config = r#"
            columns = ["CPU%", "PiD", "user", "MEM", "Tread", "T.Write", "Rps", "W/s", "tiMe", "USER", "state"]
        "#;

        let generated: ProcessConfig = toml_edit::de::from_str(config).unwrap();
        assert_eq!(
            generated.columns.unwrap(),
            vec![
                ProcWidgetColumn::Cpu,
                ProcWidgetColumn::PidOrCount,
                ProcWidgetColumn::User,
                ProcWidgetColumn::Mem,
                ProcWidgetColumn::TotalRead,
                ProcWidgetColumn::TotalWrite,
                ProcWidgetColumn::ReadPerSecond,
                ProcWidgetColumn::WritePerSecond,
                ProcWidgetColumn::Time,
                ProcWidgetColumn::User,
                ProcWidgetColumn::State,
            ],
        );
    }

    #[test]
    fn process_column_settings_2() {
        let config = r#"columns = ["MEM", "TWrite", "fake", "read", "wps"]"#;
        toml_edit::de::from_str::<ProcessConfig>(config).expect_err("Should error out!");
    }

    #[test]
    fn process_column_settings_3() {
        let config = r#"columns = ["Twrite", "T.Write"]"#;
        let generated: ProcessConfig = toml_edit::de::from_str(config).unwrap();
        let columns = generated.columns.unwrap();
        assert_eq!(columns, vec![ProcWidgetColumn::TotalWrite; 2]);

        let config = r#"columns = ["Tread", "T.read"]"#;
        let generated: ProcessConfig = toml_edit::de::from_str(config).unwrap();
        let columns = generated.columns.unwrap();
        assert_eq!(columns, vec![ProcWidgetColumn::TotalRead; 2]);

        let config = r#"columns = ["read", "rps", "r/s"]"#;
        let generated: ProcessConfig = toml_edit::de::from_str(config).unwrap();
        let columns = generated.columns.unwrap();
        assert_eq!(columns, vec![ProcWidgetColumn::ReadPerSecond; 3]);

        let config = r#"columns = ["write", "wps", "w/s"]"#;
        let generated: ProcessConfig = toml_edit::de::from_str(config).unwrap();
        let columns = generated.columns.unwrap();
        assert_eq!(columns, vec![ProcWidgetColumn::WritePerSecond; 3]);
    }
}