Skip to content

Commit e2a23f4

Browse files
dschoGit for Windows Build Agent
authored and
Git for Windows Build Agent
committed
win32: use native ANSI sequence processing, if possible
Windows 10 version 1511 (also known as Anniversary Update), according to https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences introduced native support for ANSI sequence processing. This allows using colors from the entire 24-bit color range. All we need to do is test whether the console's "virtual processing support" can be enabled. If it can, we do not even need to start the `console_thread` to handle ANSI sequences. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 860b0dd commit e2a23f4

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

compat/winansi.c

+35
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,38 @@ static void detect_msys_tty(int fd)
594594

595595
#endif
596596

597+
static HANDLE std_console_handle;
598+
static DWORD std_console_mode;
599+
600+
static void reset_std_console_mode(void)
601+
{
602+
SetConsoleMode(std_console_handle, std_console_mode);
603+
}
604+
605+
static int enable_virtual_processing(void)
606+
{
607+
std_console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
608+
if (std_console_handle == INVALID_HANDLE_VALUE ||
609+
!GetConsoleMode(std_console_handle, &std_console_mode)) {
610+
std_console_handle = GetStdHandle(STD_ERROR_HANDLE);
611+
if (std_console_handle == INVALID_HANDLE_VALUE ||
612+
!GetConsoleMode(std_console_handle, &std_console_mode))
613+
return 0;
614+
}
615+
616+
if (std_console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)
617+
return 1;
618+
619+
if (!SetConsoleMode(std_console_handle,
620+
std_console_mode |
621+
ENABLE_PROCESSED_OUTPUT |
622+
ENABLE_VIRTUAL_TERMINAL_PROCESSING))
623+
return 0;
624+
625+
atexit(reset_std_console_mode);
626+
return 1;
627+
}
628+
597629
/*
598630
* Wrapper for isatty(). Most calls in the main git code
599631
* call isatty(1 or 2) to see if the instance is interactive
@@ -632,6 +664,9 @@ void winansi_init(void)
632664
return;
633665
}
634666

667+
if (enable_virtual_processing())
668+
return;
669+
635670
/* create a named pipe to communicate with the console thread */
636671
if (swprintf(name, ARRAY_SIZE(name) - 1, L"\\\\.\\pipe\\winansi%lu",
637672
GetCurrentProcessId()) < 0)

0 commit comments

Comments
 (0)