-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadbard.py
More file actions
63 lines (53 loc) · 1.69 KB
/
loadbard.py
File metadata and controls
63 lines (53 loc) · 1.69 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import sys
import time
LOADBAR_CHAR = "█"
def spinning_cursor():
while True:
for cursor in '|/-\\':
yield cursor
progress = spinning_cursor()
def wheel(message):
sys.stdout.write(f'\b')
sys.stdout.write(next(progress))
sys.stdout.flush()
def bar(message):
sys.stdout.write(LOADBAR_CHAR)
sys.stdout.flush()
def percentage(message, replace=True):
if replace:
sys.stdout.write(f'\b'*len(message)*10)
sys.stdout.write(message)
sys.stdout.flush()
else:
print(message)
class LoadBard:
start_message = ""
mod_message = ''
time_start = float()
timer = False
log = False
detailed = False
TYPES = {"wheel": wheel, "bar": bar, "percentage": percentage}
update_func = None
def start(self,message="", timer=False, type="percentage"):
self.update_func = self.TYPES[type]
if timer:
self.timer = True
self.time_start = time.perf_counter()
self.start_message = message
self.mod_message = f'{message}:\t'
sys.stdout.write(self.mod_message)
def update(self, message="", replace=False):
self.update_func(self.mod_message+message, replace=replace)
def end(self):
s = "\b"*len(self.mod_message)*10
if self.timer:
sys.stdout.write(f'{s}{self.start_message} completed. '
f'Completed in {time.perf_counter()-self.time_start} seconds.')
else:
sys.stdout.write(f'{s}{self.start_message} completed.')
sys.stdout.flush()
sys.stdout.write("\n") # this ends the progress bar
def log(line):
with open("log.txt", "a") as f:
f.write(line + "\n")