Do not output colors e.g. into a pipe, unless forced.

Use the de-factoish environment variables CLICOLOR(_FORCE) to override default color behavior.
This commit is contained in:
Lauri Nurmi 2018-04-26 12:52:30 +03:00 committed by Lauri Nurmi
parent 8d0dce9c65
commit ba34e3e28a

View File

@ -10,6 +10,22 @@
static int s_currentColor = -1;
#endif
#if !PLATFORM_WINDOWS
static const char *getenvOrFallback(const char *var, const char *fallback) {
const char *value = getenv(var);
if (value)
return value;
else
return fallback;
}
static int shouldUseColors() {
// CLICOLOR* documented at: http://bixense.com/clicolors/
return ((getenvOrFallback("CLICOLOR", "1")[0] != '0') && isatty(1))
|| getenvOrFallback("CLICOLOR_FORCE", "0")[0] != '0';
}
#endif
int term_doGetTextColor()
{
#if PLATFORM_WINDOWS
@ -31,6 +47,11 @@ void term_doSetTextColor(int color)
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), (WORD)color);
}
#else
// Do not output colors e.g. into a pipe, unless forced.
if (!shouldUseColors())
return;
s_currentColor = color;
const char* colorTable[] =