Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support mintty (MSYS/Cygwin terminals) #226

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
36 changes: 35 additions & 1 deletion colorama/ansitowin32.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import sys
import os
import ctypes

from .ansi import AnsiFore, AnsiBack, AnsiStyle, Style
from .winterm import WinTerm, WinColor, WinStyle
Expand All @@ -13,6 +14,39 @@
winterm = WinTerm()


def is_msys_cygwin_tty(stream):
try:
import msvcrt
except ImportError:
return False
SSE4 marked this conversation as resolved.
Show resolved Hide resolved

if not hasattr(stream, "fileno"):
return False

if not hasattr(ctypes, "windll") or not hasattr(ctypes.windll.kernel32, "GetFileInformationByHandleEx"):
return False

fileno = stream.fileno()
handle = msvcrt.get_osfhandle(fileno)
FileNameInfo = 2

class FILE_NAME_INFO(ctypes.Structure):
_fields_ = [('FileNameLength', ctypes.c_ulong),
('FileName', ctypes.c_wchar * 40)]

info = FILE_NAME_INFO()
ret = ctypes.windll.kernel32.GetFileInformationByHandleEx(handle,
FileNameInfo,
ctypes.byref(info),
ctypes.sizeof(info))
if ret == 0:
return False
SSE4 marked this conversation as resolved.
Show resolved Hide resolved

msys_pattern = r"\\msys-[0-9a-f]{16}-pty\d-(to|from)-master"
cygwin_pattern = r"\\cygwin-[0-9a-f]{16}-pty\d-(to|from)-master"
return re.match(msys_pattern, info.FileName) is not None or \
re.match(cygwin_pattern, info.FileName) is not None

class StreamWrapper(object):
'''
Wraps a stream (such as stdout), acting as a transparent proxy for all
Expand Down Expand Up @@ -50,7 +84,7 @@ def isatty(self):
except AttributeError:
return False
else:
return stream_isatty()
return stream_isatty() or is_msys_cygwin_tty(stream)

@property
def closed(self):
Expand Down