From 698f9e11961d5b42516e6aa7d4a8c26c64e8bc4d Mon Sep 17 00:00:00 2001 From: Min RK Date: Wed, 24 May 2017 16:33:35 -0700 Subject: [PATCH] flush lines with carriage returns for terminal progress bars --- repo2docker/utils.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/repo2docker/utils.py b/repo2docker/utils.py index 051d463a..b3319edd 100644 --- a/repo2docker/utils.py +++ b/repo2docker/utils.py @@ -1,3 +1,4 @@ +from functools import partial import subprocess def execute_cmd(cmd, capture=False, **kwargs): @@ -15,10 +16,22 @@ def execute_cmd(cmd, capture=False, **kwargs): if ret != 0: 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: