Skip to content

Commit 894d1af

Browse files
committed
process_matcher: Add helper fn for list options
Many arguments in pgrep/pkill/pidwait take list as an option and can be handled in an unified way.
1 parent f59140c commit 894d1af

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/uu/pgrep/src/process_matcher.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Common process matcher logic shared by pgrep, pkill and pidwait
77

88
use std::collections::HashSet;
9+
use std::hash::Hash;
910

1011
use clap::{arg, Arg, ArgAction, ArgMatches};
1112
use regex::Regex;
@@ -25,7 +26,7 @@ pub struct Settings {
2526
pub newest: bool,
2627
pub oldest: bool,
2728
pub older: Option<u64>,
28-
pub parent: Option<Vec<u64>>,
29+
pub parent: Option<HashSet<u64>>,
2930
pub runstates: Option<String>,
3031
pub terminal: Option<HashSet<Teletype>>,
3132
#[cfg(unix)]
@@ -137,6 +138,10 @@ fn try_get_pattern_from(matches: &ArgMatches) -> UResult<String> {
137138
Ok(pattern.to_string())
138139
}
139140

141+
fn any_matches<T: Eq + Hash>(optional_ids: &Option<HashSet<T>>, id: T) -> bool {
142+
optional_ids.as_ref().is_none_or(|ids| ids.contains(&id))
143+
}
144+
140145
/// Collect pids with filter construct from command line arguments
141146
fn collect_matched_pids(settings: &Settings) -> Vec<ProcessInformation> {
142147
// Filtration general parameters
@@ -171,18 +176,12 @@ fn collect_matched_pids(settings: &Settings) -> Vec<ProcessInformation> {
171176
settings.regex.is_match(want)
172177
};
173178

174-
let tty_matched = match &settings.terminal {
175-
Some(ttys) => ttys.contains(&pid.tty()),
176-
None => true,
177-
};
179+
let tty_matched = any_matches(&settings.terminal, pid.tty());
178180

179181
let arg_older = settings.older.unwrap_or(0);
180182
let older_matched = pid.start_time().unwrap() >= arg_older;
181183

182-
let parent_matched = match (&settings.parent, pid.ppid()) {
183-
(Some(parents), Ok(ppid)) => parents.contains(&ppid),
184-
_ => true,
185-
};
184+
let parent_matched = any_matches(&settings.parent, pid.ppid().unwrap());
186185

187186
if (run_state_matched
188187
&& pattern_matched

0 commit comments

Comments
 (0)