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

heed NO_COLOR environment variable #8693

Merged
merged 1 commit into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions contrib/epee/src/mlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,21 @@ bool is_stdout_a_tty()
return is_a_tty.load(std::memory_order_relaxed);
}

static bool is_nocolor()
{
static const char *no_color_var = getenv("NO_COLOR");
static const bool no_color = no_color_var && *no_color_var; // apparently, NO_COLOR=0 means no color too (as per no-color.org)
Copy link
Contributor

@SChernykh SChernykh Jan 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use strtol to check the actual numeric value if you want to compare with 0.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to compare with 0, 0 apparently means the same as 1 or yes or no (according to the page). The contents do not matter, only their existence.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've just read the docs too and it explicitly say "any non-empty string", so it's better to leave the code as is.

return no_color;
}

void set_console_color(int color, bool bright)
{
if (!is_stdout_a_tty())
return;

if (is_nocolor())
return;

switch(color)
{
case console_color_default:
Expand Down Expand Up @@ -461,6 +471,9 @@ void reset_console_color() {
if (!is_stdout_a_tty())
return;

if (is_nocolor())
return;

#ifdef WIN32
HANDLE h_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h_stdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
Expand Down
5 changes: 5 additions & 0 deletions external/easylogging++/easylogging++.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ static el::Color colorFromLevel(el::Level level)

static void setConsoleColor(el::Color color, bool bright)
{
static const char *no_color_var = getenv("NO_COLOR");
static const bool no_color = no_color_var && *no_color_var; // apparently, NO_COLOR=0 means no color too (as per no-color.org)
if (no_color)
return;

#if ELPP_OS_WINDOWS
HANDLE h_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
switch (color)
Expand Down