diff --git a/README.md b/README.md index 162b9f0..b6a36f0 100644 --- a/README.md +++ b/README.md @@ -11,19 +11,55 @@ You can install ColorPattern using pip: ## Usage -```python -from colorpattern import SetPattern, start_color - -# Define color patterns -pattern1 = SetPattern(r'\d+', color='green', back='black', style='bright', underline=True) -pattern2 = SetPattern(r'error', color='red', back='yellow', style='dim', underline=False) -pattern3 = SetPattern(r'pattern', back='blue', style='reset_all', underline=True) +Use start_color() for initialize the color print, and end_color() for stop colorization. -# Initialize color for patterns -start_color([pattern1, pattern2, pattern3]) - -# Your code with colorized output -print("123 error 456 pattern") +```python +from colorpattern.colorpattern import * + +def main(): + # Define your color patterns + pattern1 = SetPattern(r'\d+', color=Fore.GREEN) + pattern2 = SetPattern(r'Colorpattern', color=Fore.LIGHTRED_EX, underline=True) + pattern3 = SetPattern(r'Croketillo', color=Fore.RED, back=Back.LIGHTYELLOW_EX, style=Style.BRIGHT) + email = SetPattern(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b', color=Fore.LIGHTCYAN_EX) + + # Initialize colorization and get the original print function and applied patterns + print("\nSTART COLORIZED PRINT") + print('-----------------------') + start_color([pattern1, pattern2, pattern3, email]) + + # Use the custom print function with colorization + print('Colorpattern v1.3') + print('By Croketillo - croketillo@gmail.com') + + # End colorization and restore the original print function + end_color() + print("\nNORMAL PRINT") + # Now, printing returns to normal + + print('-----------------------') + print('Colorpattern v1.3') + print('By Croketillo - croketillo@gmail.com') + + # You can re-enable colorization with new patterns if necessary + new_pattern = SetPattern(r'new pattern', color=Fore.LIGHTCYAN_EX) + + # Use the custom print function with new patterns + print("\nSTART COLORIZED PRINT AGAIN") + start_color([pattern1, new_pattern]) + + print('-----------------------') + print('This is a new pattern. 123456') + + # End colorization and restore the original print function + end_color() + print("\nNORMAL PRINT AGAIN") + # Now, printing returns to normal even with the new patterns + print('-----------------------') + print('This is a normal message again.') + +if __name__ == "__main__": + main() ``` ## Patterns diff --git a/README.rst b/README.rst index fb36b17..f7b69b9 100644 --- a/README.rst +++ b/README.rst @@ -16,20 +16,57 @@ You can install ColorPattern using pip: Usage ----- +Use start_color() for initialize the color print, and end_color() for +stop colorization. + .. code:: python - from colorpattern import SetPattern, start_color + from colorpattern.colorpattern import * + + def main(): + # Define your color patterns + pattern1 = SetPattern(r'\d+', color=Fore.GREEN) + pattern2 = SetPattern(r'Colorpattern', color=Fore.LIGHTRED_EX, underline=True) + pattern3 = SetPattern(r'Croketillo', color=Fore.RED, back=Back.LIGHTYELLOW_EX, style=Style.BRIGHT) + email = SetPattern(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b', color=Fore.LIGHTCYAN_EX) + + # Initialize colorization and get the original print function and applied patterns + print("\nSTART COLORIZED PRINT") + print('-----------------------') + start_color([pattern1, pattern2, pattern3, email]) + + # Use the custom print function with colorization + print('Colorpattern v1.3') + print('By Croketillo - croketillo@gmail.com') + + # End colorization and restore the original print function + end_color() + print("\nNORMAL PRINT") + # Now, printing returns to normal + + print('-----------------------') + print('Colorpattern v1.3') + print('By Croketillo - croketillo@gmail.com') + + # You can re-enable colorization with new patterns if necessary + new_pattern = SetPattern(r'new pattern', color=Fore.LIGHTCYAN_EX) + + # Use the custom print function with new patterns + print("\nSTART COLORIZED PRINT AGAIN") + start_color([pattern1, new_pattern]) - # Define color patterns - pattern1 = SetPattern(r'\d+', color='green', back='black', style='bright', underline=True) - pattern2 = SetPattern(r'error', color='red', back='yellow', style='dim', underline=False) - pattern3 = SetPattern(r'pattern', back='blue', style='reset_all', underline=True) + print('-----------------------') + print('This is a new pattern. 123456') - # Initialize color for patterns - start_color([pattern1, pattern2, pattern3]) + # End colorization and restore the original print function + end_color() + print("\nNORMAL PRINT AGAIN") + # Now, printing returns to normal even with the new patterns + print('-----------------------') + print('This is a normal message again.') - # Your code with colorized output - print("123 error 456 pattern") + if __name__ == "__main__": + main() Patterns -------- diff --git a/colorpattern/__pycache__/__init__.cpython-310.pyc b/colorpattern/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..443c5ae Binary files /dev/null and b/colorpattern/__pycache__/__init__.cpython-310.pyc differ diff --git a/colorpattern/__pycache__/colorpattern.cpython-310.pyc b/colorpattern/__pycache__/colorpattern.cpython-310.pyc new file mode 100644 index 0000000..ce5c282 Binary files /dev/null and b/colorpattern/__pycache__/colorpattern.cpython-310.pyc differ diff --git a/colorpattern/colorpattern.py b/colorpattern/colorpattern.py index f891331..b474bc3 100644 --- a/colorpattern/colorpattern.py +++ b/colorpattern/colorpattern.py @@ -1,5 +1,5 @@ """ -This file is (or part of) COLORPATTERN v1.2 +This file is (or part of) COLORPATTERN v1.3 Copyright 2023- Croketillo https://github.com/croketillo DESCIPTION: @@ -41,19 +41,20 @@ def __init__(self, pattern, color=None, back=None, style=None, underline=False): self.style = style if style is not None else Style.RESET_ALL self.underline = underline - def colorize_text(self, text): + def colorize_text(self, text, stop=None): # Apply color, background, style, and underline to matched text if self.underline: return self.pattern.sub(lambda match: f"{self.style}{self.color}{self.back}\033[4m{match.group()}\033[0m{Style.RESET_ALL}", text) else: return self.pattern.sub(lambda match: f"{self.style}{self.color}{self.back}{match.group()}{Style.RESET_ALL}", text) + # Function to initialize colorization def start_color(patterns): def custom_print(*args, **kwargs): # Convert print arguments to a string text = " ".join(map(str, args)) - + # Apply colorization to the text for pattern in patterns: text = pattern.colorize_text(text) @@ -64,3 +65,14 @@ def custom_print(*args, **kwargs): # Replace the print function with the custom version original_print = builtins.print builtins.print = custom_print + + return original_print # Return the original print function + + +# Function to end colorization and restore the original print function +def end_color(): + # Restore the original print function + builtins.print = builtins.__original_print__ + +# Save the original print function +builtins.__original_print__ = builtins.print \ No newline at end of file diff --git a/dist/colorpattern-1.3-py3-none-any.whl b/dist/colorpattern-1.3-py3-none-any.whl new file mode 100644 index 0000000..0d38041 Binary files /dev/null and b/dist/colorpattern-1.3-py3-none-any.whl differ diff --git a/dist/colorpattern-1.3.tar.gz b/dist/colorpattern-1.3.tar.gz new file mode 100644 index 0000000..3253266 Binary files /dev/null and b/dist/colorpattern-1.3.tar.gz differ diff --git a/setup.py b/setup.py index a17ad7f..0ee30d6 100644 --- a/setup.py +++ b/setup.py @@ -84,7 +84,7 @@ def read(file_name=None, is_encoding=True, ignore_raises=False): setup( name='colorpattern', - version='1.2', + version='1.3', author='croketillo', author_email='croketillo@gmail.com', License="GNU-GPL 3", @@ -92,7 +92,7 @@ def read(file_name=None, is_encoding=True, ignore_raises=False): install_requires=[ 'colorama', ], - description='Effortless console text colorization based on user-defined patterns in Python."', + description='Effortless console text colorization based on user-defined patterns in Python.', long_description=read("README.rst"), url='https://github.com/croketillo/colorpattern', classifiers=[ diff --git a/test/colorpattern_test.py b/test/colorpattern_test.py index 29ecd81..bfe26b8 100644 --- a/test/colorpattern_test.py +++ b/test/colorpattern_test.py @@ -1,12 +1,46 @@ from colorpattern.colorpattern import * +def main(): + # Define your color patterns + pattern1 = SetPattern(r'\d+', color=Fore.GREEN) + pattern2 = SetPattern(r'Colorpattern', color=Fore.LIGHTRED_EX, underline=True) + pattern3 = SetPattern(r'Croketillo', color=Fore.RED, back=Back.LIGHTYELLOW_EX, style=Style.BRIGHT) + email = SetPattern(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b', color=Fore.LIGHTCYAN_EX) -pattern1 = SetPattern(r'\d+',color=Fore.GREEN) -pattern2 = SetPattern(r'Colorpattern',color=Fore.LIGHTRED_EX, underline=True) -pattern3 = SetPattern(r'Croketillo',color=Fore.RED,back=Back.LIGHTYELLOW_EX, style=Style.BRIGHT) -email = SetPattern(r'\b[A-Za-z0.9._%+-]+@[A-Za-z0.9.-]+\.[A-Z|a-z]{2,7}\b', color=Fore.LIGHTCYAN_EX) + # Initialize colorization and get the original print function and applied patterns + print("\nSTART COLORIZED PRINT") + print('-----------------------') + start_color([pattern1, pattern2, pattern3, email]) -start_color([pattern1,pattern2,pattern3, email]) + # Use the custom print function with colorization + print('Colorpattern v1.3') + print('By Croketillo - croketillo@gmail.com') -print('2133 Colorpattern 432423') -print('By Croketillo - croketillo@gmail.com') + # End colorization and restore the original print function + end_color() + print("\nNORMAL PRINT") + # Now, printing returns to normal + + print('-----------------------') + print('Colorpattern v1.3') + print('By Croketillo - croketillo@gmail.com') + + # You can re-enable colorization with new patterns if necessary + new_pattern = SetPattern(r'new pattern', color=Fore.LIGHTCYAN_EX) + + # Use the custom print function with new patterns + print("\nSTART COLORIZED PRINT AGAIN") + start_color([pattern1, new_pattern]) + + print('-----------------------') + print('This is a new pattern. 123456') + + # End colorization and restore the original print function + end_color() + print("\nNORMAL PRINT AGAIN") + # Now, printing returns to normal even with the new patterns + print('-----------------------') + print('This is a normal message again.') + +if __name__ == "__main__": + main()