Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from enum import Enum
- from typing import Optional, Callable, TypeVar, Generic
- class ConsoleColors:
- BLACK = "\033[30m"
- RED = "\033[31m"
- GREEN = "\033[32m"
- YELLOW = "\033[33m"
- BLUE = "\033[34m"
- PURPLE = "\033[35m"
- CYAN = "\033[36m"
- WHITE = "\033[37m"
- RESET = "\033[0m"
- T = TypeVar('T')
- class Printer:
- def __init__(self):
- super().__init__()
- @staticmethod
- def write(self, object, color: ConsoleColors) -> None:
- print(color + object + ConsoleColors.RESET, end='')
- @staticmethod
- def writeln(self, object, color: ConsoleColors):
- print(color + object + ConsoleColors.RESET)
- @staticmethod
- def get_string_from_console(
- self,
- prompt: str,
- color: ConsoleColors,
- validator: Optional[Callable[[str], bool]] = None,
- errorMessage: Optional[str] = None,
- errorColor: ConsoleColors = ConsoleColors.RED
- ):
- while True:
- self.write(prompt, color)
- input_str = input()
- if input_str is not None and (validator is None or validator(input_str)):
- return input_str
- self.write(errorMessage, errorColor)
- @staticmethod
- def get_from_console(
- self,
- prompt: str,
- color: ConsoleColors,
- parser: Callable[[str], T],
- validator: Optional[Callable[[T], bool]] = None,
- errorMessage: Optional[str] = None,
- errorColor: ConsoleColors = ConsoleColors.RED
- ) -> T:
- while True:
- self.write(prompt, color)
- try:
- value = parser(input())
- if validator is None or validator(value):
- return value
- else:
- self.writeln(errorMessage, errorColor)
- except ValueError:
- self.writeln(errorMessage, errorColor)
Advertisement
Add Comment
Please, Sign In to add comment