| """ANSI Color Codes for Terminal Output""" |
|
|
| from enum import Enum |
| from typing import Optional |
|
|
|
|
| class Colors(Enum): |
| """Standard ANSI color codes""" |
|
|
| RESET = "\033[0m" |
| BOLD = "\033[1m" |
| DIM = "\033[2m" |
| ITALIC = "\033[3m" |
| UNDERLINE = "\033[4m" |
|
|
| BLACK = "\033[30m" |
| RED = "\033[31m" |
| GREEN = "\033[32m" |
| YELLOW = "\033[33m" |
| BLUE = "\033[34m" |
| MAGENTA = "\033[35m" |
| CYAN = "\033[36m" |
| WHITE = "\033[37m" |
|
|
| BG_BLACK = "\033[40m" |
| BG_RED = "\033[41m" |
| BG_GREEN = "\033[42m" |
| BG_YELLOW = "\033[43m" |
| BG_BLUE = "\033[44m" |
| BG_MAGENTA = "\033[45m" |
| BG_CYAN = "\033[46m" |
| BG_WHITE = "\033[47m" |
|
|
| BRIGHT_BLACK = "\033[90m" |
| BRIGHT_RED = "\033[91m" |
| BRIGHT_GREEN = "\033[92m" |
| BRIGHT_YELLOW = "\033[93m" |
| BRIGHT_BLUE = "\033[94m" |
| BRIGHT_MAGENTA = "\033[95m" |
| BRIGHT_CYAN = "\033[96m" |
| BRIGHT_WHITE = "\033[97m" |
|
|
|
|
| class ColorStr: |
| """String with color formatting""" |
|
|
| def __init__(self, text: str, color: Optional[str] = None): |
| self.text = text |
| self.color = color or "" |
|
|
| def __str__(self) -> str: |
| if self.color: |
| return f"{self.color}{self.text}{Colors.RESET.value}" |
| return self.text |
|
|
| def __repr__(self) -> str: |
| return f"ColorStr({self.text!r}, {self.color!r})" |
|
|
|
|
| def colorize(text: str, *styles: str) -> str: |
| """Add color/styling to text""" |
| style_codes = [] |
| for style in styles: |
| style = style.upper().replace("_", "-") |
| try: |
| style_codes.append(getattr(Colors, style).value) |
| except AttributeError: |
| pass |
|
|
| return "".join(style_codes) + text + Colors.RESET.value |
|
|
|
|
| def red(text: str) -> str: |
| """Red colored text""" |
| return colorize(text, "RED") |
|
|
|
|
| def green(text: str) -> str: |
| """Green colored text""" |
| return colorize(text, "GREEN") |
|
|
|
|
| def yellow(text: str) -> str: |
| """Yellow colored text""" |
| return colorize(text, "YELLOW") |
|
|
|
|
| def blue(text: str) -> str: |
| """Blue colored text""" |
| return colorize(text, "BLUE") |
|
|
|
|
| def magenta(text: str) -> str: |
| """Magenta colored text""" |
| return colorize(text, "MAGENTA") |
|
|
|
|
| def cyan(text: str) -> str: |
| """Cyan colored text""" |
| return colorize(text, "CYAN") |
|
|
|
|
| def bold(text: str) -> str: |
| """Bold text""" |
| return colorize(text, "BOLD") |
|
|
|
|
| def dim(text: str) -> str: |
| """Dim text""" |
| return colorize(text, "DIM") |
|
|
|
|
| def error(text: str) -> str: |
| """Error styled text (red bold)""" |
| return colorize(text, "BOLD", "RED") |
|
|
|
|
| def success(text: str) -> str: |
| """Success styled text (green bold)""" |
| return colorize(text, "BOLD", "GREEN") |
|
|
|
|
| def warning(text: str) -> str: |
| """Warning styled text (yellow)""" |
| return colorize(text, "YELLOW") |
|
|
|
|
| def info(text: str) -> str: |
| """Info styled text (cyan)""" |
| return colorize(text, "CYAN") |
|
|
|
|
| def code(text: str) -> str: |
| """Code styled text""" |
| return colorize(text, "DIM", "CYAN") |
|
|
|
|
| def header(text: str) -> str: |
| """Header styled text""" |
| return colorize(text, "BOLD", "CYAN") |
|
|
|
|
| def prompt(text: str) -> str: |
| """Prompt styled text (green)""" |
| return colorize(text, "BOLD", "GREEN") |
|
|
|
|
| def clear_line(): |
| """Clear current line in terminal""" |
| print("\033[2K\r", end="") |
|
|
|
|
| def move.cursor_up(lines: int = 1): |
| """Move cursor up""" |
| print(f"\033[{lines}A", end="") |
|
|
|
|
| def move_cursor_down(lines: int = 1): |
| """Move cursor down""" |
| print(f"\033[{lines}B", end="") |
|
|
|
|
| def move_cursor_right(cols: int = 1): |
| """Move cursor right""" |
| print(f"\033[{cols}C", end="") |
|
|
|
|
| def move_cursor_left(cols: int = 1): |
| """Move cursor left""" |
| print(f"\033[{cols}D", end="") |
|
|
|
|
| def save_cursor_position(): |
| """Save cursor position""" |
| print("\033[s", end="") |
|
|
|
|
| def restore_cursor_position(): |
| """Restore cursor position""" |
| print("\033[u", end="") |
|
|
|
|
| def clear_screen(): |
| """Clear entire screen""" |
| print("\033[2J", end="") |
|
|
|
|
| def hide_cursor(): |
| """Hide cursor""" |
| print("\033[?25l", end="") |
|
|
|
|
| def show_cursor(): |
| """Show cursor""" |
| print("\033[?25h", end="") |
|
|