-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloading_indicator.py
More file actions
36 lines (32 loc) · 1.19 KB
/
loading_indicator.py
File metadata and controls
36 lines (32 loc) · 1.19 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
import sys
from itertools import cycle
import threading
import time
class LoadingIndicator:
"""
A simple loading indicator to show that the program is still running.
"""
def __init__(self, description="Processing"):
self.description = description
self.is_running = False
self.spinner = cycle(['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'])
self.spinner_thread = None
def _spin(self):
"""Internal method to update the spinner"""
while self.is_running:
sys.stdout.write(f"\r{self.description} {next(self.spinner)} ")
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write("\r" + " " * (len(self.description) + 10) + "\r")
sys.stdout.flush()
def start(self):
"""Start the loading indicator"""
self.is_running = True
self.spinner_thread = threading.Thread(target=self._spin)
self.spinner_thread.daemon = True
self.spinner_thread.start()
def stop(self):
"""Stop the loading indicator"""
self.is_running = False
if self.spinner_thread is not None:
self.spinner_thread.join()