mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 11:04:59 +00:00
Utilities/grep: Implement line-counting mode
This commit is contained in:
parent
7ce693840b
commit
c81cef614a
1 changed files with 26 additions and 3 deletions
|
@ -51,6 +51,9 @@ int main(int argc, char** argv)
|
||||||
bool quiet_mode = false;
|
bool quiet_mode = false;
|
||||||
bool suppress_errors = false;
|
bool suppress_errors = false;
|
||||||
bool colored_output = isatty(STDOUT_FILENO);
|
bool colored_output = isatty(STDOUT_FILENO);
|
||||||
|
bool count_lines = false;
|
||||||
|
|
||||||
|
size_t matched_line_count = 0;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
args_parser.add_option(recursive, "Recursively scan files", "recursive", 'r');
|
args_parser.add_option(recursive, "Recursively scan files", "recursive", 'r');
|
||||||
|
@ -113,6 +116,7 @@ int main(int argc, char** argv)
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
args_parser.add_option(count_lines, "Output line count instead of line contents", "count", 'c');
|
||||||
args_parser.add_positional_argument(files, "File(s) to process", "file", Core::ArgsParser::Required::No);
|
args_parser.add_positional_argument(files, "File(s) to process", "file", Core::ArgsParser::Required::No);
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
|
@ -121,6 +125,7 @@ int main(int argc, char** argv)
|
||||||
pattern = files.take_first();
|
pattern = files.take_first();
|
||||||
|
|
||||||
auto user_has_specified_files = !files.is_empty();
|
auto user_has_specified_files = !files.is_empty();
|
||||||
|
auto user_specified_multiple_files = files.size() >= 2;
|
||||||
|
|
||||||
PosixOptions options {};
|
PosixOptions options {};
|
||||||
if (case_insensitive)
|
if (case_insensitive)
|
||||||
|
@ -141,6 +146,11 @@ int main(int argc, char** argv)
|
||||||
if (quiet_mode)
|
if (quiet_mode)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
if (count_lines) {
|
||||||
|
matched_line_count++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (is_binary && binary_mode == BinaryFileMode::Binary) {
|
if (is_binary && binary_mode == BinaryFileMode::Binary) {
|
||||||
outln(colored_output ? "binary file \x1B[34m{}\x1B[0m matches" : "binary file {} matches", filename);
|
outln(colored_output ? "binary file \x1B[34m{}\x1B[0m matches" : "binary file {} matches", filename);
|
||||||
} else {
|
} else {
|
||||||
|
@ -164,7 +174,8 @@ int main(int argc, char** argv)
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
auto handle_file = [&matches, binary_mode, suppress_errors](StringView filename, bool print_filename) -> bool {
|
auto handle_file = [&matches, binary_mode, suppress_errors, count_lines, quiet_mode,
|
||||||
|
user_specified_multiple_files, &matched_line_count](StringView filename, bool print_filename) -> bool {
|
||||||
auto file = Core::File::construct(filename);
|
auto file = Core::File::construct(filename);
|
||||||
if (!file->open(Core::OpenMode::ReadOnly)) {
|
if (!file->open(Core::OpenMode::ReadOnly)) {
|
||||||
if (!suppress_errors)
|
if (!suppress_errors)
|
||||||
|
@ -177,8 +188,17 @@ int main(int argc, char** argv)
|
||||||
auto is_binary = memchr(line.characters(), 0, line.length()) != nullptr;
|
auto is_binary = memchr(line.characters(), 0, line.length()) != nullptr;
|
||||||
|
|
||||||
if (matches(line, filename, line_number, print_filename, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary)
|
if (matches(line, filename, line_number, print_filename, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary)
|
||||||
return true;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (count_lines && !quiet_mode) {
|
||||||
|
if (user_specified_multiple_files)
|
||||||
|
outln("{}:{}", filename, matched_line_count);
|
||||||
|
else
|
||||||
|
outln("{}", matched_line_count);
|
||||||
|
matched_line_count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -217,8 +237,11 @@ int main(int argc, char** argv)
|
||||||
auto matched = matches(line_view, "stdin", line_number, false, is_binary);
|
auto matched = matches(line_view, "stdin", line_number, false, is_binary);
|
||||||
did_match_something = did_match_something || matched;
|
did_match_something = did_match_something || matched;
|
||||||
if (matched && is_binary && binary_mode == BinaryFileMode::Binary)
|
if (matched && is_binary && binary_mode == BinaryFileMode::Binary)
|
||||||
return 0;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (count_lines && !quiet_mode)
|
||||||
|
outln("{}", matched_line_count);
|
||||||
} else {
|
} else {
|
||||||
if (recursive) {
|
if (recursive) {
|
||||||
if (user_has_specified_files) {
|
if (user_has_specified_files) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue