Skip to main content

""" CODITECT Framework - Terminal Colors Module

Shared terminal color constants for consistent output formatting across all scripts. This module consolidates 36+ duplicate Colors class definitions into a single source.

Usage: from scripts.core.colors import Colors # or for standalone scripts: from colors import Colors

print(f"{Colors.GREEN}Success{Colors.RESET}")
print(f"{Colors.BOLD}{Colors.RED}Error{Colors.RESET}")

ADR Reference: Technical debt identified during ADR-118 duplicate check (2026-01-26) """

all = ['Colors', 'colorize', 'print_color']

class Colors: """ ANSI escape codes for terminal text formatting.

Standard colors, formatting options, and backward-compatible aliases
for consistent terminal output across all CODITECT scripts.
"""

# ==========================================================================
# Standard Colors (Bright/High Intensity)
# ==========================================================================
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
BLUE = '\033[94m'
MAGENTA = '\033[95m'
CYAN = '\033[96m'
WHITE = '\033[97m'

# ==========================================================================
# Standard Colors (Normal Intensity)
# ==========================================================================
DARK_RED = '\033[31m'
DARK_GREEN = '\033[32m'
DARK_YELLOW = '\033[33m'
DARK_BLUE = '\033[34m'
DARK_MAGENTA = '\033[35m'
DARK_CYAN = '\033[36m'
GRAY = '\033[90m'

# ==========================================================================
# Text Formatting
# ==========================================================================
BOLD = '\033[1m'
DIM = '\033[2m'
ITALIC = '\033[3m'
UNDERLINE = '\033[4m'
BLINK = '\033[5m'
REVERSE = '\033[7m'
STRIKETHROUGH = '\033[9m'

# ==========================================================================
# Reset (Canonical Name)
# ==========================================================================
RESET = '\033[0m'

# ==========================================================================
# Backward Compatibility Aliases
# These maintain compatibility with existing scripts that use different names
# ==========================================================================

# Reset aliases (found in 32 files with 3 different names)
END = RESET # Used in 7 files
ENDC = RESET # Used in 4 files
NC = RESET # "No Color" - common in shell scripts

# Semantic aliases (found in various scripts)
WARNING = YELLOW # Used in 3 files
FAIL = RED # Used in 3 files
ERROR = RED # Common semantic name
SUCCESS = GREEN # Common semantic name
INFO = BLUE # Common semantic name
HEADER = MAGENTA # Used in 6 files

# ==========================================================================
# Class Methods for Convenience
# ==========================================================================

@classmethod
def disable(cls):
"""Disable all colors (for non-TTY output or --no-color flag)."""
for attr in dir(cls):
if attr.isupper() and not attr.startswith('_'):
setattr(cls, attr, '')

@classmethod
def is_tty(cls) -> bool:
"""Check if stdout is a TTY (supports colors)."""
import sys
return hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()

def colorize(text: str, color: str, bold: bool = False) -> str: """ Apply color formatting to text.

Args:
text: The text to colorize
color: Color constant from Colors class (e.g., Colors.GREEN)
bold: Whether to make the text bold

Returns:
Formatted string with ANSI codes

Example:
print(colorize("Success!", Colors.GREEN, bold=True))
"""
prefix = Colors.BOLD if bold else ''
return f"{prefix}{color}{text}{Colors.RESET}"

def print_color(text: str, color: str, bold: bool = False, **kwargs): """ Print colored text to stdout.

Args:
text: The text to print
color: Color constant from Colors class
bold: Whether to make the text bold
**kwargs: Additional arguments passed to print()

Example:
print_color("Error: File not found", Colors.RED, bold=True)
"""
print(colorize(text, color, bold), **kwargs)

==========================================================================

Module-level convenience for direct attribute access

==========================================================================

Allow: from colors import RED, GREEN, RESET

RED = Colors.RED GREEN = Colors.GREEN YELLOW = Colors.YELLOW BLUE = Colors.BLUE MAGENTA = Colors.MAGENTA CYAN = Colors.CYAN WHITE = Colors.WHITE BOLD = Colors.BOLD DIM = Colors.DIM UNDERLINE = Colors.UNDERLINE RESET = Colors.RESET END = Colors.END ENDC = Colors.ENDC