diff --git a/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h b/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h index d9f83cc9265..cd133b46507 100644 --- a/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h +++ b/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h @@ -35,46 +35,165 @@ #pragma once #include +#include +#include +#include namespace OpenMS { class OPENMS_DLLAPI ConsoleUtils { -public: + public: /// make a string console friendly /// by breaking it into multiple lines according to the console width /// The 'indentation' gives the number of spaces which is prepended beginning at the second (!) /// line, so one gets a left aligned block which has some space to the left. /// An indentation of 0 results in the native console's default behaviour: just break at the end of /// its width and start a new line. - /// but usually one wants nicely intended blocks, which the console does not support - /// 'max_lines' gives the upper limit of lines returned after breaking is finished. + /// but usually one wants nicely indented blocks, which the console does not support + /// 'max_lines' gives the upper limit of lines returned after breaking is finished. /// Excess lines are removed and replaced by '...', BUT the last line will be preserved. /// @param input String to be split /// @param indentation Number of spaces to use for lines 2 until last line. /// @param max_lines Limit of output lines (all others are removed) - static String breakString(const String& input, const Size indentation, const Size max_lines); + static OpenMS::StringList breakString(const String& input, const Size indentation, const Size max_lines, const Size curser_pos = 0); -private: + const int getConsoleSize() + { + return console_width_; + } + + static ConsoleUtils getInstance() + { + return getInstance_(); + } + + +//#ifdef OPENMS_WINDOWSPLATFORM + + /// reset the color of the windows output handle + void resetCoutColor(); + /// reset the color of the windows error handle + void resetCerrColor(); + + /// reset the color of both output streams + void resetConsoleColor(); + + void setCoutColor(int color_code); + + void setCerrColor(int color_code); + +//#endif + + + + +/// Destructor + ~ConsoleUtils(); + + private: /// width of console we are currently in (if not determinable, set to 80 as default) int console_width_; /// read console settings for output shaping int readConsoleSize_(); + + static ConsoleUtils& getInstance_(); + /// returns a console friendly version of input - String breakString_(const String& input, const Size indentation, const Size max_lines); + OpenMS::StringList breakString_(const String& input, const Size indentation, const Size max_lines, const Size curser_pos); /// C'tor ConsoleUtils(); /// Copy C'tor - ConsoleUtils(const ConsoleUtils &); + ConsoleUtils(const ConsoleUtils&); + + // /// Destructor + // ~ConsoleUtils(); /// Assignment operator void operator=(ConsoleUtils const&); + +//#ifdef OPENMS_WINDOWSPLATFORM + + /// Default console color for output stream + int default_cout_; + + /// Default console color for error stream + int default_cerr_; + + +//#endif }; -} // namespace OpenMS + + + + + + + + + + + + + + + + class IndentedStringStream + { + public: + IndentedStringStream(std::ostream& stream, const Size indentation, const Size max_lines) : stream_(&stream), indentation_(indentation), max_lines_(max_lines), current_column_pos_(0) + { + max_line_width_ = ConsoleUtils::getInstance().getConsoleSize(); + } + + + template + IndentedStringStream& operator<<(const T& data) + { + + std::stringstream s; + s << data; + const std::string& string_to_print = s.str(); + + //wie viele zeilen sind es / viel viele zeichen in letrzter zeile + + OpenMS::StringList result = ConsoleUtils::breakString(string_to_print,indentation_,max_lines_,current_column_pos_); + + if (result.size()>=2) + { // we completed the previous line, so start counting from the latest incomplete line + current_column_pos_ = result.back().size(); + } + else + { // only one line; simply forward the column position + current_column_pos_ += result.back().size(); + } + + + + *stream_ << ListUtils::concatenate(result, '\n'); + + return *this; + + } +///741 = offset +///763 = indent + + IndentedStringStream& operator<<(Colorizer& colorizer); + + private: + std::ostream* stream_; + int indentation_; + int max_lines_; + int max_line_width_; + int current_column_pos_; + }; + + +} // namespace OpenMS diff --git a/src/openms/include/OpenMS/CONCEPT/Colorizer.h b/src/openms/include/OpenMS/CONCEPT/Colorizer.h new file mode 100644 index 00000000000..eb1f20f0bb7 --- /dev/null +++ b/src/openms/include/OpenMS/CONCEPT/Colorizer.h @@ -0,0 +1,163 @@ +// -------------------------------------------------------------------------- +// OpenMS -- Open-Source Mass Spectrometry +// -------------------------------------------------------------------------- +// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen, +// ETH Zurich, and Freie Universitaet Berlin 2002-2022. +// +// This software is released under a three-clause BSD license: +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// * Neither the name of any author or any participating institution +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// For a full list of authors, refer to the file AUTHORS. +// -------------------------------------------------------------------------- +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING +// INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// -------------------------------------------------------------------------- +// $Maintainer: Moritz Berger, Tetana Krymovska$ +// $Authors: Moritz Berger, Tetana Krymovska$ +// -------------------------------------------------------------------------- + +#pragma once + +//#include + +#include +#include +#include +//#include + + +namespace OpenMS +{ + /// enum COLOR for easier Object initialisation. + enum class COLOR + { + black, + red, + green, + yellow, + blue, + magenta, + cyan, + white, + RESET, ///< reset the color to the previous (default?) color + }; + + /** + * @brief A class, that provides options for colored output with the "<<" operator for output streams (cout, cerr) + * + */ + class Colorizer + { +public: + /// Constructor + Colorizer(const COLOR color); + + /// Copy constructor + // Colorizer(const Colorizer &rhs); + + ///Assignment Operator + + /// Destructor + ~Colorizer(); + + /// + void outputToStream(std::ostream& o_stream); + + /// + void colorStream(std::ostream& stream) const; + + /// + void resetColor(std::ostream& stream); + + /// + bool getReset(); + + /// + std::string getDataAsString(); + + /// insetrion Operator + friend std::ostream& operator<<(std::ostream& o_stream, Colorizer& col); + + /// Bracket Operator + Colorizer& operator()() + { + reset_ = false; + this->input_.str(""); // clear the stringstream + return *this; + } + + /// Bracket Operator + template + Colorizer& operator()(T s) + { + this->input_.str(""); // clear the stringstream + this->input_ << s; // add new data + reset_ = true; + return *this; + } + +private: + + const int color_; + + /// input in Colorizer object to be colored + std::stringstream input_; + + /// + bool reset_ = true; + + +/** + * @brief constant string array which saves the Linux color codes. + * 0=black + * 1=red + * 2=green + * 3=yellow + * 4=blue + * 5=magenta + * 6=cyan + * 7=white + * 8=default console color (reset) + * + */ +#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) + /// + inline static constexpr std::array colors_ {16, 12, 10, 14, 9, 13, 11, 15, 15}; + +#elif defined(__linux__) || defined(__OSX__) + /// + inline static constexpr std::array colors_ {"\033[30m", "\033[31m", "\033[32m", "\033[33m", "\033[34m", "\033[35m", "\033[36m", "\033[37m", "\033[0m"}; + +#endif + }; + + + // declaration of all colorizer object. + extern /*OPENMS_DLLAPI*/ Colorizer black; + extern /*OPENMS_DLLAPI*/ Colorizer red; + extern /*OPENMS_DLLAPI*/ Colorizer green; + extern /*OPENMS_DLLAPI*/ Colorizer yellow; + extern /*OPENMS_DLLAPI*/ Colorizer blue; + extern /*OPENMS_DLLAPI*/ Colorizer magenta; + extern /*OPENMS_DLLAPI*/ Colorizer cyan; + extern /*OPENMS_DLLAPI*/ Colorizer white; + extern /*OPENMS_DLLAPI*/ Colorizer reset_color; ///< reset the color to default, alias for 'make_default_color' + //extern /*OPENMS_DLLAPI*/ Colorizer default_color; ///< reset the color to default, alias for 'reset_color' + +} // namespace OpenMS diff --git a/src/openms/include/OpenMS/CONCEPT/sources.cmake b/src/openms/include/OpenMS/CONCEPT/sources.cmake index 41060570131..ad364110b52 100644 --- a/src/openms/include/OpenMS/CONCEPT/sources.cmake +++ b/src/openms/include/OpenMS/CONCEPT/sources.cmake @@ -5,6 +5,7 @@ set(directory include/OpenMS/CONCEPT) set(sources_list_h ClassTest.h Constants.h +Colorizer.h EnumHelpers.h Exception.h Factory.h diff --git a/src/openms/source/APPLICATIONS/ConsoleUtils.cpp b/src/openms/source/APPLICATIONS/ConsoleUtils.cpp index cf5e69335ce..d775fb91a05 100644 --- a/src/openms/source/APPLICATIONS/ConsoleUtils.cpp +++ b/src/openms/source/APPLICATIONS/ConsoleUtils.cpp @@ -37,7 +37,7 @@ #include #include -#ifdef OPENMS_WINDOWSPLATFORM +#ifdef OPENMS_WINDOWSPLATTFORM #include // for GetConsoleScreenBufferInfo() #undef min #undef max @@ -54,16 +54,39 @@ namespace OpenMS { // initialize the console width readConsoleSize_(); + + //if operating OS is Windows: save default output color for cour and cerr +#ifdef OPENMS_WINDOWSPLATTFORM + CONSOLE_SCREEN_BUFFER_INFO Info; + HANDLE handle_stdout = GetStdHandle(STD_OUTPUT_HANDLE); + HANDLE handle_stderr = GetStdHandle(STD_ERROR_HANDLE); + + GetConsoleScreenBufferInfo(handle_stdout, &Info); + + default_cout_ = Info.wAttributes; + + GetConsoleScreenBufferInfo(handle_stderr, &Info); + + default_cerr_ = Info.wAttributes; + + /// get and remember 2 default colors +#endif } - ConsoleUtils::ConsoleUtils(ConsoleUtils const& other) : - console_width_(other.console_width_) + ConsoleUtils::~ConsoleUtils() { +#ifdef OPENMS_WINDOWSPLATTFORM + resetConsoleColor(); +#endif + } + + ConsoleUtils::ConsoleUtils(ConsoleUtils const& other) = default; + void ConsoleUtils::operator=(const ConsoleUtils& other) = default; } - void ConsoleUtils::operator=(const ConsoleUtils& other) + OpenMS::StringList ConsoleUtils::breakString(const String& input, const Size indentation, const Size max_lines, const Size curser_pos) { - console_width_ = other.console_width_; + return getInstance().breakString_(input, indentation, max_lines, curser_pos); } int ConsoleUtils::readConsoleSize_() @@ -93,7 +116,7 @@ namespace OpenMS OPENMS_LOG_DEBUG << "output shaping: COLUMNS env does not exist!" << std::endl; } -#ifdef OPENMS_WINDOWSPLATFORM +#ifdef OPENMS_WINDOWSPLATTFORM HANDLE hOut; CONSOLE_SCREEN_BUFFER_INFO SBInfo; hOut = GetStdHandle(STD_OUTPUT_HANDLE); @@ -142,25 +165,39 @@ namespace OpenMS return console_width_; } - String ConsoleUtils::breakString(const String& input, const Size indentation, const Size max_lines) + ConsoleUtils& ConsoleUtils::getInstance_() { static ConsoleUtils instance; - return instance.breakString_(input, indentation, max_lines); + return instance; } - String ConsoleUtils::breakString_(const OpenMS::String& input, const Size indentation, const Size max_lines) +//erweitern, mit aktueller curser position +// rückgabe: vector von strings +// (OpenMS::StringList) + OpenMS::StringList ConsoleUtils::breakString_(const OpenMS::String& input, const Size indentation, const Size max_lines, const Size curser_pos) { - + + //OpenMS::StringList list; + //list.clear(); // get the line length - const Int line_len = ConsoleUtils::readConsoleSize_(); + Int line_len = ConsoleUtils::readConsoleSize_(); StringList result; Size short_line_len = line_len - indentation; + + /// hier wird linelen - neuer param curpos + line_len -= curser_pos; + + + if (short_line_len < 1) { std::cerr << "INTERNAL ERROR: cannot split lines into empty strings! see breakString_()"; - return input; + result.push_back(input); + return result; } + + for (Size i = 0; i < input.size(); ) { String line = input.substr(i, result.empty() ? line_len : short_line_len); // first line has full length @@ -206,7 +243,47 @@ namespace OpenMS } // remove last " " from last line to prevent automatic line break //if (result.size()>0 && result[result.size()-1].hasSuffix(" ")) result[result.size()-1] = result[result.size()-1].substr(0,result[result.size()-1].size()-1); - return ListUtils::concatenate(result, "\n"); + + return result; + } + +#ifdef OPENMS_WINDOWSPLATTFORM + void ConsoleUtils::resetConsoleColor() + { + setCoutColor(default_cout_); + setCerrColor(default_cerr_); } +void setCoutColor(int color_code) +{ + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_code); +} + +void setCerrColor(int color_code) +{ + SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), color_code); +} + +#endif + + + + IndentedStringStream& IndentedStringStream::operator<<(Colorizer& colorizer) + { + colorizer.colorStream(*stream_); + this->operator<<(colorizer.getDataAsString()); + + if(colorizer.getReset()) + { + colorizer.resetColor(*stream_); + + } + + return *this; + } + + + + + } diff --git a/src/openms/source/APPLICATIONS/TOPPBase.cpp b/src/openms/source/APPLICATIONS/TOPPBase.cpp index 1211e399aff..89e4afed6db 100755 --- a/src/openms/source/APPLICATIONS/TOPPBase.cpp +++ b/src/openms/source/APPLICATIONS/TOPPBase.cpp @@ -40,6 +40,7 @@ #include #include +#include #include #include @@ -239,10 +240,10 @@ namespace OpenMS for (int i = 0; i < argc; ++i) { if (String(argv[i]).has(' ')) - { + { args.push_back(String(argv[i]).quote()); // surround with quotes if argument contains a space } - else + else { args.push_back(argv[i]); } @@ -540,10 +541,16 @@ namespace OpenMS bool verbose = getFlag_("-helphelp"); String docurl = getDocumentationURL(); + + IndentedStringStream indentedStream0 (cerr, 0,10); // common output - cerr << "\n" - << ConsoleUtils::breakString(tool_name_ + " -- " + tool_description_, 0, 10) << "\n" - << ConsoleUtils::breakString(String("Full documentation: ") + docurl, 0, 10) << "\n" + indentedStream0 << "\n" + << red("Diese Zeile wurde manuell eingerfuegt. (TOPPBase.cpp zeile 546)") << "\n" + + << red(tool_name_) << green(tool_description_); + + indentedStream0 << tool_name_ + " -- " << tool_description_ << "\n" + << String("Full documentation: ") << docurl << "\n" << "Version: " << verboseVersion_ << "\n" << "To cite OpenMS:\n " << cite_openms_.toString() << "\n"; if (!citations_.empty()) @@ -553,12 +560,12 @@ namespace OpenMS } cerr << "\n"; cerr << "Usage:" << "\n" - << " " << tool_name_ << " " << "\n" + << " " << tool_name_ << green(" ") << "\n" << "\n"; // print warning regarding not shown parameters if (!subsections_.empty() && !verbose) - cerr << ConsoleUtils::breakString("This tool has algorithm parameters that are not shown here! Please check the ini file for a detailed description or use the --helphelp option.", 0, 10) + "\n\n"; + indentedStream0 << "This tool has algorithm parameters that are not shown here! Please check the ini file for a detailed description or use the --helphelp option." << "\n\n"; if (verbose) { @@ -590,7 +597,9 @@ namespace OpenMS UInt offset = 6 + max_size; //keep track of the current subsection we are in, to display the subsection help when a new section starts String current_TOPP_subsection(""); + + // PRINT parameters && description, restrictions and default for (vector::const_iterator it = parameters_.begin(); it != parameters_.end(); ++it) { @@ -617,7 +626,7 @@ namespace OpenMS subsection_description = current_TOPP_subsection; } - cerr << ConsoleUtils::breakString(subsection_description, 0, 10) << ":\n"; // print subsection description + indentedStream0 << yellow(subsection_description) << ":\n"; // print subsection description } else if (subsection.empty() && !current_TOPP_subsection.empty()) // subsection ended and normal parameters start again { @@ -627,9 +636,9 @@ namespace OpenMS //NAME + ARGUMENT String str_tmp = " -"; - str_tmp += it->name + " " + it->argument; + str_tmp += (it->name) + " " + (it->argument); if (it->required) - str_tmp += '*'; + str_tmp += ('*'); if (it->type == ParameterInformation::NEWLINE) str_tmp = ""; @@ -683,10 +692,10 @@ namespace OpenMS } String add = ""; - if (it->type == ParameterInformation::INPUT_FILE + if (it->type == ParameterInformation::INPUT_FILE || it->type == ParameterInformation::OUTPUT_FILE || it->type == ParameterInformation::OUTPUT_PREFIX - || it->type == ParameterInformation::INPUT_FILE_LIST + || it->type == ParameterInformation::INPUT_FILE_LIST || it->type == ParameterInformation::OUTPUT_FILE_LIST) add = " formats"; @@ -727,14 +736,14 @@ namespace OpenMS { desc_tmp += String(" (") + ListUtils::concatenate(addons, " ") + ")"; } - + IndentedStringStream indentedStream2 (cerr, offset,10); if (it->type == ParameterInformation::TEXT) - cerr << ConsoleUtils::breakString(str_tmp + desc_tmp, 0, 10); // no indentation for text + indentedStream2 << str_tmp << desc_tmp; // no indentation for text else - cerr << ConsoleUtils::breakString(str_tmp + desc_tmp, offset, 10); + indentedStream2 << str_tmp << desc_tmp; cerr << "\n"; } - + // SUBSECTION's at the end if (!subsections_.empty() && !verbose) { @@ -745,7 +754,7 @@ namespace OpenMS indent = max((UInt)it->first.size(), indent); } indent += 6; - + //output cerr << "\n" << "The following configuration subsections are valid:" << "\n"; @@ -753,14 +762,15 @@ namespace OpenMS { String tmp = String(" - ") + it->first; tmp.fillRight(' ', indent); - cerr << ConsoleUtils::breakString(tmp + it->second, indent, 10); + IndentedStringStream indentedStream2 (cerr, indent,10); + indentedStream2 << tmp << it->second; cerr << "\n"; } - cerr << "\n" - << ConsoleUtils::breakString("You can write an example INI file using the '-write_ini' option.", 0, 10) << "\n" - << ConsoleUtils::breakString("Documentation of subsection parameters can be found in the doxygen documentation or the INIFileEditor.", 0, 10) << "\n" - << ConsoleUtils::breakString("For more information, please consult the online documentation for this tool:", 0, 10) << "\n" - << ConsoleUtils::breakString(" - " + docurl, 0, 10) << "\n"; + indentedStream0 << "\n" + << "You can write an example INI file using the '-write_ini' option." << "\n" + << "Documentation of subsection parameters can be found in the doxygen documentation or the INIFileEditor." << "\n" + << "For more information, please consult the online documentation for this tool:" << "\n" + << " - " << docurl << "\n"; } cerr << endl; } @@ -1125,7 +1135,7 @@ namespace OpenMS } if (required && !default_value.empty() && count_conflicting_tags == 0) throw InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Registering a required InputFile param (" + name + ") with a non-empty default is forbidden!", default_value); - parameters_.push_back(ParameterInformation(name, ParameterInformation::INPUT_FILE, argument, default_value, description, required, advanced, tags)); + parameters_.push_back(ParameterInformation(name, ParameterInformation::INPUT_FILE, argument, default_value, description, required, advanced, tags)); } void TOPPBase::registerOutputFile_(const String& name, const String& argument, const String& default_value, const String& description, bool required, bool advanced) @@ -1236,9 +1246,9 @@ namespace OpenMS String TOPPBase::getStringOption_(const String& name) const { const ParameterInformation& p = findEntry_(name); - if (p.type != ParameterInformation::STRING - && p.type != ParameterInformation::INPUT_FILE - && p.type != ParameterInformation::OUTPUT_FILE + if (p.type != ParameterInformation::STRING + && p.type != ParameterInformation::INPUT_FILE + && p.type != ParameterInformation::OUTPUT_FILE && p.type != ParameterInformation::OUTPUT_PREFIX) { throw WrongParameterType(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, name); @@ -1422,7 +1432,7 @@ namespace OpenMS // determine file type as string FileTypes::Type f_type = FileHandler::getTypeByFileName(param_value); // Wrong ending, unknown is is ok. - if (f_type != FileTypes::UNKNOWN + if (f_type != FileTypes::UNKNOWN && !ListUtils::contains(p.valid_strings, FileTypes::typeToName(f_type).toUpper(), ListUtils::CASE::INSENSITIVE)) { throw InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, @@ -1441,8 +1451,8 @@ namespace OpenMS StringList TOPPBase::getStringList_(const String& name) const { const ParameterInformation& p = findEntry_(name); - if (p.type != ParameterInformation::STRINGLIST - && p.type != ParameterInformation::INPUT_FILE_LIST + if (p.type != ParameterInformation::STRINGLIST + && p.type != ParameterInformation::INPUT_FILE_LIST && p.type != ParameterInformation::OUTPUT_FILE_LIST) { throw WrongParameterType(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, name); @@ -2007,7 +2017,7 @@ namespace OpenMS { tags.push_back("input file"); } - + if (it->type == ParameterInformation::OUTPUT_FILE || it->type == ParameterInformation::OUTPUT_FILE_LIST) { tags.push_back("output file"); @@ -2016,7 +2026,7 @@ namespace OpenMS if (it->type == ParameterInformation::OUTPUT_PREFIX) { tags.push_back("output prefix"); - } + } switch (it->type) { diff --git a/src/openms/source/CONCEPT/Colorizer.cpp b/src/openms/source/CONCEPT/Colorizer.cpp new file mode 100644 index 00000000000..e112290e8c2 --- /dev/null +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -0,0 +1,166 @@ +// -------------------------------------------------------------------------- +// OpenMS -- Open-Source Mass Spectrometry +// -------------------------------------------------------------------------- +// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen, +// ETH Zurich, and Freie Universitaet Berlin 2002-2022. +// +// This software is released under a three-clause BSD license: +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// * Neither the name of any author or any participating institution +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// For a full list of authors, refer to the file AUTHORS. +// -------------------------------------------------------------------------- +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING +// INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// -------------------------------------------------------------------------- +// $Maintainer: Moritz Berger, Tetana Krymovska $ +// $Authors: Moritz Berger, Tetana Krymovska$ +// -------------------------------------------------------------------------- + + +// include on FU-server (Linux) +// #include + +// include on Windows PC +//#include + +// include in project +#include + + + +#include + +#ifdef OPENMS_WINDOWSPLATTFORM + #include + #include +#endif + +namespace OpenMS +{ + + // constructor + Colorizer::Colorizer(const COLOR color) : color_((int)color) // color must be in initializer list, because of const keyword + { + } + + /// Default destructor + Colorizer::~Colorizer() + { +// if colorizer object is destroyed, set console color back to def col. +#if defined(__linux__) || defined(__OSX__) + std::cout << colors_[8]; +#endif + } + + /// + void Colorizer::colorStream(std::ostream& stream) const + { +#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) + + if (&std::cout == &stream) + { + // set color of output + ConsoleUtils::getInstance().setCoutColor(colors_[color_]); + } + else if (&std::cerr == &stream) + { + ///set color of error stream + ConsoleUtils::getInstance().setCerrColor(colors_[color_]); + } + + +#elif defined(__linux__) || defined(__OSX__) + // write coloring escape codes into the string + stream << this->colors_[this->color_]; +#endif + } + + /// + void Colorizer::resetColor(std::ostream& stream) + { +#ifdef OPENMS_WINDOWSPLATTFORM + if (&std::cout == &stream) + { + // reset color of output + ConsoleUtils::getInstance().resetCoutColor(); + } + else if (&std::cerr == &stream) + { + ///reset color of error stream + ConsoleUtils::getInstance().resetCerrColor(); + } + + + +#elif defined(__linux__) || defined(__OSX__) + stream << this->colors_[8]; +#endif + } + + /// + bool Colorizer::getReset() + { + return reset_; + } + + /// + std::string Colorizer::getDataAsString() + { + return input_.str(); + } + + // Helper function, to manipulate the output stream in class. + void Colorizer::outputToStream(std::ostream& o_stream) + { + /// color the stream (or console) + colorStream(o_stream); + + // paste text + o_stream << this->input_.str(); + + // if flag reset is set: reset comand line. else dont reset. + if (this->reset_) + { + resetColor(o_stream); + } + } + + + // overload the shift operator (<<) + std::ostream& operator<<(std::ostream& o_stream, OpenMS::Colorizer& col) + { + // colorize string with color set in the object + col.outputToStream(o_stream); + return o_stream; + } + + // Objekte des typs colorizer + OpenMS::Colorizer black(COLOR::black); + OpenMS::Colorizer red(COLOR::red); + OpenMS::Colorizer green(COLOR::green); + OpenMS::Colorizer yellow(COLOR::yellow); + OpenMS::Colorizer blue(COLOR::blue); + OpenMS::Colorizer magenta(COLOR::magenta); + OpenMS::Colorizer cyan(COLOR::cyan); + OpenMS::Colorizer white(COLOR::white); + OpenMS::Colorizer reset_color(COLOR::RESET); + // OpenMS::Colorizer reset_color(COLOR::RESET); + + +} // namespace OpenMS diff --git a/src/openms/source/CONCEPT/sources.cmake b/src/openms/source/CONCEPT/sources.cmake index d8e51113075..7b9e179cd8f 100644 --- a/src/openms/source/CONCEPT/sources.cmake +++ b/src/openms/source/CONCEPT/sources.cmake @@ -4,6 +4,7 @@ set(directory source/CONCEPT) ### list all filenames of the directory here set(sources_list ClassTest.cpp +Colorizer.cpp Exception.cpp FuzzyStringComparator.cpp GlobalExceptionHandler.cpp