| 5 | from typing import Union |
| 6 | |
| 7 | def _supports_kitty_graphics() -> bool: |
| 8 | import platform |
| 9 | |
| 10 | if platform.system() not in ("Darwin", "Linux"): |
| 11 | return False |
| 12 | |
| 13 | isatty = getattr(sys.stdout, "isatty", None) |
| 14 | if not callable(isatty) or not isatty(): |
| 15 | return False |
| 16 | # Hardcoding process names instead of using |
| 17 | # https://sw.kovidgoyal.net/kitty/graphics-protocol/#querying-support-and-available-transmission-mediums |
| 18 | # to avoid startup slowdown |
| 19 | supported_terminals = { |
| 20 | "ghostty", |
| 21 | "iTerm2", |
| 22 | "kitty", |
| 23 | "konsole", |
| 24 | "warp", |
| 25 | "wayst", |
| 26 | "wezterm-gui", |
| 27 | } |
| 28 | import psutil |
| 29 | |
| 30 | try: |
| 31 | process = psutil.Process() |
| 32 | while process := process.parent(): |
| 33 | if process.name() in supported_terminals: |
| 34 | return True |
| 35 | except (psutil.Error, OSError): |
| 36 | # Walking the process tree can fail when /proc is mounted with |
| 37 | # ``hidepid`` on shared multi-user systems (common on HPC clusters): |
| 38 | # ancestor processes owned by other users are inaccessible and psutil |
| 39 | # raises AccessDenied. Treat as "unsupported" rather than letting it |
| 40 | # abort the import of IPython. |
| 41 | return False |
| 42 | return False |
| 43 | |
| 44 | |
| 45 | supports_kitty_graphics = _supports_kitty_graphics() |