Debian Codegraph @

src/options/config/process.rs

raw symbols SCIP
1 use serde::Deserialize;
2
3 use crate::{canvas::components::data_table::SortOrder, widgets::ProcColumn};
4
5 /// Process configuration fields.
6 #[derive(Clone, Debug, Default, Deserialize)]
7 #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
8 #[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
9 pub(crate) struct ProcessesConfig {
10 /// A list of process widget columns.
11 #[serde(default)]
12 pub columns: Vec<ProcColumn>, // TODO: make this more composable(?) in the future, we might need to rethink how it's done for custom widgets
13
14 /// The default sort column.
15 #[serde(default)]
16 pub default_sort: Option<ProcColumn>,
17
18 /// The default sort order. If not set, defaults to column-specific defaults
19 /// (e.g. CPU usage is descending, process names are ascending).
20 #[serde(default)]
21 pub(crate) sort_order: Option<SortOrder>,
22
23 /// Whether to get process child threads.
24 pub get_threads: Option<bool>,
25
26 /// Hide kernel threads from being shown. Linux only.
27 #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
28 pub hide_k_threads: Option<bool>,
29
30 /// Collapse the process tree by default when tree mode is set.
31 pub tree_collapse: Option<bool>,
32
33 /// Shows the full command name instead of the process name by default.
34 pub process_command: Option<bool>,
35
36 // This does nothing on Windows, but we leave it enabled to make the config file consistent
37 // across platforms.
38 //
39 // #[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))]
40 /// Disable the advanced kill dialog and just show the basic one with no options.
41 #[cfg_attr(
42 not(any(target_os = "linux", target_os = "macos", target_os = "freebsd")),
43 allow(dead_code)
44 )]
45 pub disable_advanced_kill: Option<bool>,
46
47 /// Defaults to showing process memory usage by value.
48 pub default_memory_value: Option<bool>,
49
50 /// Groups processes with the same name by default. No effect if `--tree` is set.
51 pub default_grouped: Option<bool>,
52
53 /// Enables regex by default while searching.
54 pub regex: Option<bool>,
55
56 /// Enables case sensitivity by default when searching.
57 pub case_sensitive: Option<bool>,
58
59 /// Enables whole-word matching by default while searching.
60 pub whole_word: Option<bool>,
61
62 /// Makes the process widget use tree mode by default.
63 pub default_tree: Option<bool>,
64
65 /// Calculates process CPU usage as a percentage of current usage rather than total usage.
66 pub current_usage: Option<bool>,
67
68 /// Show process CPU% usage without averaging over the number of CPU cores.
69 pub unnormalized_cpu: Option<bool>,
70 }
71
72 #[cfg(test)]
73 mod test {
74 use super::{ProcColumn, ProcessesConfig};
75 use crate::widgets::ProcWidgetColumn;
76
77 #[test]
78 fn empty_column_setting() {
79 let config = "";
80 let generated: ProcessesConfig = toml_edit::de::from_str(config).unwrap();
81 assert!(generated.columns.is_empty());
82 }
83
84 fn to_columns(columns: Vec<ProcColumn>) -> Vec<ProcWidgetColumn> {
85 columns
86 .iter()
87 .map(ProcWidgetColumn::from)
88 .collect::<Vec<_>>()
89 }
90
91 #[test]
92 fn valid_process_column_config() {
93 #[cfg(unix)]
94 let config = r#"
95 columns = ["CPU%", "PiD", "user", "MEM", "virt", "Tread", "T.Write", "Rps", "W/s", "tiMe", "USER", "state", "prioRity", "Nice"]
96 "#;
97
98 #[cfg(target_os = "windows")]
99 let config = r#"
100 columns = ["CPU%", "PiD", "user", "MEM", "virt", "Tread", "T.Write", "Rps", "W/s", "tiMe", "USER", "state", "prioRity"]
101 "#;
102
103 let generated: ProcessesConfig = toml_edit::de::from_str(config).unwrap();
104 assert_eq!(
105 to_columns(generated.columns),
106 vec![
107 ProcWidgetColumn::Cpu,
108 ProcWidgetColumn::PidOrCount,
109 ProcWidgetColumn::User,
110 ProcWidgetColumn::Mem,
111 ProcWidgetColumn::VirtualMem,
112 ProcWidgetColumn::TotalRead,
113 ProcWidgetColumn::TotalWrite,
114 ProcWidgetColumn::ReadPerSecond,
115 ProcWidgetColumn::WritePerSecond,
116 ProcWidgetColumn::Time,
117 ProcWidgetColumn::User,
118 ProcWidgetColumn::State,
119 ProcWidgetColumn::Priority,
120 #[cfg(unix)]
121 ProcWidgetColumn::Nice,
122 ],
123 );
124 }
125
126 #[test]
127 fn bad_process_column_config() {
128 let config = r#"columns = ["MEM", "TWrite", "Cpuz", "read", "wps"]"#;
129 toml_edit::de::from_str::<ProcessesConfig>(config).expect_err("Should error out!");
130 }
131
132 #[test]
133 fn valid_default_sort_config() {
134 let config = r#"default_sort = "mem""#;
135 let generated: ProcessesConfig = toml_edit::de::from_str(config).unwrap();
136 assert_eq!(generated.default_sort, Some(ProcColumn::MemPercent));
137
138 let config = r#"default_sort = "PID""#;
139 let generated: ProcessesConfig = toml_edit::de::from_str(config).unwrap();
140 assert_eq!(generated.default_sort, Some(ProcColumn::Pid));
141
142 let config = "";
143 let generated: ProcessesConfig = toml_edit::de::from_str(config).unwrap();
144 assert_eq!(generated.default_sort, None);
145 }
146
147 #[test]
148 fn invalid_default_sort_config() {
149 let config = r#"default_sort = "soup""#;
150 toml_edit::de::from_str::<ProcessesConfig>(config).expect_err("Should error out!");
151 }
152
153 #[test]
154 fn valid_process_column_config_2() {
155 let config = r#"columns = ["Twrite", "T.Write"]"#;
156 let generated: ProcessesConfig = toml_edit::de::from_str(config).unwrap();
157 assert_eq!(
158 to_columns(generated.columns),
159 vec![ProcWidgetColumn::TotalWrite; 2]
160 );
161
162 let config = r#"columns = ["Tread", "T.read"]"#;
163 let generated: ProcessesConfig = toml_edit::de::from_str(config).unwrap();
164 assert_eq!(
165 to_columns(generated.columns),
166 vec![ProcWidgetColumn::TotalRead; 2]
167 );
168
169 let config = r#"columns = ["read", "rps", "r/s"]"#;
170 let generated: ProcessesConfig = toml_edit::de::from_str(config).unwrap();
171 assert_eq!(
172 to_columns(generated.columns),
173 vec![ProcWidgetColumn::ReadPerSecond; 3]
174 );
175
176 let config = r#"columns = ["write", "wps", "w/s"]"#;
177 let generated: ProcessesConfig = toml_edit::de::from_str(config).unwrap();
178 assert_eq!(
179 to_columns(generated.columns),
180 vec![ProcWidgetColumn::WritePerSecond; 3]
181 );
182 }
183
184 /// Test that process enum variants that are advertised in the schema are valid.
185 #[cfg(feature = "generate_schema")]
186 #[test]
187 fn ensure_process_column_schema_is_accepted() {
188 use strum::VariantArray;
189
190 use crate::options::{Config, ProcColumn};
191
192 for column in ProcColumn::VARIANTS {
193 for &name in column.get_schema_names() {
194 let config = format!("[processes]\ncolumns = [\"{name}\"]\n");
195 toml_edit::de::from_str::<Config>(&config).unwrap_or_else(|e| {
196 panic!("schema name {name:?} was rejected:\n{e}\nconfig was:\n{config}")
197 });
198 }
199 }
200 }
201 }