Skip to content

Commit

Permalink
win32: use native ANSI sequence processing, if possible
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dscho committed Nov 22, 2023
1 parent 4b968f3 commit 60107fb
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions compat/winansi.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,35 @@ static void detect_msys_tty(int fd)

#endif

static HANDLE stdout_handle;
static DWORD stdout_console_mode;

static void reset_stdout_console_mode(void)
{
SetConsoleMode(stdout_handle, stdout_console_mode);
}

static int enable_virtual_processing(void)
{
stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (stdout_handle == INVALID_HANDLE_VALUE)
return 0;

if (!GetConsoleMode(stdout_handle, &stdout_console_mode))
return 0;

if (stdout_console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)
return 1;

if (!SetConsoleMode(stdout_handle,
stdout_console_mode |
ENABLE_VIRTUAL_TERMINAL_PROCESSING))
return 0;

atexit(reset_stdout_console_mode);
return 1;
}

/*
* Wrapper for isatty(). Most calls in the main git code
* call isatty(1 or 2) to see if the instance is interactive
Expand Down Expand Up @@ -632,6 +661,9 @@ void winansi_init(void)
return;
}

if (enable_virtual_processing())
return;

/* create a named pipe to communicate with the console thread */
if (swprintf(name, ARRAY_SIZE(name) - 1, L"\\\\.\\pipe\\winansi%lu",
GetCurrentProcessId()) < 0)
Expand Down

0 comments on commit 60107fb

Please sign in to comment.