1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:07:34 +00:00

pgrep: Add -c option to show the number of matches

This commit is contained in:
Tim Ledbetter 2023-05-30 17:51:18 +01:00 committed by Andreas Kling
parent f95e6ee849
commit ad851706ab
2 changed files with 18 additions and 12 deletions

View file

@ -5,11 +5,12 @@ pgrep - look up processes based on name
## Synopsis ## Synopsis
```sh ```sh
$ pgrep [-d delimiter] [--ignore-case] [--invert-match] <process-name> $ pgrep [--count] [-d delimiter] [--ignore-case] [--invert-match] <process-name>
``` ```
## Options ## Options
* `-c`, `--count`: Suppress normal output and print the number of matching processes
* `-d`, `--delimiter`: Set the string used to delimit multiple pids * `-d`, `--delimiter`: Set the string used to delimit multiple pids
* `-i`, `--ignore-case`: Make matches case-insensitive * `-i`, `--ignore-case`: Make matches case-insensitive
* `-v`, `--invert-match`: Select non-matching lines * `-v`, `--invert-match`: Select non-matching lines

View file

@ -20,12 +20,14 @@ ErrorOr<int> serenity_main(Main::Arguments args)
TRY(Core::System::unveil("/etc/passwd", "r")); TRY(Core::System::unveil("/etc/passwd", "r"));
TRY(Core::System::unveil(nullptr, nullptr)); TRY(Core::System::unveil(nullptr, nullptr));
bool display_number_of_matches = false;
auto pid_delimiter = "\n"sv; auto pid_delimiter = "\n"sv;
bool case_insensitive = false; bool case_insensitive = false;
bool invert_match = false; bool invert_match = false;
StringView pattern; StringView pattern;
Core::ArgsParser args_parser; Core::ArgsParser args_parser;
args_parser.add_option(display_number_of_matches, "Suppress normal output and print the number of matching processes", "count", 'c');
args_parser.add_option(pid_delimiter, "Set the string used to delimit multiple pids", "delimiter", 'd', nullptr); args_parser.add_option(pid_delimiter, "Set the string used to delimit multiple pids", "delimiter", 'd', nullptr);
args_parser.add_option(case_insensitive, "Make matches case-insensitive", "ignore-case", 'i'); args_parser.add_option(case_insensitive, "Make matches case-insensitive", "ignore-case", 'i');
args_parser.add_option(invert_match, "Select non-matching lines", "invert-match", 'v'); args_parser.add_option(invert_match, "Select non-matching lines", "invert-match", 'v');
@ -51,20 +53,23 @@ ErrorOr<int> serenity_main(Main::Arguments args)
} }
} }
quick_sort(matches); if (display_number_of_matches) {
outln("{}", matches.size());
} else {
quick_sort(matches);
auto displayed_at_least_one = false;
for (auto& match : matches) {
if (displayed_at_least_one)
out("{}{}"sv, pid_delimiter, match);
else
out("{}"sv, match);
displayed_at_least_one = true;
}
auto displayed_at_least_one = false;
for (auto& match : matches) {
if (displayed_at_least_one) if (displayed_at_least_one)
out("{}{}"sv, pid_delimiter, match); outln();
else
out("{}"sv, match);
displayed_at_least_one = true;
} }
if (displayed_at_least_one)
outln();
return matches.size() > 0 ? 0 : 1; return matches.size() > 0 ? 0 : 1;
} }