Make cmd_showing_output work with subprocesses that use '\r'.
authorScott <[email protected]>
Wed, 19 Jan 2022 22:10:35 +0000 (14:10 -0800)
committerScott <[email protected]>
Wed, 19 Jan 2022 22:10:35 +0000 (14:10 -0800)
exec_utils.py

index b52f52f0dc785033fc437a638c734d79cf8d5aa8..36d48fad2b4c3191c0db0c612a58db502e6be197 100644 (file)
@@ -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()