Fix background color in ColoredPrintf

Re-use existing background color for Widows' console window.
This fixes a problem where the background color for ColoredPrintf would be BLACK even if the user's console is using a different BG color.
This commit is contained in:
Zulkarnine Mahmud 2017-06-20 15:40:53 +09:00 committed by GitHub
parent c2d90bddc6
commit 649aa29552

View File

@ -2979,13 +2979,15 @@ void ColoredPrintf(GTestColor color, const char* fmt, ...) {
CONSOLE_SCREEN_BUFFER_INFO buffer_info; CONSOLE_SCREEN_BUFFER_INFO buffer_info;
GetConsoleScreenBufferInfo(stdout_handle, &buffer_info); GetConsoleScreenBufferInfo(stdout_handle, &buffer_info);
const WORD old_color_attrs = buffer_info.wAttributes; const WORD old_color_attrs = buffer_info.wAttributes;
// Let's reuse the BG
const WORD existing_bg = old_color_attrs & 0x00F0;
// We need to flush the stream buffers into the console before each // We need to flush the stream buffers into the console before each
// SetConsoleTextAttribute call lest it affect the text that is already // SetConsoleTextAttribute call lest it affect the text that is already
// printed but has not yet reached the console. // printed but has not yet reached the console.
fflush(stdout); fflush(stdout);
SetConsoleTextAttribute(stdout_handle, SetConsoleTextAttribute(stdout_handle,
GetColorAttribute(color) | FOREGROUND_INTENSITY); GetColorAttribute(color) | existing_bg | FOREGROUND_INTENSITY);
vprintf(fmt, args); vprintf(fmt, args);
fflush(stdout); fflush(stdout);