From 0f70db1dc18144874814eddda021c2ecc519bb1a Mon Sep 17 00:00:00 2001 From: Timo Ewalds Date: Sat, 22 Feb 2025 13:07:33 +0000 Subject: [PATCH] Set the default control mode based on environment variables: NO_COLOR and CLICOLOR_FORCE, in line with https://bixense.com/clicolors/ --- include/rang.hpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/include/rang.hpp b/include/rang.hpp index 831eda4..19c5d95 100644 --- a/include/rang.hpp +++ b/include/rang.hpp @@ -113,6 +113,8 @@ enum class control { // Behaviour of rang function calls Force = 2 // force ansi color output to non terminal streams }; // Use rang::setControlMode to set rang control mode +// The default can be set by environment variables: https://bixense.com/clicolors/ +// NO_COLOR to disable color output, CLICOLOR_FORCE to force color output enum class winTerm { // Windows Terminal Mode Auto = 0, // (Default) automatically detects wheter Ansi or Native API @@ -126,7 +128,16 @@ namespace rang_implementation { inline std::atomic &controlMode() noexcept { - static std::atomic value(control::Auto); + static std::atomic value = [] { + // https://bixense.com/clicolors/ + if (std::getenv("NO_COLOR") != nullptr) { + return control::Off; + } + if (std::getenv("CLICOLOR_FORCE") != nullptr) { + return control::Force; + } + return control::Auto; + }(); return value; }