How to test if console supports ANSI color codes?

ansi-colors, c++, fmt, terminal, windows

Solution

On Windows you can get/set the ANSI color support by checking/setting the `ENABLE_VIRTUAL_TERMINAL_PROCESSING` flag with `SetConsoleMode()` or `GetConsoleMode()`.

There's also the `HKEY_CURRENT_USER\Console\VirtualTerminalLevel` registry key for global virtual terminal settings. See also https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences

On *nix you can use the following ways

- Check if console supports ANSI escape codes in Java

- Programmatically detect the ANSI escape code supported by terminal

Problem

I use fmt based logger with optionally color prints - which relies on ANSI color commands. Unfortunately on Windows 10 it is disabled by default. I know how to enable it but I still want to find out how to test whether the console supports ANSI commands or not. Does anybody know?

Original source

Related problems