From 4fe8e389a7c8acd9db9e3cbf26d6f7e4bd8e93ae Mon Sep 17 00:00:00 2001 From: Lukas Hermanns Date: Sun, 20 Aug 2017 14:11:50 +0200 Subject: [PATCH] Fix: background color was erroneously reset on Win32 platform. Fix: background color was erroneously reset on Win32 platform. --- source/print.cpp | 61 +++++++++++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/source/print.cpp b/source/print.cpp index ff73b3d..8abbf12 100644 --- a/source/print.cpp +++ b/source/print.cpp @@ -35,63 +35,52 @@ clr::blue::operator const char*() { return "\x1b[34m"; } namespace libspirv { -clr::reset::operator const char*() { - const DWORD color = 0Xf; - HANDLE hConsole; - hConsole = GetStdHandle(STD_OUTPUT_HANDLE); - SetConsoleTextAttribute(hConsole, color); - hConsole = GetStdHandle(STD_ERROR_HANDLE); +static void SetConsoleForegroundColorPrimary(HANDLE hConsole, WORD color) +{ + // Get screen buffer information from console handle + CONSOLE_SCREEN_BUFFER_INFO bufInfo; + GetConsoleScreenBufferInfo(hConsole, &bufInfo); + + // Get background color + color |= (bufInfo.wAttributes & 0xfff0); + + // Set foreground color SetConsoleTextAttribute(hConsole, color); +} + +static void SetConsoleForegroundColor(WORD color) +{ + SetConsoleForegroundColorPrimary(GetStdHandle(STD_OUTPUT_HANDLE), color); + SetConsoleForegroundColorPrimary(GetStdHandle(STD_ERROR_HANDLE), color); +} + +clr::reset::operator const char*() { + SetConsoleForegroundColor(0xf); return ""; } clr::grey::operator const char*() { - const DWORD color = 0x8; - HANDLE hConsole; - hConsole = GetStdHandle(STD_OUTPUT_HANDLE); - SetConsoleTextAttribute(hConsole, color); - hConsole = GetStdHandle(STD_ERROR_HANDLE); - SetConsoleTextAttribute(hConsole, color); + SetConsoleForegroundColor(FOREGROUND_INTENSITY); return ""; } clr::red::operator const char*() { - const DWORD color = 0x4; - HANDLE hConsole; - hConsole = GetStdHandle(STD_OUTPUT_HANDLE); - SetConsoleTextAttribute(hConsole, color); - hConsole = GetStdHandle(STD_ERROR_HANDLE); - SetConsoleTextAttribute(hConsole, color); + SetConsoleForegroundColor(FOREGROUND_RED); return ""; } clr::green::operator const char*() { - const DWORD color = 0x2; - HANDLE hConsole; - hConsole = GetStdHandle(STD_OUTPUT_HANDLE); - SetConsoleTextAttribute(hConsole, color); - hConsole = GetStdHandle(STD_ERROR_HANDLE); - SetConsoleTextAttribute(hConsole, color); + SetConsoleForegroundColor(FOREGROUND_GREEN); return ""; } clr::yellow::operator const char*() { - const DWORD color = 0x6; - HANDLE hConsole; - hConsole = GetStdHandle(STD_OUTPUT_HANDLE); - SetConsoleTextAttribute(hConsole, color); - hConsole = GetStdHandle(STD_ERROR_HANDLE); - SetConsoleTextAttribute(hConsole, color); + SetConsoleForegroundColor(FOREGROUND_RED | FOREGROUND_GREEN); return ""; } clr::blue::operator const char*() { - const DWORD color = 0x1; - HANDLE hConsole; - hConsole = GetStdHandle(STD_OUTPUT_HANDLE); - SetConsoleTextAttribute(hConsole, color); - hConsole = GetStdHandle(STD_ERROR_HANDLE); - SetConsoleTextAttribute(hConsole, color); + SetConsoleForegroundColor(FOREGROUND_BLUE); return ""; } -- 2.7.4