From: Scott Date: Wed, 19 Jan 2022 22:10:35 +0000 (-0800) Subject: Make cmd_showing_output work with subprocesses that use '\r'. X-Git-Url: https://wannabe.guru.org/gitweb/?a=commitdiff_plain;h=f9a44edcbc221df7965d23313c0b3daca20775ad;p=python_utils.git Make cmd_showing_output work with subprocesses that use '\r'. --- diff --git a/exec_utils.py b/exec_utils.py index b52f52f..36d48fa 100644 --- a/exec_utils.py +++ b/exec_utils.py @@ -4,6 +4,7 @@ import atexit import logging import shlex import subprocess +import sys from typing import List, Optional @@ -11,16 +12,19 @@ logger = logging.getLogger(__file__) def cmd_showing_output(command: str) -> None: + line_enders = set([b'\n', b'\r']) p = subprocess.Popen( command, shell=True, bufsize=0, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=False, ) - for line in iter(p.stdout.readline, b''): - print(line.decode('utf-8'), end='') - p.stdout.close() + for char in iter(lambda: p.stdout.read(1), b''): + sys.stdout.buffer.write(char) + if char in line_enders: + sys.stdout.flush() p.wait()