try harder to emit console colors

master
Andrew Kelley 2017-10-14 20:31:47 -04:00
parent fd7654e4e8
commit ba405ed59b
1 changed files with 21 additions and 0 deletions

View File

@ -948,6 +948,12 @@ static void set_color_posix(TermColor color) {
} }
} }
#if defined(ZIG_OS_WINDOWS)
bool got_orig_console_attrs = false;
WORD original_console_attributes = FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE;
#endif
void os_stderr_set_color(TermColor color) { void os_stderr_set_color(TermColor color) {
#if defined(ZIG_OS_WINDOWS) #if defined(ZIG_OS_WINDOWS)
if (is_stderr_cyg_pty()) { if (is_stderr_cyg_pty()) {
@ -965,22 +971,37 @@ void os_stderr_set_color(TermColor color) {
mode_flags |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; mode_flags |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(stderr_handle, mode_flags); SetConsoleMode(stderr_handle, mode_flags);
} }
if (!got_orig_console_attrs) {
got_orig_console_attrs = true;
CONSOLE_SCREEN_BUFFER_INFO info;
if (GetConsoleScreenBufferInfo(stderr_handle, &info)) {
original_console_attributes = info.wAttributes;
}
}
DWORD chars_written; DWORD chars_written;
switch (color) { switch (color) {
case TermColorRed: case TermColorRed:
SetConsoleTextAttribute(stderr_handle, FOREGROUND_RED|FOREGROUND_INTENSITY);
WriteConsole(stderr_handle, VT_RED, strlen(VT_RED), &chars_written, NULL); WriteConsole(stderr_handle, VT_RED, strlen(VT_RED), &chars_written, NULL);
break; break;
case TermColorGreen: case TermColorGreen:
SetConsoleTextAttribute(stderr_handle, FOREGROUND_GREEN|FOREGROUND_INTENSITY);
WriteConsole(stderr_handle, VT_GREEN, strlen(VT_GREEN), &chars_written, NULL); WriteConsole(stderr_handle, VT_GREEN, strlen(VT_GREEN), &chars_written, NULL);
break; break;
case TermColorCyan: case TermColorCyan:
SetConsoleTextAttribute(stderr_handle, FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
WriteConsole(stderr_handle, VT_CYAN, strlen(VT_CYAN), &chars_written, NULL); WriteConsole(stderr_handle, VT_CYAN, strlen(VT_CYAN), &chars_written, NULL);
break; break;
case TermColorWhite: case TermColorWhite:
SetConsoleTextAttribute(stderr_handle,
FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
WriteConsole(stderr_handle, VT_WHITE, strlen(VT_WHITE), &chars_written, NULL); WriteConsole(stderr_handle, VT_WHITE, strlen(VT_WHITE), &chars_written, NULL);
break; break;
case TermColorReset: case TermColorReset:
{ {
SetConsoleTextAttribute(stderr_handle, original_console_attributes);
WriteConsole(stderr_handle, VT_RESET, strlen(VT_RESET), &chars_written, NULL); WriteConsole(stderr_handle, VT_RESET, strlen(VT_RESET), &chars_written, NULL);
DWORD mode_flags = 0; DWORD mode_flags = 0;