From 0849fe24368fdfeb9d18c03b676b8fd5f915e6c4 Mon Sep 17 00:00:00 2001 From: Tuomas Tynkkynen Date: Fri, 31 Oct 2025 23:08:58 +0200 Subject: [PATCH] ps: Implement --deselect flag --- src/uu/ps/src/process_selection.rs | 6 +++++- tests/by-util/test_ps.rs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/uu/ps/src/process_selection.rs b/src/uu/ps/src/process_selection.rs index 5fb80ff..c3f37b8 100644 --- a/src/uu/ps/src/process_selection.rs +++ b/src/uu/ps/src/process_selection.rs @@ -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); } } diff --git a/tests/by-util/test_ps.rs b/tests/by-util/test_ps.rs index bc3e41d..e1d5626 100644 --- a/tests/by-util/test_ps.rs +++ b/tests/by-util/test_ps.rs @@ -179,3 +179,20 @@ fn test_no_headers_flags() { .stdout_does_not_match(®ex); } } + +#[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()); +}