flush lines with carriage returns

for terminal progress bars
pull/14/head
Min RK 2017-05-24 16:33:35 -07:00
rodzic f3505db967
commit 698f9e1196
1 zmienionych plików z 16 dodań i 3 usunięć

Wyświetl plik

@ -1,3 +1,4 @@
from functools import partial
import subprocess import subprocess
def execute_cmd(cmd, capture=False, **kwargs): def execute_cmd(cmd, capture=False, **kwargs):
@ -15,10 +16,22 @@ def execute_cmd(cmd, capture=False, **kwargs):
if ret != 0: if ret != 0:
raise subprocess.CalledProcessError(ret, cmd) raise subprocess.CalledProcessError(ret, cmd)
return return
buf = []
def flush():
line = b''.join(buf).decode('utf8', 'replace')
buf[:] = []
return line
c_last = ''
try: try:
for line in iter(proc.stdout.readline, b''): for c in iter(partial(proc.stdout.read, 1), b''):
yield line.decode('utf8', 'replace') 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: finally:
ret = proc.wait() ret = proc.wait()
if ret != 0: if ret != 0: