Added support for NO_COLOR environment variable

This commit is contained in:
Bob Polis 2023-08-08 15:57:34 +02:00
parent 969984213d
commit 4a3e59159e
2 changed files with 40 additions and 31 deletions

View File

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

View File

@ -5,6 +5,7 @@
namespace sc { namespace sc {
namespace io { namespace io {
bool should_color(const std::ostream& out);
bool isatty_ostream(const std::ostream& out); bool isatty_ostream(const std::ostream& out);
std::ostream& hide_cursor(std::ostream& out); std::ostream& hide_cursor(std::ostream& out);