-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
56 lines (48 loc) · 2.29 KB
/
utils.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
import sys
import time
import threading
from colorama import init, Fore, Style
# Initialize colorama
init(autoreset=True)
class LoadingAnimation:
def __init__(self):
# Constants
self.LOADING_FRAMES = ["■□□□", "□■□□", "□□■□", "□□□■"]
self.LOADING_MESSAGE = "The task is in progress, please wait..."
self.CLEAR_MESSAGE = " " * 55 # Adjust the number of spaces as needed
# Flags to control the loading animation
self.loading_animation_running = False
self.loading_thread = None
def show_loading_animation(self, display=True, custom_message=None, disable_message=False):
if display:
# If the loading animation is already running, return
if self.loading_animation_running:
return
self.loading_animation_running = True
def square_loading_animation():
while self.loading_animation_running:
for frame in self.LOADING_FRAMES:
message = f"\r {Fore.CYAN}{Style.BRIGHT}{frame} {Fore.RESET}"
if custom_message:
message += custom_message
else:
message += self.LOADING_MESSAGE
sys.stdout.write(message)
sys.stdout.flush()
time.sleep(0.3)
# Start the loading animation in a separate thread
self.loading_thread = threading.Thread(target=square_loading_animation)
self.loading_thread.start()
else:
self.loading_animation_running = False
if self.loading_thread:
self.loading_thread.join() # Wait for the loading animation thread to finish
sys.stdout.write("\r" + self.CLEAR_MESSAGE + "\r") # Clear the loading message
if custom_message:
print(" " + f"{Fore.GREEN}{Style.BRIGHT}{custom_message}") # Print a custom message if provided
elif disable_message == True:
return
else:
print(" Task completed!") # Print "Task completed" as the default message
# Create an instance of the LoadingAnimation class
loading_animation = LoadingAnimation()