-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupports_ansi.py
32 lines (24 loc) · 1007 Bytes
/
supports_ansi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""
This code is from the discord.py repository.
Source: https://github.com/Rapptz/discord.py/blob/master/discord/utils.py#L1241-L1253
"""
import os
import sys
from typing import Any
__all__ = ("stream_supports_colour",)
def is_docker() -> bool:
path = "/proc/self/cgroup"
return os.path.exists("/.dockerenv") or (
os.path.isfile(path) and any("docker" in line for line in open(path))
)
def stream_supports_colour(stream: Any) -> bool:
# Pycharm and Vscode support colour in their inbuilt editors
if "PYCHARM_HOSTED" in os.environ or os.environ.get("TERM_PROGRAM") == "vscode":
return True
is_a_tty = hasattr(stream, "isatty") and stream.isatty()
if sys.platform != "win32":
# Docker does not consistently have a tty attached to it
return is_a_tty or is_docker()
# ANSICON checks for things like ConEmu
# WT_SESSION checks if this is Windows Terminal
return is_a_tty and ("ANSICON" in os.environ or "WT_SESSION" in os.environ)