Created
September 3, 2024 15:19
-
-
Save DraconicDragon/101338f3b2481a5b24eef6234431a7d6 to your computer and use it in GitHub Desktop.
Stable Diffusion Webui (/Forge) custom colored console output. paste this somewhere at the top of launch.py
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
| import sys | |
| import re | |
| class GreenOutput: | |
| # regex pattern to match URLs, IP and ports | |
| url_pattern = re.compile( | |
| r'((http|https|ftp|ftps):\/\/' # protocol | |
| r'(\d{1,3}\.){3}\d{1,3}' # ip | |
| r'(?::\d+)?' # port number if existing | |
| r'(?:[\/\w.,@?^=%&:/~+#-]*)?' # rest of URL | |
| r'|' # OR | |
| r'((http|https|ftp|ftps):\/\/' # protocol again | |
| r'[\w_-]+(?:\.[\w_-]+)+' # domain name | |
| r'(?::\d+)?' # port number if exists | |
| r'(?:[\/\w.,@?^=%&:/~+#-]*)?))' # rest of URL | |
| ) | |
| def write(self, text): | |
| green_text = "\033[32m" # ANSI code for green | |
| bright_green_text = "\033[92m" # bright green | |
| blue_text = "\033[34m" # blue | |
| bright_blue_text = "\033[94m" # bright blue | |
| reset_text = "\033[0m" # reset to default color | |
| output = "" | |
| # Use regex finditer to iterate over all matches and non-matching parts | |
| last_end = 0 | |
| for match in self.url_pattern.finditer(text): | |
| start, end = match.span() | |
| output += green_text + text[last_end:start] + reset_text | |
| output += blue_text + match.group() + reset_text # url in blue | |
| last_end = end | |
| if "Civitai Helper: " in text: | |
| output += bright_blue_text + text + reset_text | |
| elif "[GPU Setting]" in text or "[Memory Management]" in text or "Total progress: " in text or "%|" in text or "tiled upscale: " in text: | |
| output += bright_green_text + text[last_end:] + reset_text | |
| else: | |
| output += green_text + text[last_end:] + reset_text | |
| # write to console | |
| sys.__stdout__.write(output) | |
| def flush(self): | |
| sys.__stdout__.flush() | |
| def isatty(self): | |
| return sys.__stdout__.isatty() | |
| def __getattr__(self, attr): | |
| return getattr(sys.__stdout__, attr) | |
| class RedOutput: | |
| warning_keywords = ["UserWarning:", "FutureWarning:", "WARNING:", "GradioDeprecationWarning:"] | |
| def write(self, text): | |
| red_text = "\033[91m" # ANSI code for bright red | |
| yellow_text = "\033[33m" # yellow | |
| reset_text = "\033[0m" | |
| if any(keyword in text for keyword in self.warning_keywords): | |
| sys.__stderr__.write(yellow_text + text + reset_text) | |
| else: | |
| sys.__stderr__.write(red_text + text + reset_text) | |
| def flush(self): | |
| sys.__stderr__.flush() | |
| def isatty(self): | |
| return sys.__stderr__.isatty() | |
| def __getattr__(self, attr): | |
| return getattr(sys.__stderr__, attr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment