From f9a44edcbc221df7965d23313c0b3daca20775ad Mon Sep 17 00:00:00 2001 From: Scott Date: Wed, 19 Jan 2022 14:10:35 -0800 Subject: [PATCH] Make cmd_showing_output work with subprocesses that use '\r'. --- exec_utils.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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() -- 2.46.0