Adding color to CLI messages would be helpful for users to distinguish between error, success, or normal messages. Let's attempt to implement this without using any additional dependencies initially; if that's not possible, we can explore and discuss the packages to use.
Here is my POC for the color message (tested on Mac's terminal and iterm):
import sys
from typing import List, Union
class Styles:
RESET = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
ITALIC = "\033[3m"
SELECTED = "\033[7m"
BLACK = "\033[30m"
GREY = "\33[90m"
CYAN = "\033[96m"
RED = "\033[91m"
GREEN = "\033[92m"
BLUE = "\033[34m"
YELLOW = "\033[93m"
MAGENTA = "\033[35m"
WHITE = "\033[37m"
class StyledText:
def __init__(self, text: str, style: Union[str, List[str]]):
self.text = text
self.styles = style if isinstance(style, List) else [style]
# creating styled text
joined_styles = "".join(self.styles)
self._styled_text = f"{joined_styles}{text}{Styles.RESET}"
def __str__(self) -> str:
return self._styled_text
def __repr__(self) -> str:
return self._styled_text
sys.stdout.write(str(StyledText("I am red.\n", Styles.RED)))
sys.stdout.write(str(StyledText("I am green.\n", Styles.GREEN)))
sys.stdout.write(str(StyledText("I am underlined.\n", Styles.UNDERLINE)))
sys.stdout.write(
str(StyledText("I am italic and blue.\n", [Styles.UNDERLINE, Styles.BLUE]))
)
sys.stdout.write(
str(StyledText("I am yellow selected.\n", [Styles.SELECTED, Styles.YELLOW]))
)
Tested on Windows 11 Command Prompt, Power Shell, and Git bash: Working fine.
Tested on Windows 10 Command Prompt: Not working (displays the color character \033[0m).
Adding color to CLI messages would be helpful for users to distinguish between error, success, or normal messages. Let's attempt to implement this without using any additional dependencies initially; if that's not possible, we can explore and discuss the packages to use.
Here is my POC for the color message (tested on Mac's terminal and iterm):
Ref: https://stackoverflow.com/questions/287871/how-do-i-print-colored-text-to-the-terminal