Move Windows implementation of print_colored to a separate branch

as it needs further testing.
This commit is contained in:
Victor Zverovich
2015-02-19 07:54:59 -08:00
parent a92205be9c
commit 53010624a0
2 changed files with 0 additions and 36 deletions

View File

@ -119,20 +119,7 @@ struct IntChecker<true> {
} }
}; };
#ifdef _WIN32
const uint8_t WIN32_COLORS[] = {
0,
FOREGROUND_RED | FOREGROUND_INTENSITY,
FOREGROUND_GREEN | FOREGROUND_INTENSITY,
FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY,
FOREGROUND_BLUE | FOREGROUND_INTENSITY,
FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY,
FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY
};
#else
const char RESET_COLOR[] = "\x1b[0m"; const char RESET_COLOR[] = "\x1b[0m";
#endif
typedef void (*FormatFunc)(fmt::Writer &, int, fmt::StringRef); typedef void (*FormatFunc)(fmt::Writer &, int, fmt::StringRef);
@ -1111,27 +1098,11 @@ FMT_FUNC void fmt::print(std::ostream &os, StringRef format_str, ArgList args) {
} }
FMT_FUNC void fmt::print_colored(Color c, StringRef format, ArgList args) { FMT_FUNC void fmt::print_colored(Color c, StringRef format, ArgList args) {
#ifdef _WIN32
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (handle == INVALID_HANDLE_VALUE)
FMT_THROW(WindowsError(GetLastError(), "cannot get output handle"));
CONSOLE_SCREEN_BUFFER_INFO info_con;
if (!GetConsoleScreenBufferInfo(handle, &info_con))
FMT_THROW(WindowsError(GetLastError(), "cannot get console information"));
WORD reset_color = info_con.wAttributes;
WORD color = static_cast<int>(c) >= ARRAYSIZE(WIN32_COLORS) ? reset_color : WIN32_COLORS[c];
if (!SetConsoleTextAttribute(handle, color))
FMT_THROW(WindowsError(GetLastError(), "cannot set console color"));
print(format, args);
if (!SetConsoleTextAttribute(handle, reset_color))
FMT_THROW(WindowsError(GetLastError(), "cannot set console color"));
#else
char escape[] = "\x1b[30m"; char escape[] = "\x1b[30m";
escape[3] = '0' + static_cast<char>(c); escape[3] = '0' + static_cast<char>(c);
std::fputs(escape, stdout); std::fputs(escape, stdout);
print(format, args); print(format, args);
std::fputs(RESET_COLOR, stdout); std::fputs(RESET_COLOR, stdout);
#endif
} }
FMT_FUNC int fmt::fprintf(std::FILE *f, StringRef format, ArgList args) { FMT_FUNC int fmt::fprintf(std::FILE *f, StringRef format, ArgList args) {

View File

@ -1377,15 +1377,8 @@ TEST(FormatTest, Print) {
#if FMT_USE_FILE_DESCRIPTORS #if FMT_USE_FILE_DESCRIPTORS
TEST(FormatTest, PrintColored) { TEST(FormatTest, PrintColored) {
#if _WIN32
//EXPECT_WRITE(stdout, fmt::print_colored(fmt::RED, "Hello, {}!\n", "world"),
// "Hello, world!\n");
//EXPECT_WRITE(stdout, fmt::print_colored(static_cast<fmt::Color>(29673),
// "Hello, {}!\n", "world"), "Hello, world!\n");
#else
EXPECT_WRITE(stdout, fmt::print_colored(fmt::RED, "Hello, {}!\n", "world"), EXPECT_WRITE(stdout, fmt::print_colored(fmt::RED, "Hello, {}!\n", "world"),
"\x1b[31mHello, world!\n\x1b[0m"); "\x1b[31mHello, world!\n\x1b[0m");
#endif
} }
#endif #endif