2021.4 #19
@ -15,6 +15,9 @@ from cpl.console.spinner_thread import SpinnerThread
|
|||||||
|
|
||||||
|
|
||||||
class Console:
|
class Console:
|
||||||
|
"""
|
||||||
|
Useful functions for handling with input and output
|
||||||
|
"""
|
||||||
_is_first_write = True
|
_is_first_write = True
|
||||||
|
|
||||||
_background_color: BackgroundColorEnum = BackgroundColorEnum.default
|
_background_color: BackgroundColorEnum = BackgroundColorEnum.default
|
||||||
@ -59,6 +62,11 @@ class Console:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def set_background_color(cls, color: Union[BackgroundColorEnum, str]):
|
def set_background_color(cls, color: Union[BackgroundColorEnum, str]):
|
||||||
|
"""
|
||||||
|
Sets the background color
|
||||||
|
:param color:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if type(color) is str:
|
if type(color) is str:
|
||||||
cls._background_color = BackgroundColorEnum[color]
|
cls._background_color = BackgroundColorEnum[color]
|
||||||
else:
|
else:
|
||||||
@ -66,7 +74,11 @@ class Console:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def set_foreground_color(cls, color: Union[ForegroundColorEnum, str]):
|
def set_foreground_color(cls, color: Union[ForegroundColorEnum, str]):
|
||||||
|
"""
|
||||||
|
Sets the foreground color
|
||||||
|
:param color:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if type(color) is str:
|
if type(color) is str:
|
||||||
cls._foreground_color = ForegroundColorEnum[color]
|
cls._foreground_color = ForegroundColorEnum[color]
|
||||||
else:
|
else:
|
||||||
@ -74,11 +86,21 @@ class Console:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def reset_cursor_position(cls):
|
def reset_cursor_position(cls):
|
||||||
|
"""
|
||||||
|
Resets cursor position
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
cls._x = None
|
cls._x = None
|
||||||
cls._y = None
|
cls._y = None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def set_cursor_position(cls, x: int, y: int):
|
def set_cursor_position(cls, x: int, y: int):
|
||||||
|
"""
|
||||||
|
Sets cursor position
|
||||||
|
:param x:
|
||||||
|
:param y:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
cls._x = x
|
cls._x = x
|
||||||
cls._y = y
|
cls._y = y
|
||||||
|
|
||||||
@ -88,6 +110,14 @@ class Console:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _output(cls, string: str, x: int = None, y: int = None, end='\n'):
|
def _output(cls, string: str, x: int = None, y: int = None, end='\n'):
|
||||||
|
"""
|
||||||
|
Prints given output with given format
|
||||||
|
:param string:
|
||||||
|
:param x:
|
||||||
|
:param y:
|
||||||
|
:param end:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if cls._is_first_write:
|
if cls._is_first_write:
|
||||||
cls._is_first_write = False
|
cls._is_first_write = False
|
||||||
|
|
||||||
@ -112,6 +142,10 @@ class Console:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _show_select_menu(cls):
|
def _show_select_menu(cls):
|
||||||
|
"""
|
||||||
|
Shows the select menu
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if not cls._is_first_select_menu_output:
|
if not cls._is_first_select_menu_output:
|
||||||
for _ in range(0, len(cls._select_menu_items) + 1):
|
for _ in range(0, len(cls._select_menu_items) + 1):
|
||||||
print('\b', end="\r")
|
print('\b', end="\r")
|
||||||
@ -130,6 +164,11 @@ class Console:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _select_menu_key_press(cls, key: Key):
|
def _select_menu_key_press(cls, key: Key):
|
||||||
|
"""
|
||||||
|
Event function when key press is detected
|
||||||
|
:param key:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if key == Key.down:
|
if key == Key.down:
|
||||||
if cls._selected_menu_item_index == len(cls._select_menu_items) - 1:
|
if cls._selected_menu_item_index == len(cls._select_menu_items) - 1:
|
||||||
return
|
return
|
||||||
@ -151,6 +190,11 @@ class Console:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def banner(cls, string: str):
|
def banner(cls, string: str):
|
||||||
|
"""
|
||||||
|
Prints the string as a banner
|
||||||
|
:param string:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if cls._disabled:
|
if cls._disabled:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -161,8 +205,21 @@ class Console:
|
|||||||
ascii_banner = pyfiglet.figlet_format(string)
|
ascii_banner = pyfiglet.figlet_format(string)
|
||||||
cls.write_line(ascii_banner)
|
cls.write_line(ascii_banner)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def color_reset(cls):
|
||||||
|
"""
|
||||||
|
Resets color
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
cls._background_color = BackgroundColorEnum.default
|
||||||
|
cls._foreground_color = ForegroundColorEnum.default
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def clear(cls):
|
def clear(cls):
|
||||||
|
"""
|
||||||
|
Clears the console
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if cls._hold_back:
|
if cls._hold_back:
|
||||||
cls._hold_back_calls.append(ConsoleCall(cls.clear))
|
cls._hold_back_calls.append(ConsoleCall(cls.clear))
|
||||||
return
|
return
|
||||||
@ -171,6 +228,10 @@ class Console:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def close(cls):
|
def close(cls):
|
||||||
|
"""
|
||||||
|
Close the application
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if cls._disabled:
|
if cls._disabled:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -185,10 +246,20 @@ class Console:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def disable(cls):
|
def disable(cls):
|
||||||
|
"""
|
||||||
|
Disable console interaction
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
cls._disabled = True
|
cls._disabled = True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def error(cls, string: str, tb: str = None):
|
def error(cls, string: str, tb: str = None):
|
||||||
|
"""
|
||||||
|
Prints an error with traceback
|
||||||
|
:param string:
|
||||||
|
:param tb:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if cls._disabled:
|
if cls._disabled:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -205,10 +276,19 @@ class Console:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def enable(cls):
|
def enable(cls):
|
||||||
|
"""
|
||||||
|
Enable console interaction
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
cls._disabled = False
|
cls._disabled = False
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def read(cls, output: str = None) -> str:
|
def read(cls, output: str = None) -> str:
|
||||||
|
"""
|
||||||
|
Read in line
|
||||||
|
:param output:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if output is not None and not cls._hold_back:
|
if output is not None and not cls._hold_back:
|
||||||
cls.write_line(output)
|
cls.write_line(output)
|
||||||
|
|
||||||
@ -216,6 +296,11 @@ class Console:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def read_line(cls, output: str = None) -> str:
|
def read_line(cls, output: str = None) -> str:
|
||||||
|
"""
|
||||||
|
Reads in next line
|
||||||
|
:param output:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if cls._disabled and not cls._hold_back:
|
if cls._disabled and not cls._hold_back:
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
@ -226,13 +311,14 @@ class Console:
|
|||||||
|
|
||||||
return input()
|
return input()
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def color_reset(cls):
|
|
||||||
cls._background_color = BackgroundColorEnum.default
|
|
||||||
cls._foreground_color = ForegroundColorEnum.default
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def table(cls, header: list[str], values: list[list[str]]):
|
def table(cls, header: list[str], values: list[list[str]]):
|
||||||
|
"""
|
||||||
|
Prints a table with header and values
|
||||||
|
:param header:
|
||||||
|
:param values:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if cls._disabled:
|
if cls._disabled:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -254,6 +340,19 @@ class Console:
|
|||||||
cursor_foreground_color: Union[str, ForegroundColorEnum] = ForegroundColorEnum.default,
|
cursor_foreground_color: Union[str, ForegroundColorEnum] = ForegroundColorEnum.default,
|
||||||
cursor_background_color: Union[str, BackgroundColorEnum] = BackgroundColorEnum.default
|
cursor_background_color: Union[str, BackgroundColorEnum] = BackgroundColorEnum.default
|
||||||
) -> str:
|
) -> str:
|
||||||
|
"""
|
||||||
|
Prints select menu
|
||||||
|
:param char:
|
||||||
|
:param message:
|
||||||
|
:param options:
|
||||||
|
:param header_foreground_color:
|
||||||
|
:param header_background_color:
|
||||||
|
:param option_foreground_color:
|
||||||
|
:param option_background_color:
|
||||||
|
:param cursor_foreground_color:
|
||||||
|
:param cursor_background_color:
|
||||||
|
:return: Selected option as str
|
||||||
|
"""
|
||||||
cls._selected_menu_item_char = char
|
cls._selected_menu_item_char = char
|
||||||
cls.options = options
|
cls.options = options
|
||||||
cls._select_menu_items = cls.options
|
cls._select_menu_items = cls.options
|
||||||
@ -285,6 +384,19 @@ class Console:
|
|||||||
spinner_foreground_color: Union[str, ForegroundColorEnum] = None,
|
spinner_foreground_color: Union[str, ForegroundColorEnum] = None,
|
||||||
text_background_color: Union[str, BackgroundColorEnum] = None,
|
text_background_color: Union[str, BackgroundColorEnum] = None,
|
||||||
spinner_background_color: Union[str, BackgroundColorEnum] = None, **kwargs) -> any:
|
spinner_background_color: Union[str, BackgroundColorEnum] = None, **kwargs) -> any:
|
||||||
|
"""
|
||||||
|
Shows spinner and calls given function
|
||||||
|
When function has ended the spinner stops
|
||||||
|
:param message:
|
||||||
|
:param call:
|
||||||
|
:param args:
|
||||||
|
:param text_foreground_color:
|
||||||
|
:param spinner_foreground_color:
|
||||||
|
:param text_background_color:
|
||||||
|
:param spinner_background_color:
|
||||||
|
:param kwargs:
|
||||||
|
:return: Return value of call
|
||||||
|
"""
|
||||||
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
|
||||||
@ -319,6 +431,11 @@ class Console:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def write(cls, *args):
|
def write(cls, *args):
|
||||||
|
"""
|
||||||
|
Prints in active line
|
||||||
|
:param args:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if cls._disabled:
|
if cls._disabled:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -331,6 +448,13 @@ class Console:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def write_at(cls, x: int, y: int, *args):
|
def write_at(cls, x: int, y: int, *args):
|
||||||
|
"""
|
||||||
|
Prints at given position
|
||||||
|
:param x:
|
||||||
|
:param y:
|
||||||
|
:param args:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if cls._disabled:
|
if cls._disabled:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -343,6 +467,11 @@ class Console:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def write_line(cls, *args):
|
def write_line(cls, *args):
|
||||||
|
"""
|
||||||
|
Prints to new line
|
||||||
|
:param args:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if cls._disabled:
|
if cls._disabled:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -357,6 +486,13 @@ class Console:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def write_line_at(cls, x: int, y: int, *args):
|
def write_line_at(cls, x: int, y: int, *args):
|
||||||
|
"""
|
||||||
|
Prints new line at given position
|
||||||
|
:param x:
|
||||||
|
:param y:
|
||||||
|
:param args:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if cls._disabled:
|
if cls._disabled:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -4,6 +4,11 @@ from collections import Callable
|
|||||||
class ConsoleCall:
|
class ConsoleCall:
|
||||||
|
|
||||||
def __init__(self, function: Callable, *args):
|
def __init__(self, function: Callable, *args):
|
||||||
|
"""
|
||||||
|
Represents a console call, for hold back when spinner is active
|
||||||
|
:param function:
|
||||||
|
:param args:
|
||||||
|
"""
|
||||||
self._func = function
|
self._func = function
|
||||||
self._args = args
|
self._args = args
|
||||||
|
|
||||||
|
@ -12,6 +12,12 @@ from cpl.console.foreground_color_enum import ForegroundColorEnum
|
|||||||
class SpinnerThread(threading.Thread):
|
class SpinnerThread(threading.Thread):
|
||||||
|
|
||||||
def __init__(self, msg_len: int, foreground_color: ForegroundColorEnum, background_color: BackgroundColorEnum):
|
def __init__(self, msg_len: int, foreground_color: ForegroundColorEnum, background_color: BackgroundColorEnum):
|
||||||
|
"""
|
||||||
|
Thread to show spinner in terminal
|
||||||
|
:param msg_len:
|
||||||
|
:param foreground_color:
|
||||||
|
:param background_color:
|
||||||
|
"""
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
|
|
||||||
self._msg_len = msg_len
|
self._msg_len = msg_len
|
||||||
@ -22,11 +28,19 @@ class SpinnerThread(threading.Thread):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _spinner():
|
def _spinner():
|
||||||
|
"""
|
||||||
|
Selects active spinner char
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
while True:
|
while True:
|
||||||
for cursor in '|/-\\':
|
for cursor in '|/-\\':
|
||||||
yield cursor
|
yield cursor
|
||||||
|
|
||||||
def _get_color_args(self) -> list[str]:
|
def _get_color_args(self) -> list[str]:
|
||||||
|
"""
|
||||||
|
Creates color arguments
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
color_args = []
|
color_args = []
|
||||||
if self._foreground_color is not None:
|
if self._foreground_color is not None:
|
||||||
color_args.append(str(self._foreground_color.value))
|
color_args.append(str(self._foreground_color.value))
|
||||||
@ -37,6 +51,10 @@ class SpinnerThread(threading.Thread):
|
|||||||
return color_args
|
return color_args
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
|
"""
|
||||||
|
Entry point ohf thread, shows the spinner
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
rows, columns = os.popen('stty size', 'r').read().split()
|
rows, columns = os.popen('stty size', 'r').read().split()
|
||||||
end_msg = 'done'
|
end_msg = 'done'
|
||||||
columns = int(columns) - self._msg_len - len(end_msg)
|
columns = int(columns) - self._msg_len - len(end_msg)
|
||||||
@ -55,5 +73,9 @@ class SpinnerThread(threading.Thread):
|
|||||||
print(colored(end_msg, *self._get_color_args()), end='')
|
print(colored(end_msg, *self._get_color_args()), end='')
|
||||||
|
|
||||||
def stop_spinning(self):
|
def stop_spinning(self):
|
||||||
|
"""
|
||||||
|
Stops the spinner
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
self._is_spinning = False
|
self._is_spinning = False
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
Loading…
Reference in New Issue
Block a user