log-search: Do no use color codes if output is not a TTY.

This commit is contained in:
Alex Vandiver 2023-02-13 17:10:47 +00:00 committed by Tim Abbott
parent 33d06af6c2
commit cfd9e56d1a
1 changed files with 10 additions and 2 deletions

View File

@ -179,6 +179,7 @@ def main() -> None:
(filter_types, filter_funcs) = parse_filters(args)
logfile_names = parse_logfile_names(args)
use_color = sys.stdout.isatty()
try:
for logfile_name in reversed(logfile_names):
with maybe_gzip(logfile_name) as logfile:
@ -203,6 +204,7 @@ def main() -> None:
match,
args,
filter_types=filter_types,
use_color=use_color,
)
except KeyboardInterrupt:
sys.exit(signal.SIGINT + 128)
@ -374,7 +376,13 @@ def print_line(
elif code >= 500 and code <= 599:
indicator = "!"
color = FAIL
if use_color:
url = f"{BOLD}{match['path']}"
else:
url = match["path"]
color = ""
if FilterType.HOSTNAME not in filter_types:
hostname = match["hostname"]
if hostname is None:
@ -402,7 +410,7 @@ def print_line(
url,
]
print(color + " ".join([p for p in parts if p is not None]) + ENDC)
print(color + " ".join([p for p in parts if p is not None]) + ENDC if use_color else "")
if __name__ == "__main__":