Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced New Public Methods #3

Merged
merged 1 commit into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Version History

- 0.1.0: Initial Release (latest)
- 0.1.1: Introduced New Public Methods (latest)
- 0.1.0: Initial Release
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,37 @@ echo('This is a test error message', mappings=color_mappings)
2) Colorizing a message by matching it with a regex pattern and specifying text foreground color, text background
color, text effect, text case, ignore case, and color match.
3) Colorizing a message by matching it with mappings (utilizing a ColorMapper) and specifying text foreground.
- `get_colorized_message(message: str, text_color: Optional[str] = None, text_background_color: Optional[str] = None, text_effect: Optional[str] = None, text_case: Optional[int] = TextCase.NONE) -> str` -
Generates a colorized message based on the provided inputs.
- `get_colorized_message_by_regex_pattern(message: str, regex_pattern: str, text_color: Optional[str] = None, text_background_color: Optional[str] = None, text_effect: Optional[str] = None, text_case: Optional[int] = TextCase.NONE, color_match: Optional[bool] = False, ignore_case: Optional[bool] = False) -> str` -
Generates a colorized message based on the provided regex pattern and inputs.
- `get_colorized_message_by_mappings(message: str, mappings: Optional[ColorMapper] = None) -> str` - Generates a
colorized message based on the provided mappings.
- `is_colorization_supported() -> bool` - Checks if the current operating system supports colorization. Returns True if
colorization is supported, False otherwise.
- `is_true_color_supported() -> bool` - Verifies whether the true color format is supported by the current operating
system and terminal. Returns True if true color format is supported, False otherwise.

#### Usage

```python
from pycolorecho import is_colorization_supported, is_true_color_supported, get_colorized_message, get_colorized_message_by_regex_pattern, get_colorized_message_by_mappings, TextColor, TextBackgroundColor, TextEffect, ColorMapper

print(is_colorization_supported()) # Prints True if colorization is supported, False otherwise
print(is_true_color_supported()) # Prints True if true color format is supported, False otherwise

# Retrieving colorized message
print(get_colorized_message('This is a test message', text_color=TextColor.RED))

# Retrieving colorized message by regex pattern
print(get_colorized_message_by_regex_pattern('This is a test message', regex_pattern=r'test', text_color=TextColor.RED,
text_background_color=TextBackgroundColor.ACID_GREEN))

# Retrieving colorized message by mappings
color_mappings = ColorMapper()
color_mappings.add_mapping('error', [r'error'], text_color=TextColor.RED, text_effect=TextEffect.UNDERLINE)
print(get_colorized_message_by_mappings('This is a test error message', mappings=color_mappings))
```

### Layer

Expand Down
10 changes: 8 additions & 2 deletions pycolorecho/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"TextCase",
"ColorMapper",
"echo",
"is_colorization_supported",
"is_true_color_supported",
"get_colorized_message",
"get_colorized_message_by_regex_pattern",
"get_colorized_message_by_mappings",
"HEXCodes",
"__author__",
"__description__",
Expand All @@ -19,7 +24,8 @@
Simple Python package for colorized terminal output
"""
__name__ = "pycolorecho"
__version__ = "0.1.0"
__version__ = "0.1.1"

from pycolorecho.__main__ import RESET, Layer, Color, TextBackgroundColor, TextColor, TextEffect, TextCase, \
ColorMapper, echo, HEXCodes
ColorMapper, echo, HEXCodes, is_colorization_supported, is_true_color_supported, get_colorized_message, \
get_colorized_message_by_regex_pattern, get_colorized_message_by_mappings
46 changes: 33 additions & 13 deletions pycolorecho/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
RESET: str = "\033[0m"


def _is_colorization_supported() -> bool:
def is_colorization_supported() -> bool:
"""
Checks if the current operating system supports colorization.
:return: True if colorization is supported, False otherwise.
Expand Down Expand Up @@ -41,7 +41,7 @@ def _is_colorization_supported() -> bool:
os.remove(file_name)


def _is_true_color_supported() -> bool:
def is_true_color_supported() -> bool:
"""
Verifies whether the true color format is supported by the current operating system and terminal.
:return: True if true color format is supported, False otherwise.
Expand Down Expand Up @@ -3147,7 +3147,7 @@ def add_color(cls, name: str, ansi_code: str, true_color: Optional[bool] = True)
Validate.validate_type(true_color, bool, 'true_color should be a boolean.')
Validate.validate_ansi(ansi_code)

if true_color and not _is_true_color_supported():
if true_color and not is_true_color_supported():
raise Warning('True colors are not supported by this terminal.')

code = ansi_code[2:].rstrip('m')
Expand Down Expand Up @@ -5095,7 +5095,7 @@ def add_color(cls, name: str, ansi_code: str, true_color: Optional[bool] = True)
Validate.validate_type(true_color, bool, 'true_color should be a boolean.')
Validate.validate_ansi(ansi_code)

if true_color and not _is_true_color_supported():
if true_color and not is_true_color_supported():
raise Warning('True colors are not supported by this terminal.')

code = ansi_code[2:].rstrip('m')
Expand Down Expand Up @@ -5669,7 +5669,7 @@ def _get_colorize_sequence(
return colorize_sequence


def _get_colorized_message(
def get_colorized_message(
message: str,
text_color: Optional[str] = None,
text_background_color: Optional[str] = None,
Expand All @@ -5691,6 +5691,12 @@ def _get_colorized_message(
:return: The generated colorized message.
:rtype: str
"""
Validate.validate_type(message, str, 'message should be a string.')
Validate.validate_type(text_color, Union[str, None], 'text_color should be a string.')
Validate.validate_type(text_background_color, Union[str, None], 'text_background_color should be a string.')
Validate.validate_type(text_effect, Union[str, None], 'text_effect should be a string.')
Validate.validate_type(text_case, Union[int, None], 'text_case should be an integer.')

if text_color is None and text_background_color is None and text_effect is None:
return f'{TextCase.convert_text(message, text_case)}'

Expand All @@ -5702,9 +5708,9 @@ def _get_colorized_message(
)


def _get_colorized_message_by_regex_pattern(
def get_colorized_message_by_regex_pattern(
message: str,
regex_pattern: Optional[str] = None,
regex_pattern: str,
text_color: Optional[str] = None,
text_background_color: Optional[str] = None,
text_effect: Optional[str] = None,
Expand Down Expand Up @@ -5735,6 +5741,15 @@ def _get_colorized_message_by_regex_pattern(
:return: The generated colorized message.
:rtype: str
"""
Validate.validate_type(message, str, 'message should be a string.')
Validate.validate_type(regex_pattern, str, 'regex_pattern should be a string.')
Validate.validate_type(text_color, Union[str, None], 'text_color should be a string.')
Validate.validate_type(text_background_color, Union[str, None], 'text_background_color should be a string.')
Validate.validate_type(text_effect, Union[str, None], 'text_effect should be a string.')
Validate.validate_type(text_case, Union[int, None], 'text_case should be an integer.')
Validate.validate_type(color_match, Union[bool, None], 'color_match should be a boolean.')
Validate.validate_type(ignore_case, Union[bool, None], 'ignore_case should be a boolean.')

colorized_message = message
colorize_sequence = _get_colorize_sequence(text_color, text_background_color, text_effect)

Expand Down Expand Up @@ -5779,9 +5794,9 @@ def _get_colorized_message_by_regex_pattern(
return colorized_message


def _get_colorized_message_by_mappings(
def get_colorized_message_by_mappings(
message: str,
mappings: Optional[ColorMapper] = None
mappings: ColorMapper
) -> str:
"""
Generates a colorized message based on the provided mappings.
Expand All @@ -5792,6 +5807,9 @@ def _get_colorized_message_by_mappings(
:return: The generated colorized message.
:rtype: str
"""
Validate.validate_type(message, str, 'message should be a string.')
Validate.validate_type(mappings, ColorMapper, 'mappings should be of ColorMapper type.')

colorized_message = message
for key, value in mappings.get_mappings().items():
keywords = value.get('keywords', [])
Expand Down Expand Up @@ -5906,19 +5924,21 @@ def echo(
Validate.validate_type(text_effect, Union[str, None], 'text_effect should be a string.')
Validate.validate_type(text_case, Union[int, None], 'text_case should be an integer.')

Validate.validate_type(message, str, 'message should be a string.')

colorized_message = message

if _is_colorization_supported():
if is_colorization_supported():
if mappings is not None:
colorized_message = _get_colorized_message_by_mappings(colorized_message, mappings)
colorized_message = get_colorized_message_by_mappings(colorized_message, mappings)
elif regex_pattern is not None:
colorized_message = _get_colorized_message_by_regex_pattern(
colorized_message = get_colorized_message_by_regex_pattern(
colorized_message, regex_pattern,
text_color, text_background_color, text_effect, text_case,
color_match, ignore_case
)
else:
colorized_message = _get_colorized_message(
colorized_message = get_colorized_message(
colorized_message, text_color, text_background_color, text_effect, text_case
)

Expand Down
34 changes: 17 additions & 17 deletions tests/test_pycolorecho.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import unittest

from pycolorecho import RESET, TextColor, TextBackgroundColor, TextEffect, TextCase, ColorMapper
from pycolorecho.__main__ import _get_colorize_sequence, _get_colorized_message, \
_get_colorized_message_by_regex_pattern, _get_colorized_message_by_mappings
from pycolorecho import RESET, TextColor, TextBackgroundColor, TextEffect, TextCase, ColorMapper, \
get_colorized_message, get_colorized_message_by_regex_pattern, get_colorized_message_by_mappings
from pycolorecho.__main__ import _get_colorize_sequence


class TestPyColorEcho(unittest.TestCase):
Expand Down Expand Up @@ -57,7 +57,7 @@ def test_get_colorized_message(self):
)
self.assertEqual(
expected_value,
_get_colorized_message(
get_colorized_message(
message='This is a test message',
text_color=TextColor.RED,
text_background_color=TextBackgroundColor.ACID_GREEN,
Expand All @@ -71,7 +71,7 @@ def test_get_colorized_message_none(self):
expected_value = 'This is a test message'
self.assertEqual(
expected_value,
_get_colorized_message('This is a test message')
get_colorized_message('This is a test message')
)

def test_get_colorized_message_text_color(self):
Expand All @@ -81,7 +81,7 @@ def test_get_colorized_message_text_color(self):
)
self.assertEqual(
expected_value,
_get_colorized_message(
get_colorized_message(
message='This is a test message',
text_color=TextColor.RED
)
Expand All @@ -94,7 +94,7 @@ def test_get_colorized_message_text_background_color(self):
)
self.assertEqual(
expected_value,
_get_colorized_message(
get_colorized_message(
message='This is a test message',
text_background_color=TextBackgroundColor.ACID_GREEN
)
Expand All @@ -107,7 +107,7 @@ def test_get_colorized_message_text_effect(self):
)
self.assertEqual(
expected_value,
_get_colorized_message(
get_colorized_message(
message='This is a test message',
text_effect=TextEffect.BOLD
)
Expand All @@ -118,7 +118,7 @@ def test_get_colorized_message_text_case(self):
expected_value = 'this-is-a-test-message'
self.assertEqual(
expected_value,
_get_colorized_message(
get_colorized_message(
message='This is a test message',
text_case=TextCase.KEBAB_CASE
)
Expand All @@ -132,7 +132,7 @@ def test_get_colorized_message_by_regex_pattern_1(self):
)
self.assertEqual(
expected_value,
_get_colorized_message_by_regex_pattern(
get_colorized_message_by_regex_pattern(
message='This is a test message',
regex_pattern=r'test',
text_color=TextColor.RED,
Expand All @@ -150,7 +150,7 @@ def test_get_colorized_message_by_regex_pattern_2(self):
)
self.assertEqual(
expected_value,
_get_colorized_message_by_regex_pattern(
get_colorized_message_by_regex_pattern(
message='This is a test message',
regex_pattern=r'TEST',
text_color=TextColor.RED,
Expand All @@ -169,7 +169,7 @@ def test_get_colorized_message_by_regex_pattern_3(self):
)
self.assertEqual(
expected_value,
_get_colorized_message_by_regex_pattern(
get_colorized_message_by_regex_pattern(
message='This is a test message',
regex_pattern=r'test',
text_color=TextColor.RED,
Expand All @@ -185,7 +185,7 @@ def test_get_colorized_message_by_regex_pattern_4(self):
expected_value = 'This is a test message'
self.assertEqual(
expected_value,
_get_colorized_message_by_regex_pattern(
get_colorized_message_by_regex_pattern(
message='This is a test message',
regex_pattern=r'nottest',
text_color=TextColor.RED,
Expand Down Expand Up @@ -213,7 +213,7 @@ def test_get_colorized_message_by_mappings_1(self):
)
self.assertEqual(
expected_value,
_get_colorized_message_by_mappings(
get_colorized_message_by_mappings(
message='This is a test message',
mappings=colorization
)
Expand All @@ -237,7 +237,7 @@ def test_get_colorized_message_by_mappings_2(self):
)
self.assertEqual(
expected_value,
_get_colorized_message_by_mappings(
get_colorized_message_by_mappings(
message='This is a test message',
mappings=colorization
)
Expand All @@ -261,7 +261,7 @@ def test_get_colorized_message_by_mappings_3(self):
)
self.assertEqual(
expected_value,
_get_colorized_message_by_mappings(
get_colorized_message_by_mappings(
message='This is a test message',
mappings=colorization
)
Expand All @@ -282,7 +282,7 @@ def test_get_colorized_message_by_mappings_4(self):
)
self.assertEqual(
expected_value,
_get_colorized_message_by_mappings(
get_colorized_message_by_mappings(
message='This is a test message',
mappings=colorization
)
Expand Down
Loading