2021.4 #19
@ -1,5 +1,4 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
from collections import Callable
|
from collections import Callable
|
||||||
from typing import Union, Optional
|
from typing import Union, Optional
|
||||||
|
|
||||||
@ -253,18 +252,28 @@ class Console:
|
|||||||
cls._output(string, x, y, end='')
|
cls._output(string, x, y, end='')
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def spinner(cls, message: str, call: Callable, *args) -> any:
|
def spinner(cls, message: str, call: Callable, *args, text_foreground_color: ForegroundColor = None, spinner_foreground_color: ForegroundColor = None, text_background_color: BackgroundColor = None, spinner_background_color: BackgroundColor = None) -> any:
|
||||||
if cls._hold_back:
|
if cls._hold_back:
|
||||||
cls._hold_back_calls.append(ConsoleCall(cls.spinner, message, call, *args))
|
cls._hold_back_calls.append(ConsoleCall(cls.spinner, message, call, *args))
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if text_foreground_color is not None:
|
||||||
|
cls.set_foreground_color(text_foreground_color)
|
||||||
|
|
||||||
|
if text_background_color is not None:
|
||||||
|
cls.set_background_color(text_background_color)
|
||||||
|
|
||||||
cls.write_line(message)
|
cls.write_line(message)
|
||||||
cls.set_hold_back(True)
|
cls.set_hold_back(True)
|
||||||
spinner = SpinnerThread()
|
spinner = SpinnerThread(spinner_foreground_color, spinner_background_color)
|
||||||
spinner.start()
|
spinner.start()
|
||||||
return_value = call(*args)
|
return_value = call(*args)
|
||||||
spinner.stop_spinning()
|
spinner.stop_spinning()
|
||||||
cls.set_hold_back(False)
|
cls.set_hold_back(False)
|
||||||
|
|
||||||
|
cls.set_foreground_color(ForegroundColor.default)
|
||||||
|
cls.set_background_color(BackgroundColor.default)
|
||||||
|
|
||||||
for call in cls._hold_back_calls:
|
for call in cls._hold_back_calls:
|
||||||
call.function(*call.args)
|
call.function(*call.args)
|
||||||
|
|
||||||
|
@ -2,13 +2,20 @@ import sys
|
|||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from termcolor import colored
|
||||||
|
|
||||||
|
from cpl.console.background_color import BackgroundColor
|
||||||
|
from cpl.console.foreground_color import ForegroundColor
|
||||||
|
|
||||||
|
|
||||||
class SpinnerThread(threading.Thread):
|
class SpinnerThread(threading.Thread):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, foreground_color: ForegroundColor, background_color: BackgroundColor):
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
|
|
||||||
self._is_spinning = True
|
self._is_spinning = True
|
||||||
|
self._foreground_color = foreground_color
|
||||||
|
self._background_color = background_color
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _spinner():
|
def _spinner():
|
||||||
@ -16,17 +23,26 @@ class SpinnerThread(threading.Thread):
|
|||||||
for cursor in '|/-\\':
|
for cursor in '|/-\\':
|
||||||
yield cursor
|
yield cursor
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
print('\t', end='')
|
print('\t', end='')
|
||||||
spinner = self._spinner()
|
spinner = self._spinner()
|
||||||
while self._is_spinning:
|
while self._is_spinning:
|
||||||
print(next(spinner), end='')
|
print(colored(next(spinner), *self._get_color_args()), end='')
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
print('\b', end='')
|
print('\b', end='')
|
||||||
|
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
print('done', end='')
|
print(colored('done', *self._get_color_args()), end='')
|
||||||
|
|
||||||
def stop_spinning(self):
|
def stop_spinning(self):
|
||||||
self._is_spinning = False
|
self._is_spinning = False
|
||||||
|
Loading…
Reference in New Issue
Block a user