From 649aa295523885723ea4c9d56148ebe2b9b8a239 Mon Sep 17 00:00:00 2001 From: Zulkarnine Mahmud Date: Tue, 20 Jun 2017 15:40:53 +0900 Subject: [PATCH 1/7] 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. --- googletest/src/gtest.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 3a18f25d..1e6afb28 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -2979,13 +2979,15 @@ 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; - + // 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 // 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); + GetColorAttribute(color) | existing_bg | FOREGROUND_INTENSITY); vprintf(fmt, args); fflush(stdout); From 365df11427eb40e6458adee2b5ace7191b883efa Mon Sep 17 00:00:00 2001 From: Zulkarnine Mahmud Date: Wed, 21 Jun 2017 09:17:51 +0900 Subject: [PATCH 2/7] Add background_mask instead of using magic number --- googletest/src/gtest.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 1e6afb28..1ac2d6a1 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -2980,7 +2980,8 @@ void ColoredPrintf(GTestColor color, const char* fmt, ...) { GetConsoleScreenBufferInfo(stdout_handle, &buffer_info); const WORD old_color_attrs = buffer_info.wAttributes; // Let's reuse the BG - const WORD existing_bg = old_color_attrs & 0x00F0; + const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY; + const WORD existing_bg = old_color_attrs & background_mask; // We need to flush the stream buffers into the console before each // SetConsoleTextAttribute call lest it affect the text that is already From 271fb8ff5ed869d7cf38051c4cb0f54998eea6ac Mon Sep 17 00:00:00 2001 From: Zulkarnine Mahmud Date: Thu, 22 Jun 2017 11:06:17 +0900 Subject: [PATCH 3/7] Fix a problem when bg_color == fg_color Invert the intensity bit if the background_color == foreground_color --- googletest/src/gtest.cc | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 1ac2d6a1..5f41a110 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -2979,16 +2979,37 @@ 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; + // Let's reuse the BG - const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY; + const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY; + 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; + +#if 1 // do we really need to waste these cpu cycles every time? + int bg_bitOffset = 0; + WORD bg_mask = background_mask; + while((bg_mask & 0x01) == 0x00) { + bg_mask >>= 1; + ++bg_bitOffset; + } +#else + const int bg_bitOffset = 4; +#endif + if (((new_color & background_mask) >> bg_bitOffset) == (new_color & foreground_mask)) { + //revert intensity + new_color ^= FOREGROUND_INTENSITY; + } + // 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) | existing_bg | FOREGROUND_INTENSITY); + SetConsoleTextAttribute(stdout_handle, new_color); + vprintf(fmt, args); fflush(stdout); From 26b7ac3b1888b096d982bd1e04441e06ad1ce6e3 Mon Sep 17 00:00:00 2001 From: Zulkarnine Mahmud Date: Thu, 29 Jun 2017 09:47:52 +0900 Subject: [PATCH 4/7] Add helper functions for text color calculation --- googletest/src/gtest.cc | 49 +++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 5f41a110..7452391e 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -2894,6 +2894,30 @@ WORD GetColorAttribute(GTestColor color) { } } +int GetBgOffset(WORD background_mask) { + if (background_mask == 0) return 0; //let's not fall into infinite loop + + int bitOffset = 0; + while((background_mask & 1) == 0) { + background_mask >>= 1; + ++bitOffset; + } + return bitOffset; +} + +WORD GetNewColor(const GTestColor color, const 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 = GetBgOffset(background_mask); //it does not change + + if (((new_color & background_mask) >> bg_bitOffset) == (new_color & foreground_mask)) new_color ^= FOREGROUND_INTENSITY; //revert intensity + return new_color; +} + #else // Returns the ANSI color code for the given color. COLOR_DEFAULT is @@ -2979,31 +3003,8 @@ 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; - - // Let's reuse the BG - const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY; - 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; - -#if 1 // do we really need to waste these cpu cycles every time? - int bg_bitOffset = 0; - WORD bg_mask = background_mask; - while((bg_mask & 0x01) == 0x00) { - bg_mask >>= 1; - ++bg_bitOffset; - } -#else - const int bg_bitOffset = 4; -#endif + const WORD new_color = GetNewColor(color, old_color_attrs); - if (((new_color & background_mask) >> bg_bitOffset) == (new_color & foreground_mask)) { - //revert intensity - new_color ^= FOREGROUND_INTENSITY; - } - // 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. From 6a75e3c169d2ab0fb1ff096b7922375f08d3359c Mon Sep 17 00:00:00 2001 From: Zulkarnine Mahmud Date: Thu, 29 Jun 2017 09:52:41 +0900 Subject: [PATCH 5/7] Remove unnecessary const --- googletest/src/gtest.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 7452391e..30e00946 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -2905,7 +2905,7 @@ int GetBgOffset(WORD background_mask) { return bitOffset; } -WORD GetNewColor(const GTestColor color, const WORD old_color_attrs) { +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; From a6b146dfddb9462b901b1eb45ee0d6d761f021bf Mon Sep 17 00:00:00 2001 From: Zulkarnine Mahmud Date: Sat, 15 Jul 2017 17:44:18 +0900 Subject: [PATCH 6/7] Fix assumption for foreground bit offset --- googletest/src/gtest.cc | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 30e00946..9bcb1732 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -2895,7 +2895,7 @@ WORD GetColorAttribute(GTestColor color) { } int GetBgOffset(WORD background_mask) { - if (background_mask == 0) return 0; //let's not fall into infinite loop + if (background_mask == 0) return 0; int bitOffset = 0; while((background_mask & 1) == 0) { @@ -2905,6 +2905,16 @@ int GetBgOffset(WORD background_mask) { return bitOffset; } +int GetFgOffset(WORD foreground_mask) { + if (foreground_mask == 0) return 0; + + int bitOffset = 0; + while((foreground_mask & 1) == 0) { + foreground_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; @@ -2912,9 +2922,12 @@ WORD GetNewColor(GTestColor color, WORD old_color_attrs) { const WORD existing_bg = old_color_attrs & background_mask; WORD new_color = GetColorAttribute(color) | existing_bg | FOREGROUND_INTENSITY; - static const int bg_bitOffset = GetBgOffset(background_mask); //it does not change + static const int bg_bitOffset = GetBgOffset(background_mask); + static const int fg_bitOffset = GetFgOffset(foreground_mask); - if (((new_color & background_mask) >> bg_bitOffset) == (new_color & foreground_mask)) new_color ^= FOREGROUND_INTENSITY; //revert intensity + if (((new_color & background_mask) >> bg_bitOffset) == ((new_color & foreground_mask) >> fg_bitOffset)) { + new_color ^= FOREGROUND_INTENSITY; //invert intensity + } return new_color; } From 2960aa54e219b21ec9dbde0dc4692ef3010a9970 Mon Sep 17 00:00:00 2001 From: Zulkarnine Mahmud Date: Sun, 16 Jul 2017 21:59:26 +0900 Subject: [PATCH 7/7] Remove duplicate code --- googletest/src/gtest.cc | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 9bcb1732..fcaf156e 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -2894,27 +2894,17 @@ WORD GetColorAttribute(GTestColor color) { } } -int GetBgOffset(WORD background_mask) { - if (background_mask == 0) return 0; +int GetBitOffset(WORD color_mask) { + if (color_mask == 0) return 0; int bitOffset = 0; - while((background_mask & 1) == 0) { - background_mask >>= 1; + while((color_mask & 1) == 0) { + color_mask >>= 1; ++bitOffset; } return bitOffset; } -int GetFgOffset(WORD foreground_mask) { - if (foreground_mask == 0) return 0; - - int bitOffset = 0; - while((foreground_mask & 1) == 0) { - foreground_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; @@ -2922,8 +2912,8 @@ WORD GetNewColor(GTestColor color, WORD old_color_attrs) { const WORD existing_bg = old_color_attrs & background_mask; WORD new_color = GetColorAttribute(color) | existing_bg | FOREGROUND_INTENSITY; - static const int bg_bitOffset = GetBgOffset(background_mask); - static const int fg_bitOffset = GetFgOffset(foreground_mask); + 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