aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
authorLion <development@kortlepel.com>2022-08-14 21:27:28 +0300
committerGitHub <noreply@github.com>2022-08-14 20:27:28 +0200
commit2690585e992474cc458e274250c14b2a65a6b926 (patch)
treec202524b77b61c0f9be60a4019bf97e549a672ae /src/main.cpp
parent760242c076921775733c3dd039d3aaabcc64125f (diff)
downloadminetest-2690585e992474cc458e274250c14b2a65a6b926.tar.xz
Add handling of environment variables to control terminal/logging colors (#12641)
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp
index ebd1f740e..d927b141b 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -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);