Implement VT100 escape-code detection for Windows

Latest versions of windows have support for the parsing VT100 escape
code sequences, allowing for terminal colors similar to Linux.

https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#screen-colors

Here I have it get the console mode, set the
`ENABLE_VIRTUAL_TERMINAL_PROCESSING` flag, and then grab the console
mode again to both verify that VT100 escape sequences are supported and
that it is enabled after setting it to determine if the printer should
allow for fancy-color mode.
This commit is contained in:
Wunkolo
2020-10-15 13:08:59 -07:00
committed by Wunkolo
parent 5119ece0dd
commit 9867754d08

View File

@@ -9,6 +9,11 @@
#include "cpuid.h"
#include "uarch.h"
#ifdef _WIN32
#define NOMINMAX
#include <Windows.h>
#endif
#define COL_NONE ""
#define COL_INTEL_FANCY_1 "\x1b[46;1m"
#define COL_INTEL_FANCY_2 "\x1b[47;1m"
@@ -163,7 +168,15 @@ struct ascii* set_ascii(VENDOR cpuVendor, STYLE style, struct colors* cs) {
if(style == STYLE_EMPTY) {
#ifdef _WIN32
art->style = STYLE_LEGACY;
HANDLE std_handle = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD console_mode;
// Attempt to enable the VT100-processing flag
GetConsoleMode(std_handle, &console_mode);
SetConsoleMode(std_handle, console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
// Get the console mode flag again, to see if it successfully enabled it
GetConsoleMode(std_handle, &console_mode);
// Enable fancy mode if VT100-processing is enabled
art->style = (console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) ? STYLE_FANCY : STYLE_LEGACY;
#else
art->style = STYLE_FANCY;
#endif