44// file that was distributed with this source code.
55
66use clap:: ArgMatches ;
7- use uu_pgrep:: process:: { walk_process, ProcessInformation , Teletype } ;
7+ use uu_pgrep:: process:: { walk_process, ProcessInformation , RunState , Teletype } ;
88use uucore:: error:: UResult ;
99
1010#[ cfg( target_family = "unix" ) ]
@@ -46,6 +46,9 @@ pub struct ProcessSelectionSettings {
4646 /// - '-x' Lift "must have a tty" restriction.
4747 pub dont_require_tty : bool ,
4848
49+ /// - `-r` Restrict the selection to only running processes.
50+ pub only_running : bool ,
51+
4952 /// - `--deselect` Negates the selection.
5053 pub negate_selection : bool ,
5154}
@@ -57,6 +60,7 @@ impl ProcessSelectionSettings {
5760 select_non_session_leaders_with_tty : matches. get_flag ( "a" ) ,
5861 select_non_session_leaders : matches. get_flag ( "d" ) ,
5962 dont_require_tty : matches. get_flag ( "x" ) ,
63+ only_running : matches. get_flag ( "r" ) ,
6064 negate_selection : matches. get_flag ( "deselect" ) ,
6165 }
6266 }
@@ -67,6 +71,10 @@ impl ProcessSelectionSettings {
6771 let current_euid = current_process. euid ( ) . unwrap ( ) ;
6872
6973 let matches_criteria = |process : & mut ProcessInformation | -> UResult < bool > {
74+ if self . only_running && !process. run_state ( ) . is_ok_and ( |x| x == RunState :: Running ) {
75+ return Ok ( false ) ;
76+ }
77+
7078 if self . select_all {
7179 return Ok ( true ) ;
7280 }
0 commit comments