Skip to content

Instantly share code, notes, and snippets.

@eloonstra
Last active March 25, 2022 17:12
Show Gist options
  • Save eloonstra/7ae65d928845f11c37c33939dacfac93 to your computer and use it in GitHub Desktop.
Save eloonstra/7ae65d928845f11c37c33939dacfac93 to your computer and use it in GitHub Desktop.
A simple progress bar class written in Python.
import os
class ProgressBar:
progress: int = 0
def __init__(self) -> None:
self.columns, self.lines = os.get_terminal_size()
def update(self, progress: int):
if progress > 100 or progress < 0:
raise ValueError('Progress must be between 0 and 100.')
self.progress = progress
return self
def draw(self) -> None:
scaled_progress: int = self.progress * (self.columns - 20) // 100
print('\033[42m\033[30m', end='')
print('\033[?25l', end='')
print('\rProgress: [' + ' ' * (3 - len(str(self.progress))) + str(self.progress) + '%]', end='')
print('\033[0m', end='')
print(' [' + '#' * scaled_progress + '.' * (self.columns - 20 - scaled_progress) + ']', end='')
print('\033[?25h', end='')
def clear(self) -> None:
print('\r' + ' ' * self.columns, end='\r')
@eloonstra
Copy link
Author

Example.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment