Skip to content

Instantly share code, notes, and snippets.

@robey
Created February 5, 2020 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robey/351676cc5a964a7e8dedf34240ab37d9 to your computer and use it in GitHub Desktop.
Save robey/351676cc5a964a7e8dedf34240ab37d9 to your computer and use it in GitHub Desktop.
import sys
class ProgressBar:
def __init__(self, title: str, width: int):
self.title = title
self.width = width
self.amount = 0
self.update(0)
def update(self, amount: int) -> None:
"amount is 0 - 100"
self.amount = amount
fill = round(amount * self.width / 100)
self.clear()
sys.stdout.write("\r{} [{}{}] {:3d}%\r".format(self.title, "#" * fill, " " * (self.width - fill), amount))
sys.stdout.flush()
def clear(self) -> None:
sys.stdout.write("\r{}\r".format(" " * (len(self.title) + self.width + 15)))
sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment