ps: Allow multiple filtering options to be used simultaneously

Previously, it was assumed that only one filtering option, such as
`-u` or `-p` would be used at a time. With this PR, processes are now
shown if they match any of the specified filters.
This commit is contained in:
Tim Ledbetter 2023-07-09 16:26:08 +01:00 committed by Sam Atkins
parent a758e27153
commit aeb87d6e78
2 changed files with 16 additions and 11 deletions

View file

@ -18,9 +18,9 @@ For each process, print its PID (process ID), to which TTY it belongs, and invok
* `-a`: Consider all processes that are associated with a TTY.
* `-A` or `-e`: Consider all processes, not just those in the current TTY.
* `-f`: Also print for each process: UID (as resolved username), PPID (parent PID), and STATE (Runnable, Sleeping, Selecting, Reading, etc.)
* `-p pid-list`: Only consider the given PIDs, if they exist. `pid-list` is a list of PIDs, separated by commas or spaces.
* `-p pid-list`: Select processes matching any of the given PIDs. `pid-list` is a list of PIDs, separated by commas or spaces.
* `-q pid-list`: Only consider the given PIDs, if they exist. Output the processes in the order provided by `pid-list`. `pid-list` is a list of PIDs, separated by commas or spaces.
* `-u user-list`: Only consider processes for the given users, if they exist. `user-list` is a list of UIDs or login names, separated by commas or spaces.
* `-u user-list`: Select processes matching any of the given UIDs. `user-list` is a list of UIDs or login names, separated by commas or spaces.
## Examples

View file

@ -163,15 +163,20 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto& processes = all_processes.processes;
// Filter
if (!pid_list.is_empty()) {
processes.remove_all_matching([&](auto& process) { return !pid_list.contains_slow(process.pid); });
} else if (!uid_list.is_empty()) {
processes.remove_all_matching([&](auto& process) { return !uid_list.contains_slow(process.uid); });
} else if (every_terminal_process_flag) {
processes.remove_all_matching([&](auto& process) { return process.tty.is_empty(); });
} else if (!every_process_flag) {
// Default is to show processes from the current TTY
processes.remove_all_matching([&](Core::ProcessStatistics& process) { return process.tty.view() != this_pseudo_tty_name.bytes_as_string_view(); });
if (!every_process_flag) {
Vector<Core::ProcessStatistics> filtered_processes;
for (auto const& process : processes) {
// Default is to show processes from the current TTY
if ((!provided_filtering_option && process.tty == this_pseudo_tty_name.bytes_as_string_view())
|| (!pid_list.is_empty() && pid_list.contains_slow(process.pid))
|| (!uid_list.is_empty() && uid_list.contains_slow(process.uid))
|| (every_terminal_process_flag && !process.tty.is_empty())) {
filtered_processes.append(process);
}
}
processes = move(filtered_processes);
}
// Sort