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

Support click-help-colors [FEATURE] #47

Closed
sullivancolin opened this issue Feb 3, 2020 · 5 comments
Closed

Support click-help-colors [FEATURE] #47

sullivancolin opened this issue Feb 3, 2020 · 5 comments
Labels
feature New feature, enhancement or request

Comments

@sullivancolin
Copy link

Problem

I want to be able to replicate the functionality of click-contrib/click-help-colors to add colors to help text for easier readability

Ideal API

import typer
from click_help_colors import HelpColorsGroup

app = typer.Typer(cls=HelpColorsGroup,
    help_headers_color='yellow',
    help_options_color='green')

Attempted

By subclassing HelpColorsGroup and explicitly setting the colors in the __init__ it is possible to get colors

import typer
from click_help_colors import HelpColorsGroup


class CustomHelpColorsGroup(HelpColorsGroup):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.help_headers_color = "blue"
        self.help_options_color = "yellow"


app = typer.Typer(cls=CustomHelpColorsGroup)


@app.command()
def test_function(
    name: str = typer.Option("World", "--name", "-n", help="name to say hello")
) -> None:
    typer.echo(f"Hello {name}!")


@app.command()
def other() -> None:
    typer.echo("other")


if __name__ == "__main__":
    app()

typer_help_colors

However the color does not propagate to sub command help text

sub_command_no_color

@sullivancolin sullivancolin added the feature New feature, enhancement or request label Feb 3, 2020
@tiangolo
Copy link
Owner

You have to also provide the Command class, like:

import typer
from click_help_colors import HelpColorsGroup, HelpColorsCommand


class CustomHelpColorsGroup(HelpColorsGroup):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.help_headers_color = "blue"
        self.help_options_color = "yellow"


class CustomHelpColorsCommand(HelpColorsCommand):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.help_headers_color = "blue"
        self.help_options_color = "yellow"


app = typer.Typer(cls=CustomHelpColorsGroup)


@app.command(cls=CustomHelpColorsCommand)
def test_function(
    name: str = typer.Option("World", "--name", "-n", help="name to say hello")
) -> None:
    typer.echo(f"Hello {name}!")


@app.command(cls=CustomHelpColorsCommand)
def other() -> None:
    typer.echo("other")


if __name__ == "__main__":
    app()

This is because HelpColorsGroup defines its own method command that overrides the default command method from Click, to provide its own HelpColorsCommand class instead of the default Command class.

But Typer doesn't modify nor use Click classes directly, when you use @app.command() that is not the same as with a Click @cli.command().

Typer doesn't modify the function to replace it with an instance of a class, instead, it registers the function to use and the class to use, and then it creates the specific objects when you call it. But doing it as described above should work fine (I just tested it 😄 ).

@github-actions
Copy link

github-actions bot commented May 7, 2020

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

@github-actions github-actions bot closed this as completed May 7, 2020
@daddycocoaman
Copy link
Contributor

daddycocoaman commented May 28, 2020

Just wanted to add, if you want to avoid having to add the cls to every command and/or Typer, you can just subclass them. So adding onto @tiangolo, it would be something like this:

class LootMarshalTyper(typer.Typer):

    def __init__(self, *args, cls=CustomHelpColorsGroup, **kwargs) -> None:
        super().__init__(*args, cls=cls, **kwargs)

    def command(self, *args, cls=CustomHelpColorsCommand, **kwargs) -> typer.Typer.command:
        return super().command(*args, cls=cls, **kwargs)

Hope this helps.

@ssbarnea
Copy link
Sponsor Contributor

ssbarnea commented Apr 5, 2021

Here is a full example I found of using click-help-color with typer https://github.com/daddycocoaman/AzDummy/blob/78438183c95102c5aed5af3578af31601bd7f423/azdummy/core/typer.py#L2

@ssbarnea
Copy link
Sponsor Contributor

ssbarnea commented Mar 5, 2023

Apparently typer is no longer compatible with click-help-colors since 0.7

classabbyamp added a commit to miaowware/callsignlookuptools that referenced this issue Aug 30, 2023
click-help-colors is no longer compatible with typer and the colours
look fine as-is, so drop it.
tiangolo/typer#47 (comment)

fixes #33
classabbyamp added a commit to miaowware/callsignlookuptools that referenced this issue Aug 30, 2023
click-help-colors is no longer compatible with typer and the colours
look fine as-is, so drop it.
tiangolo/typer#47 (comment)

fixes #33
classabbyamp added a commit to miaowware/callsignlookuptools that referenced this issue Aug 31, 2023
click-help-colors is no longer compatible with typer and the colours
look fine as-is, so drop it.
tiangolo/typer#47 (comment)

fixes #33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature, enhancement or request
Projects
None yet
Development

No branches or pull requests

4 participants