Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
COLORS = __all__[:-2]
is_ipython = "get_ipython" in dir()
if (
os.environ.get("CMDER_ROOT")
or os.environ.get("VSCODE_PID")
or os.environ.get("TERM_PROGRAM") == "Hyper"
):
is_native_powershell = False
else:
is_native_powershell = True
try:
is_powershell = "powershell" in shellingham.detect_shell()[0]
except shellingham.ShellDetectionFailure:
is_powershell = False
if is_ipython or (is_powershell and is_native_powershell):
"""when ipython is fired lot of variables like _oh, etc are used.
There are so many ways to find current python interpreter is ipython.
get_ipython is easiest is most appealing for readers to understand.
"""
DISABLE_COLOR = True
else:
DISABLE_COLOR = False
class ColoredString(object):
"""Enhanced string for __len__ operations on Colored output."""
def __init__(self, color, s, always_color=False, bold=False):
def get(cls): # type: () -> Shell
"""
Retrieve the current shell.
"""
if cls._shell is not None:
return cls._shell
try:
name, path = detect_shell(os.getpid())
except (RuntimeError, ShellDetectionFailure):
raise RuntimeError("Unable to detect the current shell.")
cls._shell = cls(name, path)
return cls._shell
def _detect_shell():
shell = os.environ.get('SHELL', None)
if not shell:
if 'CMDER_ROOT' in os.environ:
shell = 'Cmder'
elif windows:
try:
_, shell = shellingham.detect_shell()
except shellingham.ShellDetectionFailure:
shell = os.environ.get('COMSPEC', 'cmd.exe')
else:
shell = 'sh'
return shell
def _shell_info(self) -> Tuple[str, Path]:
# detect by shellingham
try:
name, path = detect_shell()
except (ShellDetectionFailure, RuntimeError):
pass
else:
return name, Path(path)
# detect by env
for env in ('SHELL', 'COMSPEC'):
path = os.environ.get(env)
if path:
path = Path(path).resolve()
return path.stem, path
# try to find any known shell
for name in sorted(self.shells):
path = shutil.which(name)
if path is not None:
return name, Path(path)