Don't use color escape codes if terminal doesn't support them in ansicolor_sink

This commit is contained in:
gabime
2017-05-16 23:35:01 +03:00
parent e9b8286714
commit bd25f59a42
2 changed files with 120 additions and 84 deletions

View File

@@ -12,6 +12,7 @@
#include <string>
#include <chrono>
#include <thread>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
@@ -401,6 +402,32 @@ inline int pid()
}
// Detrmine if the terminal supports colors
// Source: https://github.com/agauniyal/rang/
bool is_color_terminal()
{
#ifdef _WIN32
return true;
#else
static constexpr const char* Terms[] = {
"ansi", "color", "console", "cygwin", "gnome", "konsole", "kterm",
"linux", "msys", "putty", "rxvt", "screen", "vt100", "xterm"
};
const char *env_p = std::getenv("TERM");
if (env_p == nullptr) {
return false;
}
static const bool result = std::any_of(
std::begin(Terms), std::end(Terms), [&](const char* term) {
return std::strstr(env_p, term) != nullptr;
});
#endif
}
} //os
} //details
} //spdlog