|
|
from __future__ import annotations |
|
|
|
|
|
import contextlib |
|
|
import enum |
|
|
import os |
|
|
import re |
|
|
import typing |
|
|
|
|
|
|
|
|
@typing.overload |
|
|
def env_flag(name: str, default: bool) -> bool: ... |
|
|
|
|
|
|
|
|
@typing.overload |
|
|
def env_flag(name: str, default: bool | None = None) -> bool | None: ... |
|
|
|
|
|
|
|
|
def env_flag(name: str, default: bool | None = None) -> bool | None: |
|
|
""" |
|
|
Accepts environt variables formatted as y/n, yes/no, 1/0, true/false, |
|
|
on/off, and returns it as a boolean. |
|
|
|
|
|
If the environment variable is not defined, or has an unknown value, |
|
|
returns `default` |
|
|
""" |
|
|
v = os.getenv(name) |
|
|
if v and v.lower() in ('y', 'yes', 't', 'true', 'on', '1'): |
|
|
return True |
|
|
if v and v.lower() in ('n', 'no', 'f', 'false', 'off', '0'): |
|
|
return False |
|
|
return default |
|
|
|
|
|
|
|
|
class ColorSupport(enum.IntEnum): |
|
|
"""Color support for the terminal.""" |
|
|
|
|
|
NONE = 0 |
|
|
XTERM = 16 |
|
|
XTERM_256 = 256 |
|
|
XTERM_TRUECOLOR = 16777216 |
|
|
WINDOWS = 8 |
|
|
|
|
|
@classmethod |
|
|
def from_env(cls) -> ColorSupport: |
|
|
"""Get the color support from the environment. |
|
|
|
|
|
If any of the environment variables contain `24bit` or `truecolor`, |
|
|
we will enable true color/24 bit support. If they contain `256`, we |
|
|
will enable 256 color/8 bit support. If they contain `xterm`, we will |
|
|
enable 16 color support. Otherwise, we will assume no color support. |
|
|
|
|
|
If `JUPYTER_COLUMNS` or `JUPYTER_LINES` or `JPY_PARENT_PID` is set, we |
|
|
will assume true color support. |
|
|
|
|
|
Note that the highest available value will be used! Having |
|
|
`COLORTERM=truecolor` will override `TERM=xterm-256color`. |
|
|
""" |
|
|
variables = ( |
|
|
'FORCE_COLOR', |
|
|
'PROGRESSBAR_ENABLE_COLORS', |
|
|
'COLORTERM', |
|
|
'TERM', |
|
|
) |
|
|
|
|
|
if JUPYTER: |
|
|
|
|
|
return cls.XTERM_TRUECOLOR |
|
|
elif os.name == 'nt': |
|
|
|
|
|
|
|
|
|
|
|
from .terminal.os_specific import windows |
|
|
|
|
|
if ( |
|
|
windows.get_console_mode() |
|
|
& windows.WindowsConsoleModeFlags.ENABLE_PROCESSED_OUTPUT |
|
|
): |
|
|
return cls.XTERM_TRUECOLOR |
|
|
else: |
|
|
return cls.WINDOWS |
|
|
|
|
|
support = cls.NONE |
|
|
for variable in variables: |
|
|
value = os.environ.get(variable) |
|
|
if value is None: |
|
|
continue |
|
|
elif value in {'truecolor', '24bit'}: |
|
|
|
|
|
support = cls.XTERM_TRUECOLOR |
|
|
break |
|
|
elif '256' in value: |
|
|
support = max(cls.XTERM_256, support) |
|
|
elif value == 'xterm': |
|
|
support = max(cls.XTERM, support) |
|
|
|
|
|
return support |
|
|
|
|
|
|
|
|
def is_ansi_terminal( |
|
|
fd: typing.IO[typing.Any], |
|
|
is_terminal: bool | None = None, |
|
|
) -> bool | None: |
|
|
if is_terminal is None: |
|
|
|
|
|
if JUPYTER: |
|
|
is_terminal = True |
|
|
|
|
|
|
|
|
elif os.environ.get('PYCHARM_HOSTED') == '1' and not os.environ.get( |
|
|
'PYTEST_CURRENT_TEST' |
|
|
): |
|
|
is_terminal = True |
|
|
|
|
|
if is_terminal is None: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with contextlib.suppress(Exception): |
|
|
is_tty: bool = fd.isatty() |
|
|
|
|
|
if is_tty and ANSI_TERM_RE.match(os.environ.get('TERM', '')): |
|
|
is_terminal = True |
|
|
|
|
|
elif 'ANSICON' in os.environ: |
|
|
is_terminal = True |
|
|
elif os.name == 'nt': |
|
|
from .terminal.os_specific import windows |
|
|
|
|
|
return bool( |
|
|
windows.get_console_mode() |
|
|
& windows.WindowsConsoleModeFlags.ENABLE_PROCESSED_OUTPUT, |
|
|
) |
|
|
else: |
|
|
is_terminal = None |
|
|
|
|
|
return is_terminal |
|
|
|
|
|
|
|
|
def is_terminal( |
|
|
fd: typing.IO[typing.Any], |
|
|
is_terminal: bool | None = None, |
|
|
) -> bool | None: |
|
|
if is_terminal is None: |
|
|
|
|
|
is_terminal = is_ansi_terminal(fd) or None |
|
|
|
|
|
if is_terminal is None: |
|
|
|
|
|
is_terminal = env_flag('PROGRESSBAR_IS_TERMINAL', None) |
|
|
|
|
|
if is_terminal is None: |
|
|
|
|
|
|
|
|
try: |
|
|
is_terminal = fd.isatty() |
|
|
except Exception: |
|
|
is_terminal = False |
|
|
|
|
|
return is_terminal |
|
|
|
|
|
|
|
|
|
|
|
if os.name == 'nt': |
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
JUPYTER = bool( |
|
|
os.environ.get('JUPYTER_COLUMNS') |
|
|
or os.environ.get('JUPYTER_LINES') |
|
|
or os.environ.get('JPY_PARENT_PID') |
|
|
) |
|
|
COLOR_SUPPORT = ColorSupport.from_env() |
|
|
ANSI_TERMS = ( |
|
|
'([xe]|bv)term', |
|
|
'(sco)?ansi', |
|
|
'cygwin', |
|
|
'konsole', |
|
|
'linux', |
|
|
'rxvt', |
|
|
'screen', |
|
|
'tmux', |
|
|
'vt(10[02]|220|320)', |
|
|
) |
|
|
ANSI_TERM_RE: re.Pattern[str] = re.compile( |
|
|
f"^({'|'.join(ANSI_TERMS)})", re.IGNORECASE |
|
|
) |
|
|
|