print filename if multiple files parsed (#59)

This commit is contained in:
Christophe Nanteuil 2022-12-01 14:47:03 +01:00 committed by GitHub
parent b39636c907
commit 86263ffcba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -137,14 +137,12 @@ def find_in_po(
def display_results(
matches: Sequence[Match],
pattern: str,
line_number: bool,
files_with_matches: bool,
args: argparse.Namespace,
grep_colors: GrepColors,
):
"""Display matches as a colorfull table."""
files = {match.file for match in matches}
if files_with_matches: # Just print filenames
if args.files_with_matches: # Just print filenames
for file in files:
if grep_colors:
print(grep_colors.start("fn") + file + grep_colors.NO_COLOR)
@ -156,9 +154,9 @@ def display_results(
term_width = get_terminal_size()[0]
for match in matches:
left = match.msgid
if line_number:
if args.line_number:
pnum = str(match.line) + ":"
if len(files) > 1:
if args.print_filenames:
pfile = match.file + ":"
else:
pfile = ""
@ -173,7 +171,10 @@ def display_results(
if grep_colors:
print(
colorize(
tabulate(table, tablefmt="fancy_grid"), pattern, grep_colors, prefixes
tabulate(table, tablefmt="fancy_grid"),
args.pattern,
grep_colors,
prefixes,
)
)
else:
@ -353,6 +354,7 @@ def main():
else:
grep_colors = None
files = process_path(args.path, args.recursive)
args.print_filenames = len(files) > 1
if args.exclude_dir:
files = [
f
@ -378,9 +380,7 @@ def main():
return
display_results(
results,
args.pattern,
args.line_number,
args.files_with_matches,
args,
grep_colors,
)