import asyncio
import math
import sys
from colorama import Style, Fore
[docs]def colorize(text, bg=None, fg=None, style=None):
fg = fg or ''
bg = bg or ''
style = style or ''
return f'{bg}{fg}{style}{text}{Style.RESET_ALL}'
_spinner_symbols = (
'|', '/', '-', '\\', '|', '/', '-', '\\'
)
[docs]async def spinner(condition,
sleep_time=0.25, message='',
fg=Fore.WHITE, bg=None,
symbols=_spinner_symbols):
i = 0
num_symbols = len(symbols)
while not condition():
symbol = symbols[i % num_symbols]
i += 1
if callable(message):
msg = message()
else: # pragma: no cover
msg = message
m = colorize(f'{msg} {symbol}', fg=fg, bg=bg)
msg = f'\r\x1b[K{m}'
print(msg, end='', flush=True, file=sys.stderr)
await asyncio.sleep(sleep_time)
def chunk_list(data, n):
for i in range(0, len(data), n):
yield data[i:i+n]
[docs]def print_table(data, formatting=None, file=sys.stdout):
print('\n'.join(format_table(data, formatting,)), file=file)
[docs]def goto_xy(stream, x, y): # pragma: no cover
# Make sure colorama is init on windows.
print('%c[%d;%df' % (0x1B, y, x), end='', file=stream)
[docs]async def progress_bar(
value_func, max_value,
value_formatter=None,
include_value=True,
dents=50,
sleep_time=0.005,
file=None,
full_symbol='#',
empty_symbol='-',
start_symbol='[',
end_symbol=']',
):
value = 0
value_formatter = value_formatter or (lambda x, y: f'{x} / {y}')
while value < max_value:
value = value_func()
await asyncio.sleep(sleep_time)
progress = math.ceil(value / max_value * dents)
progress_line = (
(progress * full_symbol) + (dents - progress) * empty_symbol
)
out = f'\r\x1b[K{start_symbol}{progress_line}{end_symbol}'
if include_value:
out += value_formatter(value, max_value)
print(out, end='', flush=True, file=file)