From d9d20030870aaec5ef0f369c2fea6b8de163d4ce Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 10 Mar 2021 17:04:28 +0100 Subject: [PATCH] Improved console --- src/cpl/console/console.py | 15 ++++++++++++--- src/cpl/console/spinner_thread.py | 24 ++++++++++++++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/cpl/console/console.py b/src/cpl/console/console.py index 8aad8af0..6d065170 100644 --- a/src/cpl/console/console.py +++ b/src/cpl/console/console.py @@ -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) diff --git a/src/cpl/console/spinner_thread.py b/src/cpl/console/spinner_thread.py index 27dd481d..6d4be6dd 100644 --- a/src/cpl/console/spinner_thread.py +++ b/src/cpl/console/spinner_thread.py @@ -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