Skip to content

Commit be065d7

Browse files
committed
isMsysPty doesn't seem to detect msys term anymore
it doesn't seem to to be repairable easily: - `GetFileType(h)` doesn't return FILE_TYPE_PIPE - `GetFileInformationByHandleEx()` also fails Anyways, doesn't seem to be necessary for color_out - `setWinTermAnsiColors(STD_OUTPUT_HANDLE))` succeeds. For UTF-8 output, we may perhaps try to check env vars.
1 parent 45c3625 commit be065d7

3 files changed

Lines changed: 9 additions & 60 deletions

File tree

src/host.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,14 @@ struct init_data *common_preinit(int argc, char *argv[])
581581
}
582582
is_win_utf8_terminal = (getenv("TERM") == nullptr &&
583583
_isatty(fileno(stdout)) &&
584-
win_has_ancestor_process("WindowsTerminal.exe"))
585-
|| isMsysPty(fileno(stdout));
584+
win_has_ancestor_process("WindowsTerminal.exe"));
585+
if (!is_win_utf8_terminal) { // check MSYS term
586+
char *con = getenv("MSYSCON");
587+
char *lc_ctype = getenv("LC_CTYPE");
588+
is_win_utf8_terminal = con != nullptr && lc_ctype != nullptr &&
589+
strcmp(con, "mintty.exe") == 0 &&
590+
strstr(lc_ctype, ".UTF-8") != nullptr;
591+
}
586592
#endif
587593
u8_to_mb_init(is_win_utf8_terminal); // setlocale(LC_CTYPE, "");
588594

src/utils/color_out.c

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -75,61 +75,6 @@ static bool setWinTermAnsiColors(DWORD stream) {
7575
}
7676
#endif // defined _WIN32
7777

78-
/// Taken from [rang](https://github.com/agauniyal/rang)
79-
bool
80-
isMsysPty(int fd)
81-
{
82-
#ifdef _WIN32
83-
// Dynamic load for binary compatibility with old Windows
84-
BOOL (*ptrGetFileInformationByHandleEx)(
85-
HANDLE hFile, FILE_INFO_BY_HANDLE_CLASS FileInformationClass,
86-
LPVOID lpFileInformation, DWORD dwBufferSize) = nullptr;
87-
ptrGetFileInformationByHandleEx = (void *)
88-
GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),
89-
"GetFileInformationByHandleEx");
90-
if (!ptrGetFileInformationByHandleEx) {
91-
return false;
92-
}
93-
94-
HANDLE h = (HANDLE) (intptr_t) _get_osfhandle(fd);
95-
if (h == INVALID_HANDLE_VALUE) {
96-
return false;
97-
}
98-
99-
// Check that it's a pipe:
100-
if (GetFileType(h) != FILE_TYPE_PIPE) {
101-
return false;
102-
}
103-
104-
// POD type is binary compatible with FILE_NAME_INFO from WinBase.h
105-
// It have the same alignment and used to avoid UB in caller code
106-
struct MY_FILE_NAME_INFO {
107-
DWORD FileNameLength;
108-
WCHAR FileName[MAX_PATH];
109-
};
110-
111-
struct MY_FILE_NAME_INFO pNameInfo = { 0 };
112-
113-
// Check pipe name is template of
114-
// {"cygwin-","msys-"}XXXXXXXXXXXXXXX-ptyX-XX
115-
if (!ptrGetFileInformationByHandleEx(h, FileNameInfo, &pNameInfo,
116-
sizeof pNameInfo)) {
117-
return false;
118-
}
119-
// std::wstring name(pNameInfo.FileName, pNameInfo.FileNameLength / sizeof(WCHAR));
120-
if ((wcsstr(pNameInfo.FileName, L"msys-") == nullptr &&
121-
wcsstr(pNameInfo.FileName, L"cygwin-") == nullptr) ||
122-
wcsstr(pNameInfo.FileName, L"-pty") == nullptr) {
123-
return false;
124-
}
125-
126-
return true;
127-
#else
128-
(void) fd;
129-
return false;
130-
#endif
131-
}
132-
13378
ADD_TO_PARAM("log-color", "* log-color[=no]\n"
13479
" Force enable/disable ANSI text formatting.\n");
13580
ADD_TO_PARAM("log-nocolor", "* log-nocolor\n"
@@ -155,8 +100,7 @@ is_output_color()
155100
return strcmp(env_val, "0") != 0;
156101
}
157102
#ifdef _WIN32
158-
return isMsysPty(fileno(stdout)) ||
159-
(_isatty(fileno(stdout)) &&
103+
return (_isatty(fileno(stdout)) &&
160104
setWinTermAnsiColors(STD_OUTPUT_HANDLE));
161105
#else
162106
return isatty(fileno(stdout));

src/utils/color_out.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@
9898
extern "C" {
9999
#endif
100100

101-
bool isMsysPty(int fd);
102101
bool color_output_init(void);
103102
[[gnu::format(printf, 1, 2)]] int color_printf(const char *format, ...);
104103

0 commit comments

Comments
 (0)