mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:27:45 +00:00
ps: Perform filtering step before the output loop
Previously we did some of the filtering before the loop, and some inside it, which made things awkward to reason about. This also lets us avoid generating a TTY string for each process unless there's a column for it.
This commit is contained in:
parent
3aff3c610a
commit
cf998bc082
1 changed files with 8 additions and 15 deletions
|
@ -145,11 +145,17 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
|
|
||||||
auto& processes = all_processes.processes;
|
auto& processes = all_processes.processes;
|
||||||
|
|
||||||
|
// Filter
|
||||||
if (!pid_list.is_empty()) {
|
if (!pid_list.is_empty()) {
|
||||||
every_process_flag = true;
|
|
||||||
processes.remove_all_matching([&](auto& process) { return !pid_list.contains_slow(process.pid); });
|
processes.remove_all_matching([&](auto& process) { return !pid_list.contains_slow(process.pid); });
|
||||||
|
} 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(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sort
|
||||||
if (provided_quick_pid_list) {
|
if (provided_quick_pid_list) {
|
||||||
auto processes_sort_predicate = [&pid_list](auto& a, auto& b) {
|
auto processes_sort_predicate = [&pid_list](auto& a, auto& b) {
|
||||||
return pid_list.find_first_index(a.pid).value() < pid_list.find_first_index(b.pid).value();
|
return pid_list.find_first_index(a.pid).value() < pid_list.find_first_index(b.pid).value();
|
||||||
|
@ -169,22 +175,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
rows.unchecked_append(move(header));
|
rows.unchecked_append(move(header));
|
||||||
|
|
||||||
for (auto const& process : processes) {
|
for (auto const& process : processes) {
|
||||||
auto tty = TRY(String::from_deprecated_string(process.tty));
|
|
||||||
if (every_process_flag) {
|
|
||||||
// Don't skip any.
|
|
||||||
} else if (every_terminal_process_flag) {
|
|
||||||
if (tty.is_empty())
|
|
||||||
continue;
|
|
||||||
} else if (tty != this_pseudo_tty_name) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector<String> row;
|
Vector<String> row;
|
||||||
TRY(row.try_resize(columns.size()));
|
TRY(row.try_resize(columns.size()));
|
||||||
|
|
||||||
if (tty == "")
|
|
||||||
tty = "n/a"_short_string;
|
|
||||||
|
|
||||||
if (uid_column.has_value())
|
if (uid_column.has_value())
|
||||||
row[*uid_column] = TRY(String::from_deprecated_string(process.username));
|
row[*uid_column] = TRY(String::from_deprecated_string(process.username));
|
||||||
if (pid_column.has_value())
|
if (pid_column.has_value())
|
||||||
|
@ -196,7 +189,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
if (sid_column.has_value())
|
if (sid_column.has_value())
|
||||||
row[*sid_column] = TRY(String::number(process.sid));
|
row[*sid_column] = TRY(String::number(process.sid));
|
||||||
if (tty_column.has_value())
|
if (tty_column.has_value())
|
||||||
row[*tty_column] = tty;
|
row[*tty_column] = process.tty == "" ? "n/a"_short_string : TRY(String::from_deprecated_string(process.tty));
|
||||||
if (state_column.has_value())
|
if (state_column.has_value())
|
||||||
row[*state_column] = process.threads.is_empty()
|
row[*state_column] = process.threads.is_empty()
|
||||||
? "Zombie"_short_string
|
? "Zombie"_short_string
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue