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

Remove colorama dep #85

Merged
merged 1 commit into from
Jan 2, 2025
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
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ repos:
- responses
- tomli; python_version<"3.11"
- setuptools
- colorama

- repo: https://github.com/gitleaks/gitleaks
rev: v8.22.0
Expand Down
80 changes: 40 additions & 40 deletions command_line_assistant/rendering/decorators/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import os
from typing import Optional

from colorama import Back, Fore, Style

from command_line_assistant.rendering.base import BaseDecorator

RESET_ALL = "\033[0m"


class ColorDecorator(BaseDecorator):
"""Decorator for adding foreground and background colors to text using colorama
"""Decorator for adding foreground and background colors to text

Example:
This is an example on how to use this decorator:
Expand All @@ -32,45 +32,45 @@ class ColorDecorator(BaseDecorator):
"""

FOREGROUND_COLORS = {
"black": Fore.BLACK,
"red": Fore.RED,
"green": Fore.GREEN,
"yellow": Fore.YELLOW,
"blue": Fore.BLUE,
"magenta": Fore.MAGENTA,
"cyan": Fore.CYAN,
"white": Fore.WHITE,
"reset": Fore.RESET,
"black": 30,
"red": 31,
"green": 32,
"yellow": 33,
"blue": 34,
"magenta": 35,
"cyan": 36,
"white": 37,
"reset": 39,
# Light variants
"lightblack": Fore.LIGHTBLACK_EX,
"lightred": Fore.LIGHTRED_EX,
"lightgreen": Fore.LIGHTGREEN_EX,
"lightyellow": Fore.LIGHTYELLOW_EX,
"lightblue": Fore.LIGHTBLUE_EX,
"lightmagenta": Fore.LIGHTMAGENTA_EX,
"lightcyan": Fore.LIGHTCYAN_EX,
"lightwhite": Fore.LIGHTWHITE_EX,
"lightblack": 90,
"lightred": 91,
"lightgreen": 92,
"lightyellow": 93,
"lightblue": 94,
"lightmagenta": 95,
"lightcyan": 96,
"lightwhite": 96,
}

BACKGROUND_COLORS = {
"black": Back.BLACK,
"red": Back.RED,
"green": Back.GREEN,
"yellow": Back.YELLOW,
"blue": Back.BLUE,
"magenta": Back.MAGENTA,
"cyan": Back.CYAN,
"white": Back.WHITE,
"reset": Back.RESET,
"black": 40,
"red": 41,
"green": 42,
"yellow": 43,
"blue": 44,
"magenta": 45,
"cyan": 46,
"white": 47,
"reset": 49,
# Light variants
"lightblack": Back.LIGHTBLACK_EX,
"lightred": Back.LIGHTRED_EX,
"lightgreen": Back.LIGHTGREEN_EX,
"lightyellow": Back.LIGHTYELLOW_EX,
"lightblue": Back.LIGHTBLUE_EX,
"lightmagenta": Back.LIGHTMAGENTA_EX,
"lightcyan": Back.LIGHTCYAN_EX,
"lightwhite": Back.LIGHTWHITE_EX,
"lightblack": 100,
"lightred": 101,
"lightgreen": 102,
"lightyellow": 103,
"lightblue": 104,
"lightmagenta": 105,
"lightcyan": 106,
"lightwhite": 107,
}

def __init__(
Expand Down Expand Up @@ -104,7 +104,7 @@ def _get_foreground_color(self, color: str) -> str:
raise ValueError(
f"Invalid foreground color. Choose from: {', '.join(self.FOREGROUND_COLORS.keys())}"
)
return self.FOREGROUND_COLORS[color]
return f"\033[{self.FOREGROUND_COLORS[color]}m"

def _get_background_color(self, color: str) -> str:
"""Get the unicode for the requested color.
Expand All @@ -123,7 +123,7 @@ def _get_background_color(self, color: str) -> str:
raise ValueError(
f"Invalid background color. Choose from: {', '.join(self.BACKGROUND_COLORS.keys())}"
)
return self.BACKGROUND_COLORS[color]
return f"\033[{self.BACKGROUND_COLORS[color]}m"

def decorate(self, text: str) -> str:
"""Decorate the text string and returns it.
Expand All @@ -134,7 +134,7 @@ def decorate(self, text: str) -> str:
Returns:
str: The text itself colored with the requested foreground and (optionally) background color.
"""
formatted_text = f"{self.background}{self.foreground}{text}{Style.RESET_ALL}"
formatted_text = f"{self.background}{self.foreground}{text}{RESET_ALL}"
return formatted_text


Expand Down
18 changes: 9 additions & 9 deletions command_line_assistant/rendering/decorators/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

from typing import Optional

from colorama import Style

from command_line_assistant.rendering.base import BaseDecorator

RESET_ALL = "\033[0m"


class StyleDecorator(BaseDecorator):
"""Decorator class to add font style to the textual string.
Expand All @@ -22,10 +22,10 @@ class StyleDecorator(BaseDecorator):
"""

STYLES = {
"dim": Style.DIM,
"normal": Style.NORMAL,
"bright": Style.BRIGHT,
"reset": Style.RESET_ALL,
"bright": 1,
"dim": 2,
"normal": 22,
"reset": 0,
}

def __init__(self, style: Optional[str] = None) -> None:
Expand All @@ -46,14 +46,14 @@ def _get_style(self, style: str) -> str:
ValueError: In case the specified style is not present in `self.STYLES` class attribute.

Returns:
str: The font style unicode provided by colorama.
str: The font style unicode.
"""
style = style.lower()
if style not in self.STYLES:
raise ValueError(
f"Invalid style. Choose from: {', '.join(self.STYLES.keys())}"
)
return self.STYLES[style]
return f"\033[{self.STYLES[style]}m"

def decorate(self, text: str) -> str:
"""Decorate the text string and returns it.
Expand All @@ -65,6 +65,6 @@ def decorate(self, text: str) -> str:
str: The text decorated with the font style.
"""
if self.style:
return f"{self.style}{text}{Style.RESET_ALL}"
return f"{self.style}{text}{RESET_ALL}"

return text
1 change: 0 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"python": ("https://docs.python.org/3/", None),
"dasbus": ("https://dasbus.readthedocs.io/en/latest/", None),
"requests": ("https://requests.readthedocs.io/en/latest/", None),
"colorama": ("https://super-devops.readthedocs.io/en/latest/", None),
}

templates_path = ["_templates"]
Expand Down
1 change: 0 additions & 1 deletion packaging/command-line-assistant.spec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ BuildRequires: systemd-units

Requires: python3-dasbus
Requires: python3-requests
Requires: python3-colorama
Requires: systemd

# Not needed after RHEL 10 as it is native in Python 3.11+
Expand Down
Loading
Loading