-
Notifications
You must be signed in to change notification settings - Fork 4
/
progress_bar.py
33 lines (29 loc) · 985 Bytes
/
progress_bar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import sys
import time
import math
def plot_bar(amount: int, max_amount: int, length: int, print_amount: bool = False, label: str = ''):
percent = amount / max_amount * 100
LEFT_BRACKET = '\033[0m['
RIGHT_BRACKET = '\033[0m]'
FULL_BLOCK = '\033[0;32m━'
EMPTY_BLOCK = '\033[0;31m━'
inner = length - 2
filled_space = int(math.ceil((percent / 100) * inner))
left_space = inner - filled_space
sprint = lambda text: sys.stdout.write(text)
sprint(f'\r')
sprint(LEFT_BRACKET)
sprint(FULL_BLOCK * filled_space)
if left_space:
sprint(EMPTY_BLOCK * left_space)
sprint(RIGHT_BRACKET)
sprint(' ' + str(int(percent)).rjust(3) + '%')
if print_amount:
just = len(str(max_amount))
sprint(f' {str(amount).rjust(just)}/{max_amount} ')
sprint(label)
sys.stdout.flush()
if __name__ == "__main__":
for x in range(1000):
time.sleep(0.01)
plot_bar(x, 999, 50, True, 'blocks')