Only use color in supported terminal.

Test: compile
Change-Id: Ibb962597e065ebb90a53dd8bc62c4b774a2e4f10
This commit is contained in:
Haibo Huang 2018-07-16 09:41:53 -07:00
parent a9713035ba
commit c1ef5eb034

View file

@ -78,15 +78,39 @@ static constexpr const char* COLOR_RED = "\033[0;31m";
static constexpr const char* COLOR_GREEN = "\033[0;32m";
static constexpr const char* COLOR_YELLOW = "\033[0;33m";
static void ColoredPrintf(const char* const color, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
static bool ShouldUseColor() {
bool stdout_is_tty = isatty(STDOUT_FILENO) != 0;
if (!stdout_is_tty) {
return false;
}
const auto& gtest_color = ::testing::GTEST_FLAG(color);
if (gtest_color == "yes" || gtest_color == "true" || gtest_color == "t") {
return true;
}
if (gtest_color != "auto") {
return false;
}
const char* const term = getenv("COLORTERM");
return term != nullptr && term[0] != 0;
}
static void ColoredPrintf(const char* const color, const char* fmt, ...) {
static const bool use_color = ShouldUseColor();
va_list args;
va_start(args, fmt);
if (!use_color) {
vprintf(fmt, args);
} else {
printf("%s", color);
vprintf(fmt, args);
printf("%s", COLOR_RESET);
}
va_end(args);
va_end(args);
}
constexpr int DEFAULT_GLOBAL_TEST_RUN_DEADLINE_MS = 90000;