Merge pull request #1081 from tvandijck/cache-canUseColors

cache results of shouldUseColors()
This commit is contained in:
Samuel Surtees 2018-05-08 20:56:20 +10:00 committed by GitHub
commit 4911f14ac8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,32 +6,39 @@
#include "premake.h"
#if PLATFORM_WINDOWS
#include <io.h>
#endif
#if PLATFORM_POSIX
static int s_currentColor = -1;
#endif
int canUseColors()
{
#if PLATFORM_WINDOWS
int canUseColors() {
return 1;
}
return _isatty(_fileno(stdout));
#else
int canUseColors() {
return isatty(1);
}
#endif
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') && canUseColors())
|| getenvOrFallback("CLICOLOR_FORCE", "0")[0] != '0';
static const char *getenvOrFallback(const char *var, const char *fallback)
{
const char *value = getenv(var);
return (value != NULL) ? value : fallback;
}
static int s_shouldUseColor = -1;
static int shouldUseColors()
{
if (s_shouldUseColor < 0)
{
// CLICOLOR* documented at: http://bixense.com/clicolors/
s_shouldUseColor = ((getenvOrFallback("CLICOLOR", "1")[0] != '0') && canUseColors())
|| getenvOrFallback("CLICOLOR_FORCE", "0")[0] != '0';
}
return s_shouldUseColor;
}
int term_doGetTextColor()