MCPcopy
hub / github.com/InternLM/lmdeploy / can_colorize

Function can_colorize

lmdeploy/utils.py:32–57  ·  view source on GitHub ↗

Check env vars and for tty/dumb terminal.

(*, no_color: bool | None = None, force_color: bool | None = None)

Source from the content-addressed store, hash-verified

30# copy from: https://github.com/termcolor/termcolor
31@functools.cache
32def 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
60class ColorFormatter(logging.Formatter):

Callers 2

_red_textFunction · 0.90
formatMethod · 0.85

Calls 1

getMethod · 0.45

Tested by

no test coverage detected