Enable ANSI escape code processing on Windows 10 and later

This lets the engine and projects use the same color codes
in the terminal on all platforms. This includes support for
`print_rich()` (GDScript) and `print_line_rich()` (C++).
This commit is contained in:
Hugo Locurcio 2020-12-05 12:50:20 +01:00
parent d11117d9d8
commit debfc7c38b
No known key found for this signature in database
GPG Key ID: 39E8F8BE30B0A49C
1 changed files with 15 additions and 0 deletions

View File

@ -1148,6 +1148,21 @@ OS_Windows::OS_Windows(HINSTANCE _hInstance) {
DisplayServerWindows::register_windows_driver(); DisplayServerWindows::register_windows_driver();
// Enable ANSI escape code support on Windows 10 v1607 (Anniversary Update) and later.
// This lets the engine and projects use ANSI escape codes to color text just like on macOS and Linux.
//
// NOTE: The engine does not use ANSI escape codes to color error/warning messages; it uses Windows API calls instead.
// Therefore, error/warning messages are still colored on Windows versions older than 10.
HANDLE stdoutHandle;
stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD outMode = 0;
outMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!SetConsoleMode(stdoutHandle, outMode)) {
// Windows 8.1 or below, or Windows 10 prior to Anniversary Update.
print_verbose("Can't set the ENABLE_VIRTUAL_TERMINAL_PROCESSING Windows console mode. `print_rich()` will not work as expected.");
}
Vector<Logger *> loggers; Vector<Logger *> loggers;
loggers.push_back(memnew(WindowsTerminalLogger)); loggers.push_back(memnew(WindowsTerminalLogger));
_set_logger(memnew(CompositeLogger(loggers))); _set_logger(memnew(CompositeLogger(loggers)));