Skip to content

Commit abe7f60

Browse files
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 <johannes.schindelin@gmx.de>
1 parent 2c4d077 commit abe7f60

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

compat/winansi.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,38 @@ static void detect_msys_tty(int fd)
591591

592592
#endif
593593

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

664+
if (enable_virtual_processing())
665+
return;
666+
632667
/* create a named pipe to communicate with the console thread */
633668
if (swprintf(name, ARRAY_SIZE(name) - 1, L"\\\\.\\pipe\\winansi%lu",
634669
GetCurrentProcessId()) < 0)

0 commit comments

Comments
 (0)