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

Recommended way to detect interactive terminal? #1024

Closed
hugovk opened this issue Nov 19, 2022 · 4 comments · Fixed by #1025
Closed

Recommended way to detect interactive terminal? #1024

hugovk opened this issue Nov 19, 2022 · 4 comments · Fixed by #1025

Comments

@hugovk
Copy link

hugovk commented Nov 19, 2022

I'm maintaining the termcolor colour library and recently added tty detection, so colour is disabled for things like pipes:

def _can_do_colour() -> bool:
    """Check env vars and for tty/dumb terminal"""
    if "ANSI_COLORS_DISABLED" in os.environ:
        return False
    if "NO_COLOR" in os.environ:
        return False
    if "FORCE_COLOR" in os.environ:
        return True
    return (
        hasattr(sys.stdout, "isatty")
        and sys.stdout.isatty()
        and os.environ.get("TERM") != "dumb"
    )

https://github.com/termcolor/termcolor/blob/ec269e608a08b33beb739b6ab5f17b9217a5692a/src/termcolor/termcolor.py#L106-L118

Unfortunately this means colour is disabled in Jupyter/JupyterLab: termcolor/termcolor#28

I see there was a plan to set isatty to True for ipykernel.iostream.OutStream, but instead it was made configurable to make sure things like pagers (less, more) didn't get confused:

So at the moment, if a Jupyter user wants colour from termcolor, they need to either set an env var FORCE_COLOR=1, or configure ipykernel.iostream.OutStream so isatty=True.

But if we wanted to add auto-detection to termcolor's _can_do_colour, what sort of thing should we query to know Jupyter is being used in an interactive way?

Thank you!

@blink1073
Copy link
Contributor

Hi @hugovk, we inject some environment variables here, we could add an environment variable that you could put in your _can_do_color test, or perhaps set FORCE_COLOR=1 if that makes more sense.

@hugovk
Copy link
Author

hugovk commented Nov 20, 2022

An env var would be great.

Or setting FORCE_COLOR=1 is an interesting idea, it's also supported by a number of other tools, such as Rich, pytest, nox, tox, pypa/build (see links in Textualize/rich#2425).

Having said that, I see you're already setting env["CLICOLOR"] = "1" there, so I should probably add support for that into termcolor:

@blink1073
Copy link
Contributor

Based on that link, it seems like CLICOLOR_FORCE is what we should be using, since we are not a tty.

@blink1073
Copy link
Contributor

But we could set both since rich already expects CLICOLOR.

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

Successfully merging a pull request may close this issue.

2 participants