Check env vars and for tty/dumb terminal.
(*, no_color: bool | None = None, force_color: bool | None = None)
| 30 | # copy from: https://github.com/termcolor/termcolor |
| 31 | @functools.cache |
| 32 | def can_colorize(*, no_color: bool | None = None, force_color: bool | None = None) -> bool: |
| 33 | """Check env vars and for tty/dumb terminal.""" |
| 34 | import io |
| 35 | if no_color is not None and no_color: |
| 36 | return False |
| 37 | if force_color is not None and force_color: |
| 38 | return True |
| 39 | |
| 40 | # Then check env vars: |
| 41 | if os.environ.get('ANSI_COLORS_DISABLED'): |
| 42 | return False |
| 43 | if os.environ.get('NO_COLOR'): |
| 44 | return False |
| 45 | if os.environ.get('FORCE_COLOR'): |
| 46 | return True |
| 47 | |
| 48 | # Then check system: |
| 49 | if os.environ.get('TERM') == 'dumb': |
| 50 | return False |
| 51 | if not hasattr(sys.stdout, 'fileno'): |
| 52 | return False |
| 53 | |
| 54 | try: |
| 55 | return os.isatty(sys.stdout.fileno()) |
| 56 | except io.UnsupportedOperation: |
| 57 | return sys.stdout.isatty() |
| 58 | |
| 59 | |
| 60 | class ColorFormatter(logging.Formatter): |