Add handling of environment variables to control terminal/logging colors (#12641)

This commit is contained in:
Lion 2022-08-14 21:27:28 +03:00 committed by GitHub
parent 760242c076
commit 2690585e99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -77,12 +77,17 @@ extern "C" {
#define DEBUGFILE "debug.txt"
#define DEFAULT_SERVER_PORT 30000
#define ENV_NO_COLOR "NO_COLOR"
#define ENV_CLICOLOR "CLICOLOR"
#define ENV_CLICOLOR_FORCE "CLICOLOR_FORCE"
typedef std::map<std::string, ValueSpec> OptionList;
/**********************************************************************
* Private functions
**********************************************************************/
static void get_env_opts(Settings &args);
static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args);
static void set_allowed_options(OptionList *allowed_options);
@ -136,6 +141,7 @@ int main(int argc, char *argv[])
g_logger.addOutputMaxLevel(&stderr_output, LL_ACTION);
Settings cmd_args;
get_env_opts(cmd_args);
bool cmd_args_ok = get_cmdline_opts(argc, argv, &cmd_args);
if (!cmd_args_ok
|| cmd_args.getFlag("help")
@ -269,6 +275,29 @@ int main(int argc, char *argv[])
*****************************************************************************/
static void get_env_opts(Settings &args)
{
// CLICOLOR is a de-facto standard option for colors <https://bixense.com/clicolors/>
// CLICOLOR != 0: ANSI colors are supported (auto-detection, this is the default)
// CLICOLOR == 0: ANSI colors are NOT supported
const char *clicolor = std::getenv(ENV_CLICOLOR);
if (clicolor && std::string(clicolor) == "0") {
args.set("color", "never");
}
// NO_COLOR only specifies that no color is allowed.
// Implemented according to <http://no-color.org/>
const char *no_color = std::getenv(ENV_NO_COLOR);
if (no_color && no_color[0]) {
args.set("color", "never");
}
// CLICOLOR_FORCE is another option, which should turn on colors "no matter what".
const char *clicolor_force = std::getenv(ENV_CLICOLOR_FORCE);
if (clicolor_force && std::string(clicolor_force) != "0") {
// should ALWAYS have colors, so we ignore tty (no "auto")
args.set("color", "always");
}
}
static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args)
{
set_allowed_options(&allowed_options);