diff options
author | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-06 19:00:34 -0500 |
---|---|---|
committer | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-06 19:00:34 -0500 |
commit | fb60761c469f3b144e741cef14762a5b6a037112 (patch) | |
tree | 6f40a8683d2d3a89ec0c77e3cef65f86af379d78 | |
parent | c05569d223d5bda4390f1c501dc5915149617c93 (diff) | |
download | horizon-fb60761c469f3b144e741cef14762a5b6a037112.tar.gz horizon-fb60761c469f3b144e741cef14762a5b6a037112.tar.bz2 horizon-fb60761c469f3b144e741cef14762a5b6a037112.tar.xz horizon-fb60761c469f3b144e741cef14762a5b6a037112.zip |
Add some pretty handlers and use them
-rw-r--r-- | tools/hscript-validate/validator.cc | 13 | ||||
-rw-r--r-- | util/output.hh | 28 |
2 files changed, 30 insertions, 11 deletions
diff --git a/tools/hscript-validate/validator.cc b/tools/hscript-validate/validator.cc index addc968..3683b5d 100644 --- a/tools/hscript-validate/validator.cc +++ b/tools/hscript-validate/validator.cc @@ -19,6 +19,7 @@ int main(int argc, char *argv[]) { Horizon::ScriptOptions opts; int result_code = EXIT_SUCCESS; std::string installfile; + using Horizon::ScriptOptionFlags; /* Default to pretty if we are using a TTY, unless -n specified. */ if(isatty(1) && isatty(2)) { @@ -28,10 +29,10 @@ int main(int argc, char *argv[]) { auto cli = ( clipp::value("installfile", installfile), clipp::option("-k", "--keep-going").doc("Continue parsing after errors")( - [&opts] { opts.set(Horizon::ScriptOptionFlags::KeepGoing); } + [&opts] { opts.set(ScriptOptionFlags::KeepGoing); } ), clipp::option("-n", "--no-colour").doc("Do not 'prettify' output")( - [&opts] { opts.reset(Horizon::ScriptOptionFlags::Pretty); } + [&opts] { opts.reset(ScriptOptionFlags::Pretty); } ) ); if(!clipp::parse(argc, argv, cli)) { @@ -40,13 +41,9 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - if(opts.test(Horizon::ScriptOptionFlags::Pretty)) { - std::cout << "\033[1m"; - } + bold_if_pretty(opts.test(ScriptOptionFlags::Pretty), std::cout); std::cout << "HorizonScript Validation Utility version 0.1.0"; - if(opts.test(Horizon::ScriptOptionFlags::Pretty)) { - std::cout << "\033[0m"; - } + reset_if_pretty(opts.test(ScriptOptionFlags::Pretty), std::cout); std::cout << std::endl; std::cout << "Copyright (c) 2019 Adélie Linux and contributors. AGPL-3.0 license." << std::endl; std::cout << std::endl; 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 <string> #include <iostream> +/*! 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; } |