Last active
March 25, 2022 17:12
-
-
Save eloonstra/7ae65d928845f11c37c33939dacfac93 to your computer and use it in GitHub Desktop.
A simple progress bar class written in Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example.