You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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):
importsysfromtypingimportList, UnionclassStyles:
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"classStyledText:
def__init__(self, text: str, style: Union[str, List[str]]):
self.text=textself.styles=styleifisinstance(style, List) else [style]
# creating styled textjoined_styles="".join(self.styles)
self._styled_text=f"{joined_styles}{text}{Styles.RESET}"def__str__(self) ->str:
returnself._styled_textdef__repr__(self) ->str:
returnself._styled_textsys.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).
I suggest to use rich. Although it adds a dependency, it makes the code much cleaner and it's a common dependency that is used in many python projects.
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
The text was updated successfully, but these errors were encountered: