-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstyled_console_text.py
More file actions
93 lines (75 loc) · 2.48 KB
/
styled_console_text.py
File metadata and controls
93 lines (75 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
Functions for printing formatted text to the console
"""
import ctypes
import logging
import os
logger = logging.getLogger(__name__)
class Styles:
"""Color and formatting styles"""
# Text Effects
RESET = "\033[0m"
BOLD = "\033[1m"
DIM = "\033[2m"
ITALIC = "\x1B[3m"
UNDERLINED = "\033[4m"
BLINK = "\033[5m"
REVERSE = "\033[7m"
STRIKETHROUGH = "\033[9m"
# Standard Colors
BLACK = "\033[30m"
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
PURPLE = "\033[35m"
CYAN = "\033[36m"
WHITE = "\033[37m"
# Bright Colors (Your original list mostly used these)
GRAY = "\033[90m"
BRIGHT_RED = "\033[91m"
BRIGHT_GREEN = "\033[92m"
BRIGHT_YELLOW = "\033[93m"
BRIGHT_BLUE = "\033[94m"
BRIGHT_PURPLE = "\033[95m"
BRIGHT_CYAN = "\033[96m"
BRIGHT_WHITE = "\033[97m"
# Backgrounds
BLACK_BG = "\033[40m"
RED_BG = "\033[41m"
GREEN_BG = "\033[42m"
YELLOW_BG = "\033[43m"
BLUE_BG = "\033[44m"
PURPLE_BG = "\033[45m"
CYAN_BG = "\033[46m"
WHITE_BG = "\033[47m"
@classmethod
def preview_styles(cls):
"""Preview all available styles, skipping methods and internal attributes."""
# Get all attributes that are strings and don't start with underscore
style_names = [
name for name in dir(cls)
if not name.startswith("_") and isinstance(getattr(cls, name), str)
]
for name in sorted(style_names):
print(f"{name.ljust(15)}: {getattr(cls, name)}ABCabc#@!?0123{cls.RESET}")
class ColorFormatter(logging.Formatter):
"""Adds colors to specific keywords for console output only."""
def format(self, record):
message = super().format(record)
if "PASS" in message:
message = message.replace("PASS", f"{Styles.GREEN_BG}{Styles.BOLD}PASS{Styles.RESET}")
elif "FAIL" in message:
message = message.replace("FAIL", f"{Styles.RED_BG}{Styles.BOLD}FAIL{Styles.RESET}")
return message
def enable_windows_ansi():
"""Enables ANSI escape sequences in the Windows command prompt."""
kernel32 = ctypes.windll.kernel32
# ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
# 7 is the standard handle for STDOUT
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
if os.name == "nt":
enable_windows_ansi()
# Example usage:
# console_handler.setFormatter(ColorFormatter(message_format, datefmt=date_format))
Styles.preview_styles()