Last active
October 2, 2025 06:28
-
-
Save TheBigRoomXXL/09878e8112c56d865c54f552b61b9013 to your computer and use it in GitHub Desktop.
Simple colorfull print in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| quiet = False | |
| class T: | |
| """Terminal Pretty Printing""" | |
| OK = "\033[92m" | |
| WARN = "\033[93m" | |
| ERR = "\033[91m" | |
| END = "\033[0m" | |
| ANSI_REGEX = compile(r'\033\[[0-9;]*m') | |
| @staticmethod | |
| def printq(*args, **kwargs): | |
| if not quiet: | |
| return T.print(*args, **kwargs) | |
| @staticmethod | |
| def print(*args, **kwargs): | |
| out = kwargs.get('file', stdout) | |
| # Remove ANSI escape sequence if not in terminal (redirection for example) | |
| if not isatty(out.fileno()): | |
| args = [T.ANSI_REGEX.sub('', str(arg)) if isinstance(arg,str) else arg for arg in args] | |
| print(*args, **kwargs) | |
| # Examples | |
| T.printq(c.OK, "This is a success message", c.END) | |
| T.printq(c.WARN, "This is a warning", c.END) | |
| T.print(c.ERR, "This is an error", c.END) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment