Coverage for codexa/core/output.py: 89%

18 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-08-10 07:53 +0000

1import logging 

2 

3import click 

4from colorama import Fore, Style 

5 

6 

7class ColorHandler(logging.StreamHandler): 

8 def emit(self, record: logging.LogRecord) -> None: 

9 colors = { 

10 logging.DEBUG: Fore.CYAN, 

11 logging.INFO: Fore.GREEN, 

12 logging.WARNING: Fore.YELLOW, 

13 logging.ERROR: Fore.RED, 

14 logging.CRITICAL: Fore.RED, 

15 } 

16 color = colors.get(record.levelno, Fore.WHITE) 

17 original_levelname = record.levelname 

18 try: 

19 # Replace levelname with colored version 

20 record.levelname = f"{color}{record.levelname}{Style.RESET_ALL}" 

21 super().emit(record) 

22 finally: 

23 # Restore original levelname to avoid side effects 

24 record.levelname = original_levelname 

25 

26 

27def print_error(message: str) -> None: 

28 """ 

29 Print an error message in red. 

30 

31 Args: 

32 message (str): The error message to print. 

33 """ 

34 click.echo(f"{Fore.RED}{Style.BRIGHT}{message}{Style.RESET_ALL}") 

35 

36 

37def print_warning(message: str) -> None: 

38 """ 

39 Print a warning message in yellow. 

40 

41 Args: 

42 message (str): The warning message to print. 

43 """ 

44 click.echo(f"{Fore.YELLOW}{Style.BRIGHT}{message}{Style.RESET_ALL}") 

45 

46 

47def print_success(message: str) -> None: 

48 """ 

49 Print a success message in green. 

50 

51 Args: 

52 message (str): The success message to print. 

53 """ 

54 click.echo(f"{Fore.GREEN}{Style.BRIGHT}{message}{Style.RESET_ALL}")