Skip to content

Commit c283c03

Browse files
committed
Set the default control mode based on environment variables: NO_COLOR and CLICOLOR_FORCE, in line with https://bixense.com/clicolors/
1 parent 22345aa commit c283c03

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

include/rang.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ enum class control { // Behaviour of rang function calls
113113
Force = 2 // force ansi color output to non terminal streams
114114
};
115115
// Use rang::setControlMode to set rang control mode
116+
// The default can be set by environment variables: https://bixense.com/clicolors/
117+
// NO_COLOR to disable color output, CLICOLOR_FORCE to force color output
116118

117119
enum class winTerm { // Windows Terminal Mode
118120
Auto = 0, // (Default) automatically detects wheter Ansi or Native API
@@ -124,9 +126,21 @@ enum class winTerm { // Windows Terminal Mode
124126

125127
namespace rang_implementation {
126128

129+
// https://bixense.com/clicolors/
130+
inline control defaultControlMode() noexcept
131+
{
132+
if (std::getenv("NO_COLOR") != nullptr) {
133+
return control::Off;
134+
}
135+
if (std::getenv("CLICOLOR_FORCE") != nullptr) {
136+
return control::Force;
137+
}
138+
return control::Auto;
139+
}
140+
127141
inline std::atomic<control> &controlMode() noexcept
128142
{
129-
static std::atomic<control> value(control::Auto);
143+
static std::atomic<control> value(defaultControlMode());
130144
return value;
131145
}
132146

0 commit comments

Comments
 (0)