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