Skip to content

Commit 8b168a4

Browse files
committed
Add --notty to force non-erasing progress bar
This is useful in CI environments that claim to be TTYs but really don't have escape characters such as \r correctly.
1 parent 8512c60 commit 8b168a4

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)
@@ -2114,6 +2128,11 @@ def main():
21142128
arg_uses = extract_bool_arg('--uses')
21152129
arg_global = extract_bool_arg('--global')
21162130
arg_embedded = extract_bool_arg('--embedded')
2131+
arg_notty = extract_bool_arg('--notty')
2132+
if arg_notty:
2133+
global TTY_OUTPUT
2134+
TTY_OUTPUT = False
2135+
21172136
cmd = sys.argv[1]
21182137

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

0 commit comments

Comments
 (0)