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

click.echo does not force colors on Windows with color=True #2606

Open
krisztianloki opened this issue Sep 4, 2023 · 4 comments
Open

click.echo does not force colors on Windows with color=True #2606

krisztianloki opened this issue Sep 4, 2023 · 4 comments
Milestone

Comments

@krisztianloki
Copy link

Using click.echo(click.style("Foo", fg="red"), color=True) on Windows will not print "Foo" in red if the output is not a terminal that supports colors, so it seems that color=True is ignored on Windows.

Here is an example code to reproduce the bug:

import click


def main():
    test_txt = "click_echo.txt"
    with open(test_txt, "w") as f:
        click.secho("Without setting color", file=f, fg="red")
        click.secho("With color=False", file=f, fg="red", color=False)
        click.secho("With color=True", file=f, fg="red", color=True)

    click.echo(f"Check the contents of {test_txt}")


if __name__ == "__main__":
    main()

After running it on Linux the third line of click_echo.txt will contain the ANSI color codes but on Windows all three lines will be the same (the behavior is the same if echo prints on the console and the script output is redirected to a file)

Environment:

  • Python version: 3.9
  • Click version: 8.1.7
@davidism
Copy link
Member

davidism commented Sep 4, 2023

Windows uses a different mechanism for color than Unix. It's not possible to force it on. Or if it is, you'd need to take that up with colorama.

@davidism davidism closed this as not planned Won't fix, can't repro, duplicate, stale Sep 4, 2023
@krisztianloki
Copy link
Author

I've looked into it and it seems that there is a bug in click itself: the color parameter is not passed when auto_wrap_for_ansi() is called on line 317 in utils.py:

    # ANSI style code support. For no message or bytes, nothing happens.
    # When outputting to a file instead of a terminal, strip codes.
    else:
        color = resolve_color_default(color)

        if should_strip_ansi(file, color):
            out = strip_ansi(out)
        elif WIN:
            if auto_wrap_for_ansi is not None:
                file = auto_wrap_for_ansi(file)  # type: ignore
            elif not color:
                out = strip_ansi(out)

If I change it to file = auto_wrap_for_ansi(file, color) then forcing colors seems to work.

@davidism davidism reopened this Sep 5, 2023
@davidism
Copy link
Member

davidism commented Sep 5, 2023

Thanks for looking. Happy to review a PR.

@kdeldycke
Copy link
Contributor

FWIW, I had to skip color-based tests on Windows in a lot of my Click-based libraries:

So yes, I am pretty sure there is something wrong with colors on Windows. But I never found a way to reliably fix it and find the root cause.

Even with loads of hacks and workarounds in my Click Extra project, like:

So yeah, @Tyl13 is probably onto something here with his #2731 PR ! :)

Related issues might include:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants