-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadingbar.py
95 lines (72 loc) · 2.19 KB
/
loadingbar.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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Draw loadingbar.
Attributes:
bars (TYPE): List of bars you can use.
Loading (TYPE): Description
thrd (TYPE): Description
Deleted Attributes:
L (TYPE): Description
t (TYPE): Description
"""
import sys
import time
# Create and launch a thread
from threading import Thread
bars = [
'01234567890123456789',
'. ',
'Loading... ',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'░▒▓█▓▒░▒▓█▓▒░▒▓█▓▒',
'▒▓█▓▒░░░░░░░░░░░░░',
'█▓▒░░▒▓████████████████',
'░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ ',
'░▒▓█▓▒ ',
'█▓▒░ ░▒▓████████████████']
class LoadingBar:
"""A Loading bar class."""
def __init__(self):
"""Class initialization."""
self._running = True
self._to_right = True
self._speed = 24
def stop(self):
"""Stop the drawing."""
self._running = False
def change_direction(self):
"""Change the direction."""
self._to_right = not self._to_right
def change_speed(self, speed: int):
"""Change the speed.
Args:
speed (int): Update FPS
"""
self._speed = speed
def start(self, load_bar: str):
"""Start the drawing.
Args:
load_bar (str): Bar
"""
while self._running and len(load_bar) > 0:
for i in range(len(load_bar)):
if self._to_right:
sys.stdout.write('\r' + load_bar[-i:] + load_bar[:-i])
else:
sys.stdout.write('\r' + load_bar[i:] + load_bar[:i])
sys.stdout.flush()
time.sleep(1 / self._speed)
Loading = LoadingBar()
thrd = Thread(target=Loading.start, args=(bars[5], ))
thrd.start()
time.sleep(5)
# Change direction
Loading.change_direction()
time.sleep(5)
# Change direction
Loading.change_direction()
# Change the speed
Loading.change_speed(50)
time.sleep(5)
# Signal termination
Loading.stop()
# Wait for actual termination (if needed)
thrd.join()