2021-03-10 20:34:00 +01:00
|
|
|
import os
|
2021-03-05 17:09:12 +01:00
|
|
|
import sys
|
2021-03-05 15:55:14 +01:00
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
2021-03-10 17:04:28 +01:00
|
|
|
from termcolor import colored
|
|
|
|
|
2021-03-12 15:58:30 +01:00
|
|
|
from cpl.console.background_color_enum import BackgroundColorEnum
|
2021-03-12 16:06:30 +01:00
|
|
|
from cpl.console.foreground_color_enum import ForegroundColorEnum
|
2021-03-10 17:04:28 +01:00
|
|
|
|
2021-03-05 15:55:14 +01:00
|
|
|
|
|
|
|
class SpinnerThread(threading.Thread):
|
|
|
|
|
2021-03-12 16:06:30 +01:00
|
|
|
def __init__(self, msg_len: int, foreground_color: ForegroundColorEnum, background_color: BackgroundColorEnum):
|
2021-03-05 15:55:14 +01:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
2021-03-10 20:34:00 +01:00
|
|
|
self._msg_len = msg_len
|
2021-03-10 17:04:28 +01:00
|
|
|
self._foreground_color = foreground_color
|
|
|
|
self._background_color = background_color
|
2021-03-05 15:55:14 +01:00
|
|
|
|
2021-03-10 20:34:00 +01:00
|
|
|
self._is_spinning = True
|
|
|
|
|
2021-03-05 15:55:14 +01:00
|
|
|
@staticmethod
|
|
|
|
def _spinner():
|
|
|
|
while True:
|
|
|
|
for cursor in '|/-\\':
|
|
|
|
yield cursor
|
|
|
|
|
2021-03-10 17:04:28 +01:00
|
|
|
def _get_color_args(self) -> list[str]:
|
|
|
|
color_args = []
|
|
|
|
if self._foreground_color is not None:
|
|
|
|
color_args.append(str(self._foreground_color.value))
|
|
|
|
|
|
|
|
if self._background_color is not None:
|
|
|
|
color_args.append(str(self._background_color.value))
|
|
|
|
|
|
|
|
return color_args
|
|
|
|
|
2021-03-05 15:55:14 +01:00
|
|
|
def run(self) -> None:
|
2021-03-10 20:34:00 +01:00
|
|
|
rows, columns = os.popen('stty size', 'r').read().split()
|
|
|
|
end_msg = 'done'
|
|
|
|
columns = int(columns) - self._msg_len - len(end_msg)
|
|
|
|
print(f'{"" : >{columns}}', end='')
|
2021-03-05 15:55:14 +01:00
|
|
|
spinner = self._spinner()
|
|
|
|
while self._is_spinning:
|
2021-03-10 20:34:00 +01:00
|
|
|
print(colored(f'{next(spinner): >{len(end_msg)}}', *self._get_color_args()), end='')
|
2021-03-05 15:55:14 +01:00
|
|
|
time.sleep(0.1)
|
2021-03-10 20:34:00 +01:00
|
|
|
back = ''
|
|
|
|
for i in range(0, len(end_msg)):
|
|
|
|
back += '\b'
|
|
|
|
|
|
|
|
print(back, end='')
|
2021-03-05 17:09:12 +01:00
|
|
|
sys.stdout.flush()
|
2021-03-05 15:55:14 +01:00
|
|
|
|
2021-03-10 20:34:00 +01:00
|
|
|
print(colored(end_msg, *self._get_color_args()), end='')
|
2021-03-05 15:55:14 +01:00
|
|
|
|
|
|
|
def stop_spinning(self):
|
|
|
|
self._is_spinning = False
|
|
|
|
time.sleep(0.1)
|