Skip to content

Instantly share code, notes, and snippets.

@somewacko
Last active February 3, 2016 05:35
Show Gist options
  • Save somewacko/8190b4a464f0d648b51c to your computer and use it in GitHub Desktop.
Save somewacko/8190b4a464f0d648b51c to your computer and use it in GitHub Desktop.
A simple progress bar with carriage return. Reinventing the wheel.
import math, sys
def print_progress_bar(progress, length=40, fill_char='=', empty_char=' ',
side_char='|'):
'''
Prints a simple progress bar using carriage returns.
Args:
progress (int): The progress done as a percentage 0-100
Args (optional):
length (int): How long the progress bar should be.
fill_char (str): The character used to draw the bar.
empty_char (str): The character used to draw the empty space.
side_char (str): The character used to draw the boundaries of the bar.
'''
if progress < 0 or 100 < progress:
raise RuntimeError('Progress may only be from 0-100.')
not_prog = 100-progress
done = fill_char * int( math.ceil( length*progress/100.0 ) )
left = empty_char * int( math.floor( length*not_prog/100.0 ) )
sys.stdout.write(
'\r{0}{1}{2}{0} {3:3d}%'.format(side_char, done, left, progress)
)
sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment