Skip to content

Commit

Permalink
Version 1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
croketillo committed Nov 11, 2023
1 parent ccf8d7c commit ccfcf32
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 33 deletions.
60 changes: 48 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(<patterns>) 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
Expand Down
55 changes: 46 additions & 9 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand Down
Binary file added colorpattern/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file not shown.
18 changes: 15 additions & 3 deletions colorpattern/colorpattern.py
Original file line number Diff line number Diff line change
@@ -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 <croketillo@gmail.com> https://github.com/croketillo
DESCIPTION:
Expand Down Expand Up @@ -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)
Expand All @@ -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
Binary file added dist/colorpattern-1.3-py3-none-any.whl
Binary file not shown.
Binary file added dist/colorpattern-1.3.tar.gz
Binary file not shown.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ 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",
packages=find_packages(),
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=[
Expand Down
48 changes: 41 additions & 7 deletions test/colorpattern_test.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit ccfcf32

Please sign in to comment.