forked from Mirrorlandia_minetest/minetest
Add handling of environment variables to control terminal/logging colors (#12641)
This commit is contained in:
parent
760242c076
commit
2690585e99
29
src/main.cpp
29
src/main.cpp
@ -77,12 +77,17 @@ extern "C" {
|
|||||||
#define DEBUGFILE "debug.txt"
|
#define DEBUGFILE "debug.txt"
|
||||||
#define DEFAULT_SERVER_PORT 30000
|
#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;
|
typedef std::map<std::string, ValueSpec> OptionList;
|
||||||
|
|
||||||
/**********************************************************************
|
/**********************************************************************
|
||||||
* Private functions
|
* Private functions
|
||||||
**********************************************************************/
|
**********************************************************************/
|
||||||
|
|
||||||
|
static void get_env_opts(Settings &args);
|
||||||
static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args);
|
static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args);
|
||||||
static void set_allowed_options(OptionList *allowed_options);
|
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);
|
g_logger.addOutputMaxLevel(&stderr_output, LL_ACTION);
|
||||||
|
|
||||||
Settings cmd_args;
|
Settings cmd_args;
|
||||||
|
get_env_opts(cmd_args);
|
||||||
bool cmd_args_ok = get_cmdline_opts(argc, argv, &cmd_args);
|
bool cmd_args_ok = get_cmdline_opts(argc, argv, &cmd_args);
|
||||||
if (!cmd_args_ok
|
if (!cmd_args_ok
|
||||||
|| cmd_args.getFlag("help")
|
|| 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)
|
static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args)
|
||||||
{
|
{
|
||||||
set_allowed_options(&allowed_options);
|
set_allowed_options(&allowed_options);
|
||||||
|
Loading…
Reference in New Issue
Block a user