Skip to content

Commit d806615

Browse files
authored
Merge pull request #168 from sbc100/tty_option
Add --notty to force non-erasing progress bar
2 parents 857f418 + 8b168a4 commit d806615

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

emsdk

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ binaryen_git_repo = 'https://github.com/WebAssembly/binaryen.git'
3030

3131
# Enable this to do very verbose printing about the different steps that are being run. Useful for debugging.
3232
VERBOSE = bool(os.getenv('EMSDK_VERBOSE')) if os.getenv('EMSDK_VERBOSE') != None else False
33+
TTY_OUTPUT = sys.stdout.isatty()
3334

3435
POWERSHELL = bool(os.getenv('EMSDK_POWERSHELL'))
3536

@@ -473,18 +474,31 @@ def download_file(url, dstpath, download_even_if_exists=False, filename_prefix =
473474
else: print("Downloading: %s from %s" % (file_name, url))
474475

475476
file_size_dl = 0
477+
# Draw a progress bar 80 chars wide (in non-TTY mode)
478+
progress_max = 80 - 4
479+
progress_shown = 0
476480
block_sz = 8192
481+
if not TTY_OUTPUT:
482+
print(' [', end='')
477483
while True:
478484
buffer = u.read(block_sz)
479485
if not buffer:
480486
break
481487

482488
file_size_dl += len(buffer)
483489
f.write(buffer)
484-
if sys.stdout.isatty() and file_size:
485-
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
486-
status = status + chr(8)*(len(status)+1)
487-
print(status, end=' ')
490+
if file_size:
491+
percent = file_size_dl * 100.0 / file_size
492+
if TTY_OUTPUT:
493+
status = r" %10d [%3.02f%%]" % (file_size_dl, percent)
494+
print(status, end='\r')
495+
else:
496+
while progress_shown < progress_max * percent / 100:
497+
print('-', end='')
498+
sys.stdout.flush()
499+
progress_shown += 1
500+
if not TTY_OUTPUT:
501+
print(']')
488502
except Exception as e:
489503
print("Error downloading URL '" + url + "': " + str(e))
490504
rmfile(file_name)
@@ -2131,6 +2145,11 @@ def main():
21312145
arg_uses = extract_bool_arg('--uses')
21322146
arg_global = extract_bool_arg('--global')
21332147
arg_embedded = extract_bool_arg('--embedded')
2148+
arg_notty = extract_bool_arg('--notty')
2149+
if arg_notty:
2150+
global TTY_OUTPUT
2151+
TTY_OUTPUT = False
2152+
21342153
cmd = sys.argv[1]
21352154

21362155
# On first run when tag list is not present, populate it to bootstrap.

0 commit comments

Comments
 (0)