Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win32: use native ANSI sequence processing, if possible #4700

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions compat/winansi.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,38 @@ static void detect_msys_tty(int fd)

#endif

static HANDLE std_console_handle;
static DWORD std_console_mode;

static void reset_std_console_mode(void)
{
SetConsoleMode(std_console_handle, std_console_mode);
}

static int enable_virtual_processing(void)
{
std_console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (std_console_handle == INVALID_HANDLE_VALUE ||
!GetConsoleMode(std_console_handle, &std_console_mode)) {
std_console_handle = GetStdHandle(STD_ERROR_HANDLE);
if (std_console_handle == INVALID_HANDLE_VALUE ||
!GetConsoleMode(std_console_handle, &std_console_mode))
return 0;
}

if (std_console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)
return 1;

if (!SetConsoleMode(std_console_handle,
std_console_mode |
ENABLE_PROCESSED_OUTPUT |
ENABLE_VIRTUAL_TERMINAL_PROCESSING))
rimrul marked this conversation as resolved.
Show resolved Hide resolved
return 0;

atexit(reset_std_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 +664,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