diff --git a/src/zenlib/util/colorize.py b/src/zenlib/util/colorize.py index 31bc23a..5c880c5 100644 --- a/src/zenlib/util/colorize.py +++ b/src/zenlib/util/colorize.py @@ -1,10 +1,23 @@ -__version__ = "0.2.0" +__version__ = "0.3.0" +from dataclasses import dataclass from enum import Enum ANSI_START = "\x1b[" +class Formatters(Enum): + BOLD = 1 + DIM = 2 + ITALIC = 3 + UNDERLINE = 4 + BLINK = 5 + FAST_BLINK = 6 + REVERSE = 7 + HIDDEN = 8 + STRIKETHROUGH = 9 + + class Colors(Enum): RED = 31 GREEN = 32 @@ -15,20 +28,35 @@ class Colors(Enum): WHITE = 37 +@dataclass class ANSICode: - def __init__(self, code, bright=False, bold=False): - self.code = code - self.bright = bright - self.bold = bold + code: int + bright: bool = False + background: bool = False + bold: bool = False + dim: bool = False + italic: bool = False + underline: bool = False + blink: bool = False + fast_blink: bool = False + reverse: bool = False + hidden: bool = False + strikethrough: bool = False + + @property + def formatters(self): + return [str(formatter.value) for formatter in Formatters if getattr(self, formatter.name.lower())] def __str__(self): color_code = self.code color_code += 60 if self.bright else 0 - bold_str = ";1" if self.bold else "" - return f"{ANSI_START}{color_code}{bold_str}m" + color_code += 10 if self.background else 0 + if formatters := self.formatters: + color_code = f"{color_code};{';'.join(formatters)}" + return f"{ANSI_START}{color_code}m" -def colorize(text: str, color="white", bright=False, bold=False) -> str: +def colorize(text: str, color="white", *args, **kwargs) -> str: try: color_code = Colors[color.upper()].value except (KeyError, AttributeError): @@ -38,4 +66,4 @@ def colorize(text: str, color="white", bright=False, bold=False) -> str: raise ValueError(f"Invalid color: {color}") color_code = int(color) - return f"{ANSICode(color_code, bright, bold)}{text}{ANSICode(0)}" + return f"{ANSICode(color_code, *args, **kwargs)}{text}{ANSICode(0)}"