Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache results of shouldUseColors() #1081

Merged
merged 1 commit into from
May 8, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions src/host/term_textColor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
static const char *getenvOrFallback(const char *var, const char *fallback)
{
const char *value = getenv(var);
if (value)
return value;
else
return fallback;
return (value != NULL) ? value : 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 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()
Expand Down