From: Otto Moerbeek Date: Sat, 19 Feb 2022 18:28:13 +0000 (+0100) Subject: Basic abstraction for handling colored terminal output, respecting isatty(), --no... X-Git-Tag: dnsdist-1.8.0-rc1~212^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F11346%2Fhead;p=thirdparty%2Fpdns.git Basic abstraction for handling colored terminal output, respecting isatty(), --no-colors and NO_COLOR --- diff --git a/pdns/pdnsutil.cc b/pdns/pdnsutil.cc index 557f621b82..e875adf978 100644 --- a/pdns/pdnsutil.cc +++ b/pdns/pdnsutil.cc @@ -1136,7 +1136,34 @@ static int clearZone(DNSSECKeeper& dk, const DNSName &zone) { return EXIT_SUCCESS; } -static int editZone(const DNSName &zone) { +class PDNSColors +{ +public: + PDNSColors(bool nocolors) + : d_colors(!nocolors && isatty(STDOUT_FILENO) && getenv("NO_COLORS") == nullptr) + { + } + [[nodiscard]] string red() const + { + return d_colors ? "\x1b[31m" : ""; + } + [[nodiscard]] string green() const + { + return d_colors ? "\x1b[32m" : ""; + } + [[nodiscard]] string bold() const + { + return d_colors ? "\x1b[1m" : ""; + } + [[nodiscard]] string rst() const + { + return d_colors ? "\x1b[0m" : ""; + } +private: + bool d_colors; +}; + +static int editZone(const DNSName &zone, const PDNSColors& col) { UeberBackend B; DomainInfo di; DNSSECKeeper dk(&B); @@ -1234,7 +1261,7 @@ static int editZone(const DNSName &zone) { } if(checkZone(dk, B, zone, &checkrr)) { reAsk:; - cerr<getZoneRepresentation(true)<<"\033[0m"<getZoneRepresentation(true) << col.rst() <getZoneRepresentation(true)<<"\033[0m"<getZoneRepresentation(true) << col.rst() <getZoneRepresentation(true)<<"\033[0m"<getZoneRepresentation(true) << col.rst() <getZoneRepresentation(true)<<"\033[0m"<getZoneRepresentation(true) << col.rst() <()->default_value(""), "virtual configuration name") ("config-dir", po::value()->default_value(SYSCONFDIR), "location of pdns.conf") + ("no-colors", "do not use colors in output") ("commands", po::value >()); po::positional_options_description p; @@ -3092,7 +3120,8 @@ try if (cmds.at(1) == ".") cmds.at(1).clear(); - return editZone(DNSName(cmds.at(1))); + PDNSColors col(g_vm.count("no-colors")); + return editZone(DNSName(cmds.at(1)), col); } else if (cmds.at(0) == "clear-zone") { if(cmds.size() != 2) {