MCPcopy
hub / github.com/Textualize/rich / is_terminal

Method is_terminal

rich/console.py:931–976  ·  view source on GitHub ↗

Check if the console is writing to a terminal. Returns: bool: True if the console writing to a device capable of understanding escape sequences, otherwise False.

(self)

Source from the content-addressed store, hash-verified

929
930 @property
931 def is_terminal(self) -> bool:
932 """Check if the console is writing to a terminal.
933
934 Returns:
935 bool: True if the console writing to a device capable of
936 understanding escape sequences, otherwise False.
937 """
938 # If dev has explicitly set this value, return it
939 if self._force_terminal is not None:
940 return self._force_terminal
941
942 # Fudge for Idle
943 if hasattr(sys.stdin, "__module__") and sys.stdin.__module__.startswith(
944 "idlelib"
945 ):
946 # Return False for Idle which claims to be a tty but can't handle ansi codes
947 return False
948
949 if self.is_jupyter:
950 # return False for Jupyter, which may have FORCE_COLOR set
951 return False
952
953 environ = self._environ
954
955 tty_compatible = environ.get("TTY_COMPATIBLE", "")
956 # 0 indicates device is not tty compatible
957 if tty_compatible == "0":
958 return False
959 # 1 indicates device is tty compatible
960 if tty_compatible == "1":
961 return True
962
963 # https://force-color.org/
964 force_color = environ.get("FORCE_COLOR")
965 if force_color is not None:
966 return force_color != ""
967
968 # Any other value defaults to auto detect
969 isatty: Optional[Callable[[], bool]] = getattr(self.file, "isatty", None)
970 try:
971 return False if isatty is None else isatty()
972 except ValueError:
973 # in some situation (at the end of a pytest run for example) isatty() can raise
974 # ValueError: I/O operation on closed file
975 # return False because we aren't in a terminal anymore
976 return False
977
978 @property
979 def is_dumb_terminal(self) -> bool:

Callers

nothing calls this directly

Calls 1

getMethod · 0.45

Tested by

no test coverage detected