mirror of
https://github.com/minetest/minetest.git
synced 2024-11-23 16:13:46 +01:00
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 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);
|
||||
|
Loading…
Reference in New Issue
Block a user