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

Add color for success and error messages #5

Open
aj3sh opened this issue Jan 15, 2024 · 2 comments
Open

Add color for success and error messages #5

aj3sh opened this issue Jan 15, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@aj3sh
Copy link
Member

aj3sh commented Jan 15, 2024

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]))
)

Ref: https://stackoverflow.com/questions/287871/how-do-i-print-colored-text-to-the-terminal

@aj3sh aj3sh added the enhancement New feature or request label Jan 15, 2024
@aj3sh aj3sh added this to Commitlint Jan 15, 2024
@aj3sh
Copy link
Member Author

aj3sh commented Apr 3, 2024

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).

@aj3sh aj3sh self-assigned this Apr 10, 2024
@aj3sh aj3sh removed their assignment May 16, 2024
@kiyoon
Copy link

kiyoon commented Nov 27, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: No status
Development

No branches or pull requests

2 participants