-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspinner.py
More file actions
58 lines (50 loc) · 1.83 KB
/
Copy pathspinner.py
File metadata and controls
58 lines (50 loc) · 1.83 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
import threading
import time
import sys
class Spinner:
"""
A simple text-based spinner class to display a loading animation in the console.
Attributes:
message (str): The message to display next to the spinner. Defaults to "Loading".
delay (float): The delay between each animation step in seconds. Defaults to 0.1.
running (bool): Whether the spinner is currently running. Defaults to False.
spinner (threading.Thread): The thread that runs the spinner animation.
Methods:
start(): Starts the spinner animation.
stop(): Stops the spinner animation.
"""
def __init__(self, message="Loading", delay=0.1):
"""
Initializes a new Spinner instance.
Args:
message (str, optional): The message to display next to the spinner. Defaults to "Loading".
delay (float, optional): The delay between each animation step in seconds. Defaults to 0.1.
"""
self.message = message
self.delay = delay
self.running = False
self.spinner = threading.Thread(target=self._animate)
def _animate(self):
"""
Animates the spinner by printing the message and a rotating character.
"""
chars = "|/-\\"
while self.running:
for char in chars:
sys.stdout.write(f'\r{self.message} {char}')
sys.stdout.flush()
time.sleep(self.delay)
def start(self):
"""
Starts the spinner animation.
"""
self.running = True
self.spinner.start()
def stop(self):
"""
Stops the spinner animation and clears the console line.
"""
self.running = False
self.spinner.join()
sys.stdout.write('\r' + ' ' * (len(self.message) + 2) + '\r')
sys.stdout.flush()