46 lines
1.0 KiB
Python
46 lines
1.0 KiB
Python
from typing import Any, Optional
|
|
|
|
|
|
class Color:
|
|
BLACK = 0
|
|
RED = 1
|
|
GREEN = 2
|
|
YELLOW = 3
|
|
BLUE = 4
|
|
MAGENTA = 5
|
|
CYAN = 6
|
|
WHITE = 7
|
|
|
|
BRIGHT_BLACK = 8
|
|
BRIGHT_RED = 9
|
|
BRIGHT_GREEN = 10
|
|
BRIGHT_YELLOW = 11
|
|
BRIGHT_BLUE = 12
|
|
BRIGHT_MAGENTA = 13
|
|
BRIGHT_CYAN = 14
|
|
BRIGHT_WHITE = 15
|
|
|
|
def __init__(self, foreground: Optional[int],
|
|
background: Optional[int] = None) -> None:
|
|
self.foreground = foreground
|
|
self.background = background
|
|
|
|
def print(self, *args: Any, end: str = '\n') -> None:
|
|
print(self.format(*args), end=end)
|
|
|
|
def format(self, *args: Any) -> str:
|
|
colors = ['0']
|
|
|
|
if self.foreground is not None:
|
|
colors.append(f"38;5;{self.foreground}")
|
|
|
|
if self.background is not None:
|
|
colors.append(f"48;5;{self.background}")
|
|
|
|
color_str = ';'.join(colors)
|
|
text = ' '.join(str(a) for a in args)
|
|
if not text:
|
|
return ''
|
|
|
|
return f'\033[{color_str}m{text}\033[0m'
|