Add a status bar to show test progress

This commit is contained in:
Zakhary Kaplan 2020-07-28 18:09:39 -04:00
parent c58ad3367b
commit 3fc2728481
1 changed files with 34 additions and 6 deletions

View File

@ -766,7 +766,7 @@ def get_attributes_by_tag_name(dom, tag_name):
def print_dots(shutdown_event): def print_dots(shutdown_event):
"""Built in callback function used by Thread classes for printing """Built in callback function used by Thread classes for printing
status status dots
""" """
def inner(current, total, start=False, end=False): def inner(current, total, start=False, end=False):
if shutdown_event.isSet(): if shutdown_event.isSet():
@ -779,6 +779,36 @@ def print_dots(shutdown_event):
return inner return inner
def update_status_bar(shutdown_event):
"""Built in callback function used by Thread classes for updating
status bar
"""
empty = ' '
cursor = '>'
filled = '='
def inner(current, total, start=False, end=False):
if shutdown_event.isSet():
return
# Update status bar
header = '({:d}/{:d})'.format(current + 1, total)
_, columns = os.popen('stty size', 'r').read().split()
width = (int(columns) // 2) - len(header) - 4
completion = width * (current + 1) // total
progress = '[{}{}{}]'.format(filled * (completion - 1), cursor,
empty * (width - completion))
status_bar = '{} {}'.format(header, progress)
# Print updated status bar
sys.stdout.write('\033[K{}\r'.format(status_bar))
if current + 1 == total and end is True:
sys.stdout.write('\033[K') # clear to end of line
sys.stdout.flush()
return inner
def do_nothing(*args, **kwargs): def do_nothing(*args, **kwargs):
pass pass
@ -1865,7 +1895,7 @@ def shell():
if quiet or debug: if quiet or debug:
callback = do_nothing callback = do_nothing
else: else:
callback = print_dots(shutdown_event) callback = update_status_bar(shutdown_event)
printer('Retrieving speedtest.net configuration...', quiet) printer('Retrieving speedtest.net configuration...', quiet)
try: try:
@ -1932,8 +1962,7 @@ def shell():
'%(latency)s ms' % results.server, quiet) '%(latency)s ms' % results.server, quiet)
if args.download: if args.download:
printer('Testing download speed', quiet, printer('Testing download speed...', quiet)
end=('', '\n')[bool(debug)])
speedtest.download( speedtest.download(
callback=callback, callback=callback,
threads=(None, 1)[args.single] threads=(None, 1)[args.single]
@ -1946,8 +1975,7 @@ def shell():
printer('Skipping download test', quiet) printer('Skipping download test', quiet)
if args.upload: if args.upload:
printer('Testing upload speed', quiet, printer('Testing upload speed...', quiet)
end=('', '\n')[bool(debug)])
speedtest.upload( speedtest.upload(
callback=callback, callback=callback,
pre_allocate=args.pre_allocate, pre_allocate=args.pre_allocate,