msys2-runtime icon indicating copy to clipboard operation
msys2-runtime copied to clipboard

Virtual terminal processing is disabled on MSYS2 in Windows terminal

Open silverqx opened this issue 3 years ago • 6 comments

I have this code:

#include <iostream>
#include <windows.h>

int main()
{
    // Ansi output test
    std::cout << "\033[32mgreen\033[0m" << std::endl;
    // Get the current console mode
    DWORD mode = 0;
    if (GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &mode) == 0)
        std::cout << "failed" << std::endl;

    std::cout << "Mode : " << mode << std::endl;

    // Enable vt100
    SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);

    // Ansi output test
    std::cout << "\033[32mgreen\033[0m" << std::endl;
    // Get the current console mode
    mode = 0;
    if (GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &mode) == 0)
        std::cout << "failed" << std::endl;

    std::cout << "Mode : " << mode << std::endl;

    return 0;
}

When I compile it with the msvc and invoke it from the Windows Terminal then it returns 7. When I compile it with the ucrt64 g++/clang++ and invoke it from the wterminal then it returns 3.

Here are defines for the return value:

#define ENABLE_PROCESSED_OUTPUT             0x0001
#define ENABLE_WRAP_AT_EOL_OUTPUT           0x0002
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING  0x0004

Problem is that the vt100 support (ENABLE_VIRTUAL_TERMINAL_PROCESSING flag) is disabled when compiled with the ucrt64 g++/clang++ although the terminal obviously supports vt100 processing.

The question is does MSYS2 have any effect on this? Or does MSYS2 modify these flags or it is completely on the winapi, why it is disabled by default on msys2 and enabled on msvc.

silverqx avatar Apr 21 '22 10:04 silverqx

By default, msys2 disables conpty feature. It can be enabled with MSYS=enable_pcon environment variable. Make sure to set the variable before running any msys2 instance.

why it is disabled by default on msys2 and enabled on msvc.

Because conpty messes up output of many programs.

Biswa96 avatar Apr 21 '22 12:04 Biswa96

Thx for clarification, MSYS=enable_pcon is not important for me because I'm looking at it from the library developer view (I'm working on some shared library and want to have color output).

So if I understand it correctly, I will have to explicitly enable it through the SetConsoleMode(ENABLE_VIRTUAL_TERMINAL_PROCESSING) on MSYS2 wrapped in the #ifdef __MINGW32__ if I want my library to have a color or ansi output?

silverqx avatar Apr 21 '22 12:04 silverqx

AFAIK, SetConsoleMode is not required for msys2/cygwin without conpty because the text processing is done by mintty. I may be wrong. Others can provide correct answer.

Biswa96 avatar Apr 21 '22 12:04 Biswa96

AFAIK, SetConsoleMode is not required for msys2/cygwin without conpty because the text processing is done by mintty

You are right, I would forget about that, it of course depends on the console, but who uses mintty these days if wterm is at v1.12 with all that features 😎 that it provides.

But how is possible that all commands provided by MSYS mc, ls and all other commands have color output, but if you compile your own exe it is disabled. These commands have to use ansi sequences as well to colorize output and all other ansi stuffs, right?

silverqx avatar Apr 21 '22 13:04 silverqx

Because it looks like pcon is enabled for all linux executables provided by MSYS2 but is only disabled when a user compiles his own exe with g++/clang 🤔, am I missing something?

silverqx avatar Apr 22 '22 08:04 silverqx