From 45506bb12f0f9fdc49de3b15e6f05bf39001a471 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 25 May 2024 10:41:11 +0200 Subject: [PATCH] amend! win32: use native ANSI sequence processing, if possible 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. Or, almost all we need to do: When `console_thread()` does its work, it uses the Unicode-aware `write_console()` function to write to the Win32 Console, which supports Git for Windows' implicit convention that all text that is written is encoded in UTF-8. The same is not necessarily true if native ANSI sequence processing is used, as the output is then subject to the current code page. Let's ensure that the code page is set to `CP_UTF8` as long as Git writes to it. Signed-off-by: Johannes Schindelin --- compat/winansi.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/compat/winansi.c b/compat/winansi.c index 104db975b5591a..664cdb56f26a31 100644 --- a/compat/winansi.c +++ b/compat/winansi.c @@ -595,11 +595,15 @@ static void detect_msys_tty(int fd) #endif static HANDLE std_console_handle; -static DWORD std_console_mode; +static DWORD std_console_mode = ENABLE_VIRTUAL_TERMINAL_PROCESSING; +static UINT std_console_code_page = CP_UTF8; -static void reset_std_console_mode(void) +static void reset_std_console(void) { - SetConsoleMode(std_console_handle, std_console_mode); + if (std_console_mode != ENABLE_VIRTUAL_TERMINAL_PROCESSING) + SetConsoleMode(std_console_handle, std_console_mode); + if (std_console_code_page != CP_UTF8) + SetConsoleOutputCP(std_console_code_page); } static int enable_virtual_processing(void) @@ -613,6 +617,14 @@ static int enable_virtual_processing(void) return 0; } + std_console_code_page = GetConsoleOutputCP(); + if (std_console_code_page != CP_UTF8) + SetConsoleOutputCP(CP_UTF8); + if (!std_console_code_page) + std_console_code_page = CP_UTF8; + + atexit(reset_std_console); + if (std_console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) return 1; @@ -622,7 +634,6 @@ static int enable_virtual_processing(void) ENABLE_VIRTUAL_TERMINAL_PROCESSING)) return 0; - atexit(reset_std_console_mode); return 1; }