Skip to content

Commit

Permalink
Version 1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
croketillo committed Nov 11, 2023
1 parent a85a07b commit cd5bde6
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 52 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

ColorPattern is a Python module designed for enhancing text output in the console by applying color to specific patterns. It offers a flexible way to define patterns and apply different text colors, background colors, styles, and underlines to matching text in the output.

In Version 1.4 can strikethrough

## Installation

You can install ColorPattern using pip:
Expand All @@ -14,31 +16,33 @@ You can install ColorPattern using pip:
Use ```start_color(<patterns>)``` for initialize the color print, and ```end_color()``` for stop colorization.

```python
from colorpattern import *
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)
pattern3 = SetPattern(r'Croketillo', color=Fore.BLACK, back=Back.LIGHTWHITE_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.BLUE)
strike= SetPattern(r'NEW!!!', strikethrough=True)

# 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, strike])

# Use the custom print function with colorization
print('Colorpattern v1.3')
print('Colorpattern v1.4')
print('By Croketillo - croketillo@gmail.com')
print('NEW!!! - NOW YOU CAN INCLUDE STRIKETHROUGH IN PATTERNS')

# End colorization and restore the original print function
end_color()
print("\nNORMAL PRINT")
# Now, printing returns to normal

print('-----------------------')
print('Colorpattern v1.3')
print('Colorpattern v1.4')
print('By Croketillo - croketillo@gmail.com')

# You can re-enable colorization with new patterns if necessary
Expand All @@ -60,6 +64,7 @@ def main():

if __name__ == "__main__":
main()

```

## Patterns
Expand Down
18 changes: 11 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ the console by applying color to specific patterns. It offers a flexible
way to define patterns and apply different text colors, background
colors, styles, and underlines to matching text in the output.

In Version 1.4 can strikethrough

Installation
------------

Expand All @@ -16,8 +18,8 @@ You can install ColorPattern using pip:
Usage
-----

Use start_color() for initialize the color print, and end_color() for
stop colorization.
Use ``start_color(<patterns>)`` for initialize the color print, and
``end_color()`` for stop colorization.

.. code:: python
Expand All @@ -27,25 +29,27 @@ stop colorization.
# 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)
pattern3 = SetPattern(r'Croketillo', color=Fore.BLACK, back=Back.LIGHTWHITE_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.BLUE)
strike= SetPattern(r'NEW!!!', strikethrough=True)
# 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, strike])
# Use the custom print function with colorization
print('Colorpattern v1.3')
print('Colorpattern v1.4')
print('By Croketillo - croketillo@gmail.com')
print('NEW!!! - NOW YOU CAN INCLUDE STRIKETHROUGH IN PATTERNS')
# End colorization and restore the original print function
end_color()
print("\nNORMAL PRINT")
# Now, printing returns to normal
print('-----------------------')
print('Colorpattern v1.3')
print('Colorpattern v1.4')
print('By Croketillo - croketillo@gmail.com')
# You can re-enable colorization with new patterns if necessary
Expand Down
Binary file modified colorpattern/__pycache__/colorpattern.cpython-310.pyc
Binary file not shown.
42 changes: 9 additions & 33 deletions colorpattern/colorpattern.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,26 @@
"""
This file is (or part of) COLORPATTERN v1.3
Copyright 2023- Croketillo <croketillo@gmail.com> https://github.com/croketillo
DESCIPTION:
COLORPATTERN - Effortless console text colorization based on user-defined patterns
in Python.
LICENSE - GNU GPL-3
This software is protected by the GNU General Public License version 3 (GNU GPL-3).
You are free to use, modify, and redistribute this software in accordance with the
terms of the GNU GPL-3. You can find a copy of the license at the following link:
https://www.gnu.org/licenses/gpl-3.0.html.
This software is provided as-is, without any warranties, whether express or implied.
Under no circumstances shall the authors or copyright holders be liable for any claims,
damages, or liabilities arising in connection with the use of this software.
If you make modifications to this software and redistribute it, you must comply with
the terms of the GNU GPL-3, which includes the obligation to provide the source code
for your modifications. Additionally, any derived software must also be under the
GNU GPL-3.
For more information about the GNU GPL-3 and its terms, please carefully read the full
license or visit https://www.gnu.org/licenses/gpl-3.0.html
"""

import re
from colorama import Fore, Style, Back
import builtins

class SetPattern:
def __init__(self, pattern, color=None, back=None, style=None, underline=False):
def __init__(self, pattern, color=None, back=None, style=None, underline=False, strikethrough=False):
# Compile the regular expression pattern
self.pattern = re.compile(pattern)
# Set default values for color, background, style, and underline
self.color = color if color is not None else Fore.RESET
self.back = back if back is not None else Back.RESET
self.style = style if style is not None else Style.RESET_ALL
self.underline = underline
self.strikethrough = strikethrough

def colorize_text(self, text, stop=None):
# Apply color, background, style, and underline to matched text
if self.underline:
# Apply color, background, style, underline, and strikethrough to matched text
if self.underline and self.strikethrough:
return self.pattern.sub(lambda match: f"{self.style}{self.color}{self.back}\033[4m\033[9m{match.group()}\033[0m{Style.RESET_ALL}", text)
elif 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)
elif self.strikethrough:
return self.pattern.sub(lambda match: f"{self.style}{self.color}{self.back}\033[9m{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)

Expand Down Expand Up @@ -75,4 +51,4 @@ def end_color():
builtins.print = builtins.__original_print__

# Save the original print function
builtins.__original_print__ = builtins.print
builtins.__original_print__ = builtins.print
Binary file added dist/colorpattern-1.4.0-py3-none-any.whl
Binary file not shown.
Binary file added dist/colorpattern-1.4.0.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def read(file_name=None, is_encoding=True, ignore_raises=False):

setup(
name='colorpattern',
version='1.3',
version='1.4.0',
author='croketillo',
author_email='croketillo@gmail.com',
license=read("LICENSE", is_encoding=False, ignore_raises=True),
Expand Down
12 changes: 7 additions & 5 deletions test/colorpattern_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ 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)
pattern3 = SetPattern(r'Croketillo', color=Fore.BLACK, back=Back.LIGHTWHITE_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.BLUE)
strike= SetPattern(r'NEW!!!', strikethrough=True)

# 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, strike])

# Use the custom print function with colorization
print('Colorpattern v1.3')
print('Colorpattern v1.4')
print('By Croketillo - croketillo@gmail.com')
print('NEW!!! - NOW YOU CAN INCLUDE STRIKETHROUGH IN PATTERNS')

# End colorization and restore the original print function
end_color()
print("\nNORMAL PRINT")
# Now, printing returns to normal

print('-----------------------')
print('Colorpattern v1.3')
print('Colorpattern v1.4')
print('By Croketillo - croketillo@gmail.com')

# You can re-enable colorization with new patterns if necessary
Expand Down

0 comments on commit cd5bde6

Please sign in to comment.