Skip to content

Commit

Permalink
Alternative approach to Windows color initialization (doctest#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saalvage authored Jan 5, 2022
1 parent 84d5efd commit 86d0e2e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 50 deletions.
43 changes: 18 additions & 25 deletions doctest/doctest.h
Original file line number Diff line number Diff line change
Expand Up @@ -4025,29 +4025,6 @@ namespace {
return suiteOrderComparator(lhs, rhs);
}

#ifdef DOCTEST_CONFIG_COLORS_WINDOWS
HANDLE g_stdoutHandle;
WORD g_origFgAttrs;
WORD g_origBgAttrs;
bool g_attrsInited = false;

int colors_init() {
if(!g_attrsInited) {
g_stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
g_attrsInited = true;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(g_stdoutHandle, &csbiInfo);
g_origFgAttrs = csbiInfo.wAttributes & ~(BACKGROUND_GREEN | BACKGROUND_RED |
BACKGROUND_BLUE | BACKGROUND_INTENSITY);
g_origBgAttrs = csbiInfo.wAttributes & ~(FOREGROUND_GREEN | FOREGROUND_RED |
FOREGROUND_BLUE | FOREGROUND_INTENSITY);
}
return 0;
}

int dummy_init_console_colors = colors_init();
#endif // DOCTEST_CONFIG_COLORS_WINDOWS

DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations")
void color_to_stream(std::ostream& s, Color::Enum code) {
static_cast<void>(s); // for DOCTEST_CONFIG_COLORS_NONE or DOCTEST_CONFIG_COLORS_WINDOWS
Expand Down Expand Up @@ -4084,7 +4061,23 @@ namespace {
(_isatty(_fileno(stdout)) == false && getContextOptions()->force_colors == false))
return;

#define DOCTEST_SET_ATTR(x) SetConsoleTextAttribute(g_stdoutHandle, x | g_origBgAttrs)
static struct ConsoleHelper {
HANDLE stdoutHandle;
WORD origFgAttrs;
WORD origBgAttrs;

ConsoleHelper() {
stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(stdoutHandle, &csbiInfo);
origFgAttrs = csbiInfo.wAttributes & ~(BACKGROUND_GREEN | BACKGROUND_RED |
BACKGROUND_BLUE | BACKGROUND_INTENSITY);
origBgAttrs = csbiInfo.wAttributes & ~(FOREGROUND_GREEN | FOREGROUND_RED |
FOREGROUND_BLUE | FOREGROUND_INTENSITY);
}
} ch;

#define DOCTEST_SET_ATTR(x) SetConsoleTextAttribute(ch.stdoutHandle, x | ch.origBgAttrs)

// clang-format off
switch (code) {
Expand All @@ -4101,7 +4094,7 @@ namespace {
case Color::BrightWhite: DOCTEST_SET_ATTR(FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE); break;
case Color::None:
case Color::Bright: // invalid
default: DOCTEST_SET_ATTR(g_origFgAttrs);
default: DOCTEST_SET_ATTR(ch.origFgAttrs);
}
// clang-format on
#endif // DOCTEST_CONFIG_COLORS_WINDOWS
Expand Down
43 changes: 18 additions & 25 deletions doctest/parts/doctest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1184,29 +1184,6 @@ namespace {
return suiteOrderComparator(lhs, rhs);
}

#ifdef DOCTEST_CONFIG_COLORS_WINDOWS
HANDLE g_stdoutHandle;
WORD g_origFgAttrs;
WORD g_origBgAttrs;
bool g_attrsInited = false;

int colors_init() {
if(!g_attrsInited) {
g_stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
g_attrsInited = true;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(g_stdoutHandle, &csbiInfo);
g_origFgAttrs = csbiInfo.wAttributes & ~(BACKGROUND_GREEN | BACKGROUND_RED |
BACKGROUND_BLUE | BACKGROUND_INTENSITY);
g_origBgAttrs = csbiInfo.wAttributes & ~(FOREGROUND_GREEN | FOREGROUND_RED |
FOREGROUND_BLUE | FOREGROUND_INTENSITY);
}
return 0;
}

int dummy_init_console_colors = colors_init();
#endif // DOCTEST_CONFIG_COLORS_WINDOWS

DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations")
void color_to_stream(std::ostream& s, Color::Enum code) {
static_cast<void>(s); // for DOCTEST_CONFIG_COLORS_NONE or DOCTEST_CONFIG_COLORS_WINDOWS
Expand Down Expand Up @@ -1243,7 +1220,23 @@ namespace {
(_isatty(_fileno(stdout)) == false && getContextOptions()->force_colors == false))
return;

#define DOCTEST_SET_ATTR(x) SetConsoleTextAttribute(g_stdoutHandle, x | g_origBgAttrs)
static struct ConsoleHelper {
HANDLE stdoutHandle;
WORD origFgAttrs;
WORD origBgAttrs;

ConsoleHelper() {
stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(stdoutHandle, &csbiInfo);
origFgAttrs = csbiInfo.wAttributes & ~(BACKGROUND_GREEN | BACKGROUND_RED |
BACKGROUND_BLUE | BACKGROUND_INTENSITY);
origBgAttrs = csbiInfo.wAttributes & ~(FOREGROUND_GREEN | FOREGROUND_RED |
FOREGROUND_BLUE | FOREGROUND_INTENSITY);
}
} ch;

#define DOCTEST_SET_ATTR(x) SetConsoleTextAttribute(ch.stdoutHandle, x | ch.origBgAttrs)

// clang-format off
switch (code) {
Expand All @@ -1260,7 +1253,7 @@ namespace {
case Color::BrightWhite: DOCTEST_SET_ATTR(FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE); break;
case Color::None:
case Color::Bright: // invalid
default: DOCTEST_SET_ATTR(g_origFgAttrs);
default: DOCTEST_SET_ATTR(ch.origFgAttrs);
}
// clang-format on
#endif // DOCTEST_CONFIG_COLORS_WINDOWS
Expand Down

0 comments on commit 86d0e2e

Please sign in to comment.