From fb60761c469f3b144e741cef14762a5b6a037112 Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Sun, 6 Oct 2019 19:00:34 -0500 Subject: Add some pretty handlers and use them --- util/output.hh | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'util') diff --git a/util/output.hh b/util/output.hh index ac356bc..f4f939f 100644 --- a/util/output.hh +++ b/util/output.hh @@ -16,6 +16,28 @@ #include #include +/*! Bolds output on +stream+ if it's a TTY. + * @param pretty Whether to act. + * @param stream The stream to turn bold. + */ +inline void bold_if_pretty(bool pretty, std::ostream &stream) { + if(pretty) stream << "\033[0;1m"; +} + +/*! ANSI colour code on +stream+ if +pretty+. + * @param pretty Whether to act. + * @param stream The stream on which to colourise output. + * @param what The colour code. + */ +inline void colour_if_pretty(bool pretty, std::ostream &stream, + std::string what) { + if(pretty) stream << "\033[" + what + ";1m"; +} + +inline void reset_if_pretty(bool pretty, std::ostream &stream) { + if(pretty) stream << "\033[0m"; +} + /*! Outputs a message of the specified +type+ to the log stream. * @param type The type of message to output. * @param colour The colourisation of the message. @@ -28,11 +50,11 @@ inline void output_message(std::string type, std::string colour, std::string where, std::string message, std::string detail = "", bool pretty = false) { std::cerr << where << ": "; - if(pretty) std::cerr << "\033[" + colour + ";1m"; + colour_if_pretty(pretty, std::cerr, colour); std::cerr << type << ": "; - if(pretty) std::cerr << "\033[0;1m"; + bold_if_pretty(pretty, std::cerr); std::cerr << message; - if(pretty) std::cerr << "\033[0m"; + reset_if_pretty(pretty, std::cerr); if(!detail.empty()) { std::cerr << ": " << detail; } -- cgit v1.2.3-70-g09d2