fix terminal resizing making progress bar go crazy

This commit is contained in:
Nick Sweeting 2019-03-26 05:30:23 -04:00
parent 63abc8980c
commit 4f8c99011a
2 changed files with 4 additions and 3 deletions

View file

@ -77,7 +77,7 @@ USE_WGET = FETCH_WGET or FETCH_WGET_REQUISITES or FETCH_WARC
try: try:
### Terminal Configuration ### Terminal Configuration
TERM_WIDTH = shutil.get_terminal_size((100, 10)).columns TERM_WIDTH = lambda: shutil.get_terminal_size((100, 10)).columns
ANSI = { ANSI = {
'reset': '\033[00;00m', 'reset': '\033[00;00m',
'lightblue': '\033[01;30m', 'lightblue': '\033[01;30m',

View file

@ -482,16 +482,17 @@ class TimedProgress:
self.p.terminate() self.p.terminate()
self.p = None self.p = None
sys.stdout.write('\r{}{}\r'.format((' ' * TERM_WIDTH), ANSI['reset'])) # clear whole terminal line sys.stdout.write('\r{}{}\r'.format((' ' * TERM_WIDTH()), ANSI['reset'])) # clear whole terminal line
sys.stdout.flush() sys.stdout.flush()
def progress_bar(seconds: int, prefix: str='') -> None: def progress_bar(seconds: int, prefix: str='') -> None:
"""show timer in the form of progress bar, with percentage and seconds remaining""" """show timer in the form of progress bar, with percentage and seconds remaining"""
chunk = '' if sys.stdout.encoding == 'UTF-8' else '#' chunk = '' if sys.stdout.encoding == 'UTF-8' else '#'
chunks = TERM_WIDTH - len(prefix) - 20 # number of progress chunks to show (aka max bar width) chunks = TERM_WIDTH() - len(prefix) - 20 # number of progress chunks to show (aka max bar width)
try: try:
for s in range(seconds * chunks): for s in range(seconds * chunks):
chunks = TERM_WIDTH() - len(prefix) - 20
progress = s / chunks / seconds * 100 progress = s / chunks / seconds * 100
bar_width = round(progress/(100/chunks)) bar_width = round(progress/(100/chunks))