diff --git a/src/iomanip.cpp b/src/iomanip.cpp index 185ff17..b403bbe 100644 --- a/src/iomanip.cpp +++ b/src/iomanip.cpp @@ -1,6 +1,8 @@ #include "iomanip.hpp" #include #include +#include +#include using namespace sc::io; @@ -8,7 +10,7 @@ static std::ostream& gray(std::ostream& out, int val, int code) { if (val < 0 || val > 23) { throw std::invalid_argument("gray value out of range"); } - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[" << code << ";5;" << 232 + val << "m"; return out; } @@ -17,186 +19,192 @@ static std::ostream& rgb(std::ostream& out, int r, int g, int b, int code) { if (r < 0 || r > 5 || g < 0 || g > 5 || b < 0 || b > 5) { throw std::invalid_argument("rgb color component out of range"); } - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[" << code << ";5;" << (16 + 36 * r + 6 * g + b) << "m"; return out; } +bool sc::io::should_color(const std::ostream& out) { + char* no_color_env {getenv("NO_COLOR")}; + std::string no_color {no_color_env ? no_color_env : ""}; + return no_color != "1" && isatty_ostream(out); +} + bool sc::io::isatty_ostream(const std::ostream& out) { return (&out == &std::cout && isatty(STDOUT_FILENO)) || (&out == &std::cerr && isatty(STDERR_FILENO)); } std::ostream& sc::io::hide_cursor(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[?25l"; return out; } std::ostream& sc::io::show_cursor(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[?25h"; return out; } std::ostream& sc::io::reset(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[m"; return out; } std::ostream& sc::io::bold(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[1m"; return out; } std::ostream& sc::io::italic(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[3m"; return out; } std::ostream& sc::io::underline(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[4m"; return out; } std::ostream& sc::io::overline(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[53m"; return out; } std::ostream& sc::io::strikethru(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[9m"; return out; } std::ostream& sc::io::blinkslow(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[5m"; return out; } std::ostream& sc::io::blinkfast(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[6m"; return out; } std::ostream& sc::io::reverse(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[7m"; return out; } std::ostream& sc::io::blackf(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[30m"; return out; } std::ostream& sc::io::redf(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[31m"; return out; } std::ostream& sc::io::greenf(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[32m"; return out; } std::ostream& sc::io::yellowf(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[33m"; return out; } std::ostream& sc::io::bluef(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[34m"; return out; } std::ostream& sc::io::magentaf(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[35m"; return out; } std::ostream& sc::io::cyanf(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[36m"; return out; } std::ostream& sc::io::whitef(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[37m"; return out; } std::ostream& sc::io::defaultf(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[39m"; return out; } std::ostream& sc::io::blackb(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[40m"; return out; } std::ostream& sc::io::redb(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[41m"; return out; } std::ostream& sc::io::greenb(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[42m"; return out; } std::ostream& sc::io::yellowb(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[43m"; return out; } std::ostream& sc::io::blueb(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[44m"; return out; } std::ostream& sc::io::magentab(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[45m"; return out; } std::ostream& sc::io::cyanb(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[46m"; return out; } std::ostream& sc::io::whiteb(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[47m"; return out; } std::ostream& sc::io::defaultb(std::ostream& out) { - if (isatty_ostream(out)) + if (should_color(out)) out << "\x1b[49m"; return out; } diff --git a/src/iomanip.hpp b/src/iomanip.hpp index 0404b16..792bbac 100644 --- a/src/iomanip.hpp +++ b/src/iomanip.hpp @@ -5,6 +5,7 @@ namespace sc { namespace io { + bool should_color(const std::ostream& out); bool isatty_ostream(const std::ostream& out); std::ostream& hide_cursor(std::ostream& out);