ps: Implement --deselect flag

This commit is contained in:
Tuomas Tynkkynen
2025-11-03 12:30:32 +02:00
parent c63a93da53
commit 0849fe2436
2 changed files with 22 additions and 1 deletions
+5 -1
View File
@@ -42,6 +42,9 @@ pub struct ProcessSelectionSettings {
pub select_non_session_leaders_with_tty: bool,
/// - `-d` Select all processes except session leaders.
pub select_non_session_leaders: bool,
/// - `--deselect` Negates the selection.
pub negate_selection: bool,
}
impl ProcessSelectionSettings {
@@ -50,6 +53,7 @@ impl ProcessSelectionSettings {
select_all: matches.get_flag("A"),
select_non_session_leaders_with_tty: matches.get_flag("a"),
select_non_session_leaders: matches.get_flag("d"),
negate_selection: matches.get_flag("deselect"),
}
}
@@ -77,7 +81,7 @@ impl ProcessSelectionSettings {
let mut selected = vec![];
for mut process in walk_process() {
if matches_criteria(&mut process)? {
if matches_criteria(&mut process)? ^ self.negate_selection {
selected.push(process);
}
}
+17
View File
@@ -179,3 +179,20 @@ fn test_no_headers_flags() {
.stdout_does_not_match(&regex);
}
}
#[test]
#[cfg(target_os = "linux")]
fn test_deselect() {
// Inverse of all processes should be empty
new_ucmd!()
.args(&["--deselect", "-A", "--no-headers"])
.fails()
.code_is(1)
.stdout_is("");
// PID 1 should be present in inverse of default filter criteria
new_ucmd!()
.args(&["--deselect"])
.succeeds()
.stdout_matches(&Regex::new("\n *1 ").unwrap());
}