diff options
author | Gennadiy Civil <gennadiycivil@users.noreply.github.com> | 2017-08-11 00:43:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-11 00:43:26 (GMT) |
commit | 1579064390bd8eb0f591745d907ed272a8b884b8 (patch) | |
tree | 2fa6e2cce8563d5812651ee69508469616f6b8d2 /googletest | |
parent | cfab28d59448c06dc394327f6dadee4533786dcc (diff) | |
parent | 2960aa54e219b21ec9dbde0dc4692ef3010a9970 (diff) | |
download | googletest-1579064390bd8eb0f591745d907ed272a8b884b8.zip googletest-1579064390bd8eb0f591745d907ed272a8b884b8.tar.gz googletest-1579064390bd8eb0f591745d907ed272a8b884b8.tar.bz2 |
Merge pull request #1127 from zulkarnine/patch-1
Fix background color in ColoredPrintf
Diffstat (limited to 'googletest')
-rw-r--r-- | googletest/src/gtest.cc | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 46d8d21..b6087f9 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -2895,6 +2895,33 @@ WORD GetColorAttribute(GTestColor color) { } } +int GetBitOffset(WORD color_mask) { + if (color_mask == 0) return 0; + + int bitOffset = 0; + while((color_mask & 1) == 0) { + color_mask >>= 1; + ++bitOffset; + } + return bitOffset; +} + +WORD GetNewColor(GTestColor color, WORD old_color_attrs) { + // Let's reuse the BG + static const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY; + static const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY; + const WORD existing_bg = old_color_attrs & background_mask; + + WORD new_color = GetColorAttribute(color) | existing_bg | FOREGROUND_INTENSITY; + static const int bg_bitOffset = GetBitOffset(background_mask); + static const int fg_bitOffset = GetBitOffset(foreground_mask); + + if (((new_color & background_mask) >> bg_bitOffset) == ((new_color & foreground_mask) >> fg_bitOffset)) { + new_color ^= FOREGROUND_INTENSITY; //invert intensity + } + return new_color; +} + #else // Returns the ANSI color code for the given color. COLOR_DEFAULT is @@ -2980,13 +3007,14 @@ void ColoredPrintf(GTestColor color, const char* fmt, ...) { CONSOLE_SCREEN_BUFFER_INFO buffer_info; GetConsoleScreenBufferInfo(stdout_handle, &buffer_info); const WORD old_color_attrs = buffer_info.wAttributes; - + const WORD new_color = GetNewColor(color, old_color_attrs); + // We need to flush the stream buffers into the console before each // SetConsoleTextAttribute call lest it affect the text that is already // printed but has not yet reached the console. fflush(stdout); - SetConsoleTextAttribute(stdout_handle, - GetColorAttribute(color) | FOREGROUND_INTENSITY); + SetConsoleTextAttribute(stdout_handle, new_color); + vprintf(fmt, args); fflush(stdout); |