mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:37:45 +00:00
pgrep: Add -l
option to list the process name as well as its pid
This commit is contained in:
parent
ad851706ab
commit
cb6a2d60d3
2 changed files with 14 additions and 8 deletions
|
@ -5,7 +5,7 @@ pgrep - look up processes based on name
|
||||||
## Synopsis
|
## Synopsis
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ pgrep [--count] [-d delimiter] [--ignore-case] [--invert-match] <process-name>
|
$ pgrep [--count] [-d delimiter] [--ignore-case] [--list-name] [--invert-match] <process-name>
|
||||||
```
|
```
|
||||||
|
|
||||||
## Options
|
## Options
|
||||||
|
@ -13,6 +13,7 @@ $ pgrep [--count] [-d delimiter] [--ignore-case] [--invert-match] <process-name>
|
||||||
* `-c`, `--count`: Suppress normal output and print the number of matching processes
|
* `-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
|
||||||
|
* `-l`, `--list-name`: List the process name in addition to its pid
|
||||||
* `-v`, `--invert-match`: Select non-matching lines
|
* `-v`, `--invert-match`: Select non-matching lines
|
||||||
|
|
||||||
## Arguments
|
## Arguments
|
||||||
|
|
|
@ -23,6 +23,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
||||||
bool display_number_of_matches = false;
|
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 list_process_name = false;
|
||||||
bool invert_match = false;
|
bool invert_match = false;
|
||||||
StringView pattern;
|
StringView pattern;
|
||||||
|
|
||||||
|
@ -30,6 +31,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
||||||
args_parser.add_option(display_number_of_matches, "Suppress normal output and print the number of matching processes", "count", 'c');
|
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(list_process_name, "List the process name in addition to its pid", "list-name", 'l');
|
||||||
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');
|
||||||
args_parser.add_positional_argument(pattern, "Process name to search for", "process-name");
|
args_parser.add_positional_argument(pattern, "Process name to search for", "process-name");
|
||||||
args_parser.parse(args);
|
args_parser.parse(args);
|
||||||
|
@ -45,24 +47,27 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
||||||
|
|
||||||
auto all_processes = TRY(Core::ProcessStatisticsReader::get_all());
|
auto all_processes = TRY(Core::ProcessStatisticsReader::get_all());
|
||||||
|
|
||||||
Vector<pid_t> matches;
|
Vector<Core::ProcessStatistics> matches;
|
||||||
for (auto& it : all_processes.processes) {
|
for (auto const& it : all_processes.processes) {
|
||||||
auto result = re.match(it.name, PosixFlags::Global);
|
auto result = re.match(it.name, PosixFlags::Global);
|
||||||
if (result.success ^ invert_match) {
|
if (result.success ^ invert_match) {
|
||||||
matches.append(it.pid);
|
matches.append(it);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (display_number_of_matches) {
|
if (display_number_of_matches) {
|
||||||
outln("{}", matches.size());
|
outln("{}", matches.size());
|
||||||
} else {
|
} else {
|
||||||
quick_sort(matches);
|
quick_sort(matches, [](auto const& a, auto const& b) { return a.pid < b.pid; });
|
||||||
auto displayed_at_least_one = false;
|
auto displayed_at_least_one = false;
|
||||||
for (auto& match : matches) {
|
for (auto& match : matches) {
|
||||||
if (displayed_at_least_one)
|
if (displayed_at_least_one)
|
||||||
out("{}{}"sv, pid_delimiter, match);
|
out("{}"sv, pid_delimiter);
|
||||||
else
|
|
||||||
out("{}"sv, match);
|
out("{}"sv, match.pid);
|
||||||
|
|
||||||
|
if (list_process_name)
|
||||||
|
out(" {}"sv, match.name);
|
||||||
|
|
||||||
displayed_at_least_one = true;
|
displayed_at_least_one = true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue