Skip to content

Commit e8eb577

Browse files
committed
ps: Implement -r flag
1 parent cc812d1 commit e8eb577

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/uu/ps/src/process_selection.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// file that was distributed with this source code.
55

66
use clap::ArgMatches;
7-
use uu_pgrep::process::{walk_process, ProcessInformation, Teletype};
7+
use uu_pgrep::process::{walk_process, ProcessInformation, RunState, Teletype};
88
use 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
}

src/uu/ps/src/ps.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,10 @@ pub fn uu_app() -> Command {
171171
.short('N')
172172
.help("negate selection")
173173
.action(ArgAction::SetTrue),
174-
// Arg::new("r")
175-
// .short('r')
176-
// .action(ArgAction::SetTrue)
177-
// .help("only running processes")
178-
// .allow_hyphen_values(true),
174+
Arg::new("r")
175+
.short('r')
176+
.action(ArgAction::SetTrue)
177+
.help("only running processes"),
179178
// Arg::new("T")
180179
// .short('T')
181180
// .action(ArgAction::SetTrue)

0 commit comments

Comments
 (0)