From d2503fbf49e144e4725bcb6a0495e38ff6525dc7 Mon Sep 17 00:00:00 2001 From: Daniel Mach Date: Tue, 16 Apr 2024 16:31:27 +0200 Subject: [PATCH] Modernize output.run_pager() --- osc/output/output.py | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/osc/output/output.py b/osc/output/output.py index d646cd22..14455cfd 100644 --- a/osc/output/output.py +++ b/osc/output/output.py @@ -170,17 +170,12 @@ def run_pager(message: Union[bytes, str], tmp_suffix: str = ""): if not message: return - if not sys.stdout.isatty(): - if isinstance(message, str): - print(message) - else: - sys.stdout.buffer.write(message) - else: - tmpfile = tempfile.NamedTemporaryFile(suffix=tmp_suffix) - if isinstance(message, str): - tmpfile.write(bytes(message, 'utf-8')) - else: - tmpfile.write(message) + if not tty.IS_INTERACTIVE: + safe_write(sys.stdout, message) + return + + with tempfile.NamedTemporaryFile(suffix=tmp_suffix) as tmpfile: + safe_write(tmpfile, message) tmpfile.flush() env = os.environ.copy() @@ -195,7 +190,4 @@ def run_pager(message: Union[bytes, str], tmp_suffix: str = ""): env["LESS"] = less_opts cmd = shlex.split(pager) + [tmpfile.name] - try: - run_external(*cmd, env=env) - finally: - tmpfile.close() + run_external(*cmd, env=env)