grep: Print filenames when counting lines and path size is less than 2

`grep --count --recursive foo` doesn't specify any file, but we should
show the paths anyway.
This commit is contained in:
Karol Kosek 2023-09-14 17:53:58 +02:00 committed by Andreas Kling
parent 018dcf570e
commit db4a654e9f

View file

@ -168,7 +168,6 @@ ErrorOr<int> serenity_main(Main::Arguments args)
patterns.append(files.take_first());
auto user_has_specified_files = !files.is_empty();
auto user_specified_multiple_files = files.size() >= 2;
PosixOptions options {};
if (case_insensitive)
@ -228,7 +227,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
auto exit_status = ExitStatus::NoLinesMatched;
auto handle_file = [&matches, binary_mode, count_lines, quiet_mode,
user_specified_multiple_files, &matched_line_count, &exit_status](StringView filename, bool print_filename) -> ErrorOr<void> {
&matched_line_count, &exit_status](StringView filename, bool print_filename) -> ErrorOr<void> {
auto file = TRY(Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read));
auto buffered_file = TRY(Core::InputBufferedFile::create(move(file)));
if (filename == '-')
@ -250,7 +249,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
}
if (count_lines && !quiet_mode) {
if (user_specified_multiple_files)
if (print_filename)
outln("{}:{}", filename, matched_line_count);
else
outln("{}", matched_line_count);