Skip to content

Commit a9fede2

Browse files
committed
Added support for NO_COLOR.
1 parent 06edde2 commit a9fede2

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

sgr.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ namespace priv
6464

6565
static sgrmode mode = sgrmode::ONLY_IF_TTY;
6666

67+
struct query_env_nocolor {
68+
query_env_nocolor() {
69+
if ( getenv("NO_COLOR") != nullptr )
70+
mode = sgrmode::DISABLED;
71+
}
72+
};
73+
74+
query_env_nocolor q;
75+
6776
template<typename CharT, typename Traits = std::char_traits<CharT>>
6877
bool sgr_enabled(std::basic_ostream<CharT, Traits>& os)
6978
{
@@ -353,7 +362,7 @@ namespace priv
353362
inline std::ostream&
354363
operator<<(std::ostream& os, const priv::fg_rgb_manip& rm)
355364
{
356-
if ( priv::isatty(os) && !rm.noop )
365+
if ( priv::sgr_enabled(os) and priv::isatty(os) and !rm.noop )
357366
{
358367
if (rm.r < 0 || rm.r > 5) return os;
359368
if (rm.g < 0 || rm.g > 5) return os;
@@ -374,7 +383,7 @@ rgbfg(int r, int g, int b) {
374383
inline std::ostream&
375384
operator<<(std::ostream& os, const priv::bg_rgb_manip& rm)
376385
{
377-
if ( priv::isatty(os) && !rm.noop )
386+
if ( priv::sgr_enabled(os) and priv::isatty(os) and !rm.noop )
378387
{
379388
if (rm.r < 0 || rm.r > 5) return os;
380389
if (rm.g < 0 || rm.g > 5) return os;

0 commit comments

Comments
 (0)