flush lines with carriage returns
for terminal progress bars
+from functools import partial
import subprocess
def execute_cmd(cmd, capture=False, **kwargs):
@@ -16,9 +17,21 @@ def execute_cmd(cmd, capture=False, **kwargs):
raise subprocess.CalledProcessError(ret, cmd)
return
+ buf = []
+ def flush():
+ line = b''.join(buf).decode('utf8', 'replace')
+ buf[:] = []
+ return line
+
+ c_last = ''
try:
- for line in iter(proc.stdout.readline, b''):
- yield line.decode('utf8', 'replace')
+ for c in iter(partial(proc.stdout.read, 1), b''):
+ if c_last == b'\r' and buf and c != b'\n':
+ yield flush()
+ buf.append(c)
+ if c == b'\n':
+ yield flush()
+ c_last = c
finally:
ret = proc.wait()
if ret != 0:
