From 52d7559d99d8e9eb5c08df43d171b2e2aa24b891 Mon Sep 17 00:00:00 2001 From: Moritz Berger Date: Thu, 14 Apr 2022 10:48:27 +0200 Subject: [PATCH 01/23] Added class and header Colorizer.* into concept. Basic functionality for colorizing command line output (on Linux) is esablished --- src/openms/include/OpenMS/CONCEPT/Colorizer.h | 145 +++++++++++++++++ src/openms/source/CONCEPT/Colorizer.cpp | 154 ++++++++++++++++++ 2 files changed, 299 insertions(+) create mode 100644 src/openms/include/OpenMS/CONCEPT/Colorizer.h create mode 100644 src/openms/source/CONCEPT/Colorizer.cpp diff --git a/src/openms/include/OpenMS/CONCEPT/Colorizer.h b/src/openms/include/OpenMS/CONCEPT/Colorizer.h new file mode 100644 index 00000000000..5733abda928 --- /dev/null +++ b/src/openms/include/OpenMS/CONCEPT/Colorizer.h @@ -0,0 +1,145 @@ +// -------------------------------------------------------------------------- +// OpenMS -- Open-Source Mass Spectrometry +// -------------------------------------------------------------------------- +// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen, +// ETH Zurich, and Freie Universitaet Berlin 2002-2021. +// +// 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 +{ + + /** + * @brief A class, that provides options for coloring String output with the "<<" Operator + * + */ + class Colorizer + { + + public: + /** @name Constructors and Destructor + */ + //@{ + Colorizer(int color); + + /// Copy constructor + //Colorizer(const Colorizer &rhs); + + /// Default destructor + ~Colorizer(); + //@} + + + auto getColor(int i = NULL); + std::string getText(); + bool getReset() {return this->reset;} + +//operator overloading + friend std::ostream &operator<<(std::ostream &o_stream, Colorizer& col); + template + Colorizer& operator()(T s) + { + //clear was not possible (resets some flags in the sream) + this->reset = true; + this->_input.str(""); + this->_input << s; + return *this; + } + Colorizer& operator()() + { + + this ->reset = false; + this->_input.str(""); + return *this; + } + + private: + //std::string _text; + const int _color; + std::stringstream _input; + bool reset = true; + + //ewnum für farbauswahl + +#if defined(__linux__) || defined(__OSX__) + /** + * @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) + * + */ + inline static const std::array colors {"\033[30m", "\033[31m", "\033[32m", "\033[33m", "\033[34m", "\033[35m", "\033[36m", "\033[37m", "\033[0m"}; + #elif defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) + inline static const std::array colors {16,12,10,14,9,13,11,15,15}; +#endif + }; + + + + 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 def; +/* + Colorizer red(std::string text); + Colorizer green(std::string text); + Colorizer yellow(std::string text); + Colorizer cyan(std::string text); + + //Colorizer blue(const char *text); + Colorizer red(const char *text); + Colorizer green(const char *text); + Colorizer yellow(const char *text); + Colorizer cyan(const char *text);*/ +} diff --git a/src/openms/source/CONCEPT/Colorizer.cpp b/src/openms/source/CONCEPT/Colorizer.cpp new file mode 100644 index 00000000000..66be896e82d --- /dev/null +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -0,0 +1,154 @@ +// -------------------------------------------------------------------------- +// OpenMS -- Open-Source Mass Spectrometry +// -------------------------------------------------------------------------- +// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen, +// ETH Zurich, and Freie Universitaet Berlin 2002-2021. +// +// 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 +#include + +#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) +#include +#endif +/** + +#include + +#include +#include +**/ +namespace OpenMS +{ + + /** @name Constructors and Destructor + */ + //@{ + // constructor + Colorizer::Colorizer(int color): _color(color) + { + } + + /// Default destructor + Colorizer::~Colorizer() + { + } + //@} + + /** + \fn getColor(int i) + \details this function return the ANSI escape sequence for the color, saved in the Colorizer Object. + \return std::string contaaining ANSI escape characters for colors + \arg i codes for following colors: + * 0=black + * 1=red + * 2=green + * 3=yellow + * 4=blue + * 5=magenta + * 6=cyan + * 7=white + * 8=default console color (reset) + \return std::string contaaining ANSI escape characters for colors + */ + auto Colorizer::getColor(int i) + { + //checking if parameter is outside array size or not set at all + // Darf nicht NULL, da dadurch kein "black" mehr möglich ist + return ((i == NULL || i > 8 || i < 0) ? this->colors[this->_color] : this->colors[i]); + } + + std::string Colorizer::getText() + { + + return _input.str(); + } + // overload the shift operator (<<) + std::ostream &operator<<(std::ostream &o_stream, OpenMS::Colorizer& col) + { + // colorize string with color set in the object +#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) + + // safe console Font info in Attributes + // could be set global, at programm start, to have less requests and faster access + CONSOLE_SCREEN_BUFFER_INFO Info; + HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); + GetConsoleScreenBufferInfo(hStdout, &Info); + WORD Attributes = Info.wAttributes; + + + SetConsoleTextAttribute(hStdout,col.getColor()); + + + // paste text + o_stream << col.getText(); + // recover old Console font and set it as new one. + if(col.getReset()) + { + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), Attributes); + } + +#elif defined(__linux__) || defined(__OSX__) + o_stream << col.getColor(); + + o_stream << col.getText(); + //if flag reset is set: reset comand line. else dont reset. + o_stream << ((col.getReset())? col.getColor(8):""); +#endif + + return o_stream; + } + // All color types (Linux/OSX) + /* + Black 30 40 + Red 31 41 + Green 32 42 + Yellow 33 43 + Blue 34 44 + Magenta 35 45 + Cyan 36 46 + White 37 47 + */ + + //Objekte des typs colorizer + OpenMS::Colorizer black(0); + OpenMS::Colorizer red(1); + OpenMS::Colorizer green(2); + OpenMS::Colorizer yellow(3); + OpenMS::Colorizer blue(4); + OpenMS::Colorizer magenta(5); + OpenMS::Colorizer cyan(6); + OpenMS::Colorizer white(7); + OpenMS::Colorizer def(8); + + //free funct. make_color + + +} From 11df650bc0161c8a1f7400f83298f2857dc48c7e Mon Sep 17 00:00:00 2001 From: Moritz Berger Date: Fri, 22 Apr 2022 13:26:45 +0200 Subject: [PATCH 02/23] updated colorizer class --- src/openms/include/OpenMS/CONCEPT/Colorizer.h | 11 +++++++++++ src/openms/source/CONCEPT/Colorizer.cpp | 12 ++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/openms/include/OpenMS/CONCEPT/Colorizer.h b/src/openms/include/OpenMS/CONCEPT/Colorizer.h index 5733abda928..ec6368a6012 100644 --- a/src/openms/include/OpenMS/CONCEPT/Colorizer.h +++ b/src/openms/include/OpenMS/CONCEPT/Colorizer.h @@ -92,6 +92,17 @@ namespace OpenMS return *this; } +/* + template + std::string operator()(T s) + { + + std::stringstream text; + text << this; + return text.str(); + } +*/ + private: //std::string _text; const int _color; diff --git a/src/openms/source/CONCEPT/Colorizer.cpp b/src/openms/source/CONCEPT/Colorizer.cpp index 66be896e82d..dd0449ea164 100644 --- a/src/openms/source/CONCEPT/Colorizer.cpp +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -32,7 +32,8 @@ // $Authors: Moritz Berger ; Tetana Krymovska$ // -------------------------------------------------------------------------- -#include +#include +//#include #include #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) @@ -89,14 +90,14 @@ namespace OpenMS { return _input.str(); - } + } // overload the shift operator (<<) std::ostream &operator<<(std::ostream &o_stream, OpenMS::Colorizer& col) { // colorize string with color set in the object #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) - - // safe console Font info in Attributes + + // safe console Font info in Attributes // could be set global, at programm start, to have less requests and faster access CONSOLE_SCREEN_BUFFER_INFO Info; HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); @@ -105,8 +106,7 @@ namespace OpenMS SetConsoleTextAttribute(hStdout,col.getColor()); - - + // paste text o_stream << col.getText(); // recover old Console font and set it as new one. From bfbad5e7d6b36abc5927bf5b176f69d401896930 Mon Sep 17 00:00:00 2001 From: Moritz Berger Date: Fri, 22 Apr 2022 16:06:05 +0200 Subject: [PATCH 03/23] added full functionality for windows use --- src/openms/include/OpenMS/CONCEPT/Colorizer.h | 4 ++- src/openms/source/CONCEPT/Colorizer.cpp | 26 ++++++++++++------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/openms/include/OpenMS/CONCEPT/Colorizer.h b/src/openms/include/OpenMS/CONCEPT/Colorizer.h index ec6368a6012..9f0bf9a616e 100644 --- a/src/openms/include/OpenMS/CONCEPT/Colorizer.h +++ b/src/openms/include/OpenMS/CONCEPT/Colorizer.h @@ -108,7 +108,9 @@ namespace OpenMS const int _color; std::stringstream _input; bool reset = true; - +#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) + int _defcolor; +#endif //ewnum für farbauswahl #if defined(__linux__) || defined(__OSX__) diff --git a/src/openms/source/CONCEPT/Colorizer.cpp b/src/openms/source/CONCEPT/Colorizer.cpp index dd0449ea164..01d2f08dbd4 100644 --- a/src/openms/source/CONCEPT/Colorizer.cpp +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -55,11 +55,21 @@ namespace OpenMS // constructor Colorizer::Colorizer(int color): _color(color) { + #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) + CONSOLE_SCREEN_BUFFER_INFO Info; + HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); + GetConsoleScreenBufferInfo(hStdout, &Info); + _defcolor = Info.wAttributes; + #endif + } /// Default destructor Colorizer::~Colorizer() { + #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), _defcolor); + #endif } //@} @@ -97,22 +107,16 @@ namespace OpenMS // colorize string with color set in the object #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) - // safe console Font info in Attributes - // could be set global, at programm start, to have less requests and faster access - CONSOLE_SCREEN_BUFFER_INFO Info; - HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); - GetConsoleScreenBufferInfo(hStdout, &Info); - WORD Attributes = Info.wAttributes; - - - SetConsoleTextAttribute(hStdout,col.getColor()); + //set color of output + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),col.getColor()); // paste text o_stream << col.getText(); + // recover old Console font and set it as new one. if(col.getReset()) { - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), Attributes); + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), col._defcolor); } #elif defined(__linux__) || defined(__OSX__) @@ -125,6 +129,8 @@ namespace OpenMS return o_stream; } + + // All color types (Linux/OSX) /* Black 30 40 From 04ea4f4da44513a68a1105b3b42e0e2d66178d41 Mon Sep 17 00:00:00 2001 From: Moritz Berger Date: Wed, 27 Apr 2022 10:50:15 +0200 Subject: [PATCH 04/23] added colorizer to sources.cmake --- src/openms/include/OpenMS/CONCEPT/sources.cmake | 1 + src/openms/source/CONCEPT/sources.cmake | 1 + 2 files changed, 2 insertions(+) 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/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 From c5c344795c1805e546e15470f96820643f2dae95 Mon Sep 17 00:00:00 2001 From: Moritz Berger Date: Thu, 28 Apr 2022 09:48:25 +0200 Subject: [PATCH 05/23] =?UTF-8?q?colorizer=20in=20TOPPBase=20hinzugef?= =?UTF-8?q?=C3=BCgt,=20Testcodes=20eingef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/openms/include/OpenMS/CONCEPT/Colorizer.h | 16 +++++--- src/openms/source/APPLICATIONS/TOPPBase.cpp | 38 ++++++++++--------- src/openms/source/CONCEPT/Colorizer.cpp | 7 +++- 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/openms/include/OpenMS/CONCEPT/Colorizer.h b/src/openms/include/OpenMS/CONCEPT/Colorizer.h index 9f0bf9a616e..38197ab9757 100644 --- a/src/openms/include/OpenMS/CONCEPT/Colorizer.h +++ b/src/openms/include/OpenMS/CONCEPT/Colorizer.h @@ -69,7 +69,7 @@ namespace OpenMS //@} - auto getColor(int i = NULL); + auto getColor(int i = -1); std::string getText(); bool getReset() {return this->reset;} @@ -93,15 +93,17 @@ namespace OpenMS } /* +//klammer operator, um die farbigen Text an eine funktion übergeben zu können. template - std::string operator()(T s) + friend std::string operator()(T s) { std::stringstream text; text << this; return text.str(); } -*/ +*/ + private: //std::string _text; @@ -113,8 +115,9 @@ namespace OpenMS #endif //ewnum für farbauswahl -#if defined(__linux__) || defined(__OSX__) - /** + + +/** * @brief constant string array which saves the Linux color codes. * 0=black * 1=red @@ -127,8 +130,9 @@ namespace OpenMS * 8=default console color (reset) * */ +#if defined(__linux__) || defined(__OSX__) inline static const std::array colors {"\033[30m", "\033[31m", "\033[32m", "\033[33m", "\033[34m", "\033[35m", "\033[36m", "\033[37m", "\033[0m"}; - #elif defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) +#elif defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) inline static const std::array colors {16,12,10,14,9,13,11,15,15}; #endif }; diff --git a/src/openms/source/APPLICATIONS/TOPPBase.cpp b/src/openms/source/APPLICATIONS/TOPPBase.cpp index 1211e399aff..6b267bc4557 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]); } @@ -542,6 +543,7 @@ namespace OpenMS // common output cerr << "\n" + << red("Diese Zeile wurde manuell eingerfuegt. (TOPPBase.cpp zeile 546)") << "\n" << ConsoleUtils::breakString(tool_name_ + " -- " + tool_description_, 0, 10) << "\n" << ConsoleUtils::breakString(String("Full documentation: ") + docurl, 0, 10) << "\n" << "Version: " << verboseVersion_ << "\n" @@ -553,7 +555,7 @@ namespace OpenMS } cerr << "\n"; cerr << "Usage:" << "\n" - << " " << tool_name_ << " " << "\n" + << " " << tool_name_ << green(" ") << "\n" << "\n"; // print warning regarding not shown parameters @@ -617,7 +619,7 @@ namespace OpenMS subsection_description = current_TOPP_subsection; } - cerr << ConsoleUtils::breakString(subsection_description, 0, 10) << ":\n"; // print subsection description + cerr << yellow(ConsoleUtils::breakString(subsection_description, 0, 10)) << ":\n"; // print subsection description } else if (subsection.empty() && !current_TOPP_subsection.empty()) // subsection ended and normal parameters start again { @@ -683,10 +685,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"; @@ -729,9 +731,9 @@ namespace OpenMS } if (it->type == ParameterInformation::TEXT) - cerr << ConsoleUtils::breakString(str_tmp + desc_tmp, 0, 10); // no indentation for text + cerr << green(ConsoleUtils::breakString(str_tmp + desc_tmp, 0, 10)); // no indentation for text else - cerr << ConsoleUtils::breakString(str_tmp + desc_tmp, offset, 10); + cerr << green(ConsoleUtils::breakString(str_tmp + desc_tmp, offset, 10)); cerr << "\n"; } @@ -1125,7 +1127,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 +1238,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); @@ -1368,7 +1370,7 @@ namespace OpenMS writeLog_("Input file '" + param_value + "' could not be found (by searching on PATH). " "Either provide a full filepath or fix your PATH environment!" + (p.required ? "" : " Since this file is not strictly required, you might also pass the empty string \"\" as " - "argument to prevent its usage (this might limit the usability of the tool).")); + "argument to prevent it's usage (this might limit the usability of the tool).")); throw FileNotFound(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, param_value); } } @@ -1422,7 +1424,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 +1443,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 +2009,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 +2018,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 index 01d2f08dbd4..3f823a4ed1c 100644 --- a/src/openms/source/CONCEPT/Colorizer.cpp +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -93,7 +93,7 @@ namespace OpenMS { //checking if parameter is outside array size or not set at all // Darf nicht NULL, da dadurch kein "black" mehr möglich ist - return ((i == NULL || i > 8 || i < 0) ? this->colors[this->_color] : this->colors[i]); + return ((i == -1 || i > 8 || i < 0) ? this->colors[this->_color] : this->colors[i]); } std::string Colorizer::getText() @@ -146,6 +146,11 @@ namespace OpenMS //Objekte des typs colorizer OpenMS::Colorizer black(0); OpenMS::Colorizer red(1); + //std::string red(std::string text) + //{ + // return std::string(red(text)); + + //} OpenMS::Colorizer green(2); OpenMS::Colorizer yellow(3); OpenMS::Colorizer blue(4); From 87d41b7dbfd9e07ab71fc607ef3d19bffee02635 Mon Sep 17 00:00:00 2001 From: Moritz Berger Date: Thu, 28 Apr 2022 11:53:53 +0200 Subject: [PATCH 06/23] changed to colorizing functions, with std::string output rather than Colorizer Objects. (enables colorizer calls in functions with std::string expection) --- src/openms/include/OpenMS/CONCEPT/Colorizer.h | 104 ++++++++++++++++-- src/openms/source/APPLICATIONS/TOPPBase.cpp | 8 +- src/openms/source/CONCEPT/Colorizer.cpp | 22 ++-- 3 files changed, 111 insertions(+), 23 deletions(-) diff --git a/src/openms/include/OpenMS/CONCEPT/Colorizer.h b/src/openms/include/OpenMS/CONCEPT/Colorizer.h index 38197ab9757..8b42c49a132 100644 --- a/src/openms/include/OpenMS/CONCEPT/Colorizer.h +++ b/src/openms/include/OpenMS/CONCEPT/Colorizer.h @@ -75,13 +75,20 @@ namespace OpenMS //operator overloading friend std::ostream &operator<<(std::ostream &o_stream, Colorizer& col); + + + template Colorizer& operator()(T s) { //clear was not possible (resets some flags in the sream) - this->reset = true; + //std::string str = ""; this->_input.str(""); this->_input << s; + + + (this->_input.str() == "")? this->reset = false: this->reset = true; + return *this; } Colorizer& operator()() @@ -139,15 +146,82 @@ namespace OpenMS - 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 def; + extern /*OPENMS_DLLAPI*/ Colorizer color_black; + extern /*OPENMS_DLLAPI*/ Colorizer make_red; + extern /*OPENMS_DLLAPI*/ Colorizer make_green; + extern /*OPENMS_DLLAPI*/ Colorizer make_yellow; + extern /*OPENMS_DLLAPI*/ Colorizer make_blue; + extern /*OPENMS_DLLAPI*/ Colorizer make_magenta; + extern /*OPENMS_DLLAPI*/ Colorizer make_cyan; + extern /*OPENMS_DLLAPI*/ Colorizer make_white; + extern /*OPENMS_DLLAPI*/ Colorizer make_def; + + + + template + extern std::string black(T s = T(" ")) + { + std::stringstream text; + text << color_black(s); + return text.str(); +} + template + extern std::string red(T s = T("")) + { + std::stringstream text; + text << make_red(s); + return text.str(); +} + template + extern std::string green(T s) + { + std::stringstream text; + text << make_green(s); + return text.str(); +} + template + extern std::string yellow(T s = T("") ) + { + std::stringstream text; + text << make_yellow(s); + return text.str(); +} + template + extern std::string blue(T s = T("")) + { + std::stringstream text; + text << make_blue(s); + return text.str(); +} + template + extern std::string magenta(T s = T("")) + { + std::stringstream text; + text << make_magenta(s); + return text.str(); +} + template + extern std::string cyan(T s = T("")) + { + std::stringstream text; + text << make_cyan(s); + return text.str(); +} + template + extern std::string white(T s = T("")) + { + std::stringstream text; + text << make_white(s); + return text.str(); +} + template + extern std::string def(T s = T("")) + { + std::stringstream text; + text << make_def(s); + return text.str(); +} + /* Colorizer red(std::string text); Colorizer green(std::string text); @@ -160,3 +234,13 @@ namespace OpenMS Colorizer yellow(const char *text); Colorizer cyan(const char *text);*/ } + + + + + + + + + + diff --git a/src/openms/source/APPLICATIONS/TOPPBase.cpp b/src/openms/source/APPLICATIONS/TOPPBase.cpp index 6b267bc4557..5c0ec6b7c6b 100755 --- a/src/openms/source/APPLICATIONS/TOPPBase.cpp +++ b/src/openms/source/APPLICATIONS/TOPPBase.cpp @@ -629,9 +629,9 @@ namespace OpenMS //NAME + ARGUMENT String str_tmp = " -"; - str_tmp += it->name + " " + it->argument; + str_tmp += green(it->name) + " " + blue(it->argument); if (it->required) - str_tmp += '*'; + str_tmp += blue('*'); if (it->type == ParameterInformation::NEWLINE) str_tmp = ""; @@ -731,9 +731,9 @@ namespace OpenMS } if (it->type == ParameterInformation::TEXT) - cerr << green(ConsoleUtils::breakString(str_tmp + desc_tmp, 0, 10)); // no indentation for text + cerr << (ConsoleUtils::breakString(red(str_tmp) + desc_tmp, 0, 10)); // no indentation for text else - cerr << green(ConsoleUtils::breakString(str_tmp + desc_tmp, offset, 10)); + cerr << ConsoleUtils::breakString(str_tmp + desc_tmp, offset, 10); cerr << "\n"; } diff --git a/src/openms/source/CONCEPT/Colorizer.cpp b/src/openms/source/CONCEPT/Colorizer.cpp index 3f823a4ed1c..5ca32396c1f 100644 --- a/src/openms/source/CONCEPT/Colorizer.cpp +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -144,20 +144,24 @@ namespace OpenMS */ //Objekte des typs colorizer - OpenMS::Colorizer black(0); - OpenMS::Colorizer red(1); + OpenMS::Colorizer color_black(0); + OpenMS::Colorizer make_red(1); //std::string red(std::string text) //{ // return std::string(red(text)); //} - OpenMS::Colorizer green(2); - OpenMS::Colorizer yellow(3); - OpenMS::Colorizer blue(4); - OpenMS::Colorizer magenta(5); - OpenMS::Colorizer cyan(6); - OpenMS::Colorizer white(7); - OpenMS::Colorizer def(8); + OpenMS::Colorizer make_green(2); + + + OpenMS::Colorizer make_yellow(3); + OpenMS::Colorizer make_blue(4); + OpenMS::Colorizer make_magenta(5); + OpenMS::Colorizer make_cyan(6); + OpenMS::Colorizer make_white(7); + OpenMS::Colorizer make_def(8); + + //free funct. make_color From 2eb89f1145a5fefe4e16cadfacb6992ecc6499ed Mon Sep 17 00:00:00 2001 From: Moritz Berger Date: Fri, 29 Apr 2022 14:37:49 +0200 Subject: [PATCH 07/23] added few comments, clead functions, ready for Pull request (overview) --- src/openms/include/OpenMS/CONCEPT/Colorizer.h | 14 ++++++++------ src/openms/source/APPLICATIONS/TOPPBase.cpp | 2 +- src/openms/source/CONCEPT/Colorizer.cpp | 4 ++++ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/openms/include/OpenMS/CONCEPT/Colorizer.h b/src/openms/include/OpenMS/CONCEPT/Colorizer.h index 8b42c49a132..fefd334b0ca 100644 --- a/src/openms/include/OpenMS/CONCEPT/Colorizer.h +++ b/src/openms/include/OpenMS/CONCEPT/Colorizer.h @@ -78,8 +78,8 @@ namespace OpenMS - template - Colorizer& operator()(T s) + template + Colorizer& operator()(T s = T("")) { //clear was not possible (resets some flags in the sream) //std::string str = ""; @@ -91,6 +91,7 @@ namespace OpenMS return *this; } + /* Colorizer& operator()() { @@ -98,6 +99,7 @@ namespace OpenMS this->_input.str(""); return *this; } + */ /* //klammer operator, um die farbigen Text an eine funktion übergeben zu können. @@ -145,7 +147,7 @@ namespace OpenMS }; - +//deklaration of all colorizer object. extern /*OPENMS_DLLAPI*/ Colorizer color_black; extern /*OPENMS_DLLAPI*/ Colorizer make_red; extern /*OPENMS_DLLAPI*/ Colorizer make_green; @@ -157,9 +159,9 @@ namespace OpenMS extern /*OPENMS_DLLAPI*/ Colorizer make_def; - +//definition of colorizing functions. (so function calls like process_string(green(string)) are possible.) template - extern std::string black(T s = T(" ")) + extern std::string black(T s = T("")) { std::stringstream text; text << color_black(s); @@ -173,7 +175,7 @@ namespace OpenMS return text.str(); } template - extern std::string green(T s) + extern std::string green(T s = T("")) { std::stringstream text; text << make_green(s); diff --git a/src/openms/source/APPLICATIONS/TOPPBase.cpp b/src/openms/source/APPLICATIONS/TOPPBase.cpp index 5c0ec6b7c6b..2fcfb1b3839 100755 --- a/src/openms/source/APPLICATIONS/TOPPBase.cpp +++ b/src/openms/source/APPLICATIONS/TOPPBase.cpp @@ -692,7 +692,7 @@ namespace OpenMS || it->type == ParameterInformation::OUTPUT_FILE_LIST) add = " formats"; - addons.push_back(String("valid") + add + ": " + ListUtils::concatenate(copy, ", ")); // concatenate restrictions by comma + addons.push_back(String("valid") + add + ": " + magenta(ListUtils::concatenate(copy, ", "))); // concatenate restrictions by comma } break; diff --git a/src/openms/source/CONCEPT/Colorizer.cpp b/src/openms/source/CONCEPT/Colorizer.cpp index 5ca32396c1f..83597a54989 100644 --- a/src/openms/source/CONCEPT/Colorizer.cpp +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -55,6 +55,7 @@ namespace OpenMS // constructor Colorizer::Colorizer(int color): _color(color) { + //If we are on windows, get the "default" console color and safe it in _defcolor #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) CONSOLE_SCREEN_BUFFER_INFO Info; HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); @@ -67,8 +68,11 @@ namespace OpenMS /// Default destructor Colorizer::~Colorizer() { + //if colorizer oibject is destroyed, set console color back to def col. #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), _defcolor); + #elif defined(__linux__) || defined(__OSX__) + std::cout << colors[8]; #endif } //@} From a80ff5875b5f3847773fff23a73ce0a47a0b680d Mon Sep 17 00:00:00 2001 From: morm24 <74315267+morm24@users.noreply.github.com> Date: Mon, 2 May 2022 13:28:21 +0200 Subject: [PATCH 08/23] Apply suggestions from code review Changes made in pull request code review. Not working, will be fixed by next commit. Co-authored-by: Chris Bielow Co-authored-by: Lenny Kovac <44415793+lennykovac@users.noreply.github.com> Co-authored-by: hbeschorner <102038306+hbeschorner@users.noreply.github.com> --- src/openms/include/OpenMS/CONCEPT/Colorizer.h | 74 +++++++++++++------ src/openms/source/APPLICATIONS/TOPPBase.cpp | 2 +- src/openms/source/CONCEPT/Colorizer.cpp | 10 +-- 3 files changed, 56 insertions(+), 30 deletions(-) diff --git a/src/openms/include/OpenMS/CONCEPT/Colorizer.h b/src/openms/include/OpenMS/CONCEPT/Colorizer.h index fefd334b0ca..5dd849b65e4 100644 --- a/src/openms/include/OpenMS/CONCEPT/Colorizer.h +++ b/src/openms/include/OpenMS/CONCEPT/Colorizer.h @@ -28,8 +28,8 @@ // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // // -------------------------------------------------------------------------- -// $Maintainer: Moritz Berger ; Tetana Krymovska$ -// $Authors: Moritz Berger ; Tetana Krymovska$ +// $Maintainer: Moritz Berger, Tetana Krymovska$ +// $Authors: Moritz Berger, Tetana Krymovska$ // -------------------------------------------------------------------------- #pragma once @@ -49,45 +49,71 @@ namespace OpenMS { /** - * @brief A class, that provides options for coloring String output with the "<<" Operator + * @brief A class, that provides options for colored output with the "<<" operator for output streams (cout, cerr) * */ class Colorizer { public: + + enum class COLOR // todo: move this outside of class + { + black, + red, + green, + yellow, + blue, + magenta, + cyan, + white, + RESET, ///< reset the color to the previous (default?) color + }; + /** @name Constructors and Destructor */ //@{ - Colorizer(int color); + Colorizer(COLOR color); /// Copy constructor //Colorizer(const Colorizer &rhs); - /// Default destructor + /// Destructor ~Colorizer(); //@} auto getColor(int i = -1); std::string getText(); - bool getReset() {return this->reset;} + bool getReset() + { + return this->reset; + } //operator overloading friend std::ostream &operator<<(std::ostream &o_stream, Colorizer& col); - template - Colorizer& operator()(T s = T("")) + + Colorizer& operator()() + { + reset_ = false; + this->_input.str(""); // clear the stream + return *this; + } + + + template + Colorizer& operator()(T s) { //clear was not possible (resets some flags in the sream) //std::string str = ""; - this->_input.str(""); - this->_input << s; + this->_input.str(""); // clear the stringstream + this->_input << s; // add new data - (this->_input.str() == "")? this->reset = false: this->reset = true; + reset_ = true; return *this; } @@ -116,11 +142,11 @@ namespace OpenMS private: //std::string _text; - const int _color; - std::stringstream _input; - bool reset = true; -#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) - int _defcolor; + const int color_; + std::stringstream input_; + bool reset_ = true; +#ifdef OPENMS_WINDOWSPLATFORM + int default_color_; #endif //ewnum für farbauswahl @@ -140,15 +166,15 @@ namespace OpenMS * */ #if defined(__linux__) || defined(__OSX__) - inline static const std::array colors {"\033[30m", "\033[31m", "\033[32m", "\033[33m", "\033[34m", "\033[35m", "\033[36m", "\033[37m", "\033[0m"}; + inline static constexpr std::array colors_ {"\033[30m", "\033[31m", "\033[32m", "\033[33m", "\033[34m", "\033[35m", "\033[36m", "\033[37m", "\033[0m"}; #elif defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) - inline static const std::array colors {16,12,10,14,9,13,11,15,15}; + inline static constexpr std::array colors_ {16,12,10,14,9,13,11,15,15}; #endif }; -//deklaration of all colorizer object. - extern /*OPENMS_DLLAPI*/ Colorizer color_black; +//declaration of all colorizer object. + extern /*OPENMS_DLLAPI*/ Colorizer make_black; extern /*OPENMS_DLLAPI*/ Colorizer make_red; extern /*OPENMS_DLLAPI*/ Colorizer make_green; extern /*OPENMS_DLLAPI*/ Colorizer make_yellow; @@ -156,16 +182,16 @@ namespace OpenMS extern /*OPENMS_DLLAPI*/ Colorizer make_magenta; extern /*OPENMS_DLLAPI*/ Colorizer make_cyan; extern /*OPENMS_DLLAPI*/ Colorizer make_white; - extern /*OPENMS_DLLAPI*/ Colorizer make_def; + extern /*OPENMS_DLLAPI*/ Colorizer reset_color; ///< reset the color to default, alias for 'make_default_color' + extern /*OPENMS_DLLAPI*/ Colorizer make_default_color; ///< reset the color to default, alias for 'reset_color' + //definition of colorizing functions. (so function calls like process_string(green(string)) are possible.) template extern std::string black(T s = T("")) { - std::stringstream text; - text << color_black(s); - return text.str(); + return color_black(s); } template extern std::string red(T s = T("")) diff --git a/src/openms/source/APPLICATIONS/TOPPBase.cpp b/src/openms/source/APPLICATIONS/TOPPBase.cpp index 2fcfb1b3839..b5ce0036ba9 100755 --- a/src/openms/source/APPLICATIONS/TOPPBase.cpp +++ b/src/openms/source/APPLICATIONS/TOPPBase.cpp @@ -1370,7 +1370,7 @@ namespace OpenMS writeLog_("Input file '" + param_value + "' could not be found (by searching on PATH). " "Either provide a full filepath or fix your PATH environment!" + (p.required ? "" : " Since this file is not strictly required, you might also pass the empty string \"\" as " - "argument to prevent it's usage (this might limit the usability of the tool).")); + "argument to prevent its usage (this might limit the usability of the tool).")); throw FileNotFound(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, param_value); } } diff --git a/src/openms/source/CONCEPT/Colorizer.cpp b/src/openms/source/CONCEPT/Colorizer.cpp index 83597a54989..6e919457efb 100644 --- a/src/openms/source/CONCEPT/Colorizer.cpp +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -28,11 +28,11 @@ // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // // -------------------------------------------------------------------------- -// $Maintainer: Moritz Berger; Tetana Krymovska $ -// $Authors: Moritz Berger ; Tetana Krymovska$ +// $Maintainer: Moritz Berger, Tetana Krymovska $ +// $Authors: Moritz Berger, Tetana Krymovska$ // -------------------------------------------------------------------------- -#include +#include //#include #include @@ -68,7 +68,7 @@ namespace OpenMS /// Default destructor Colorizer::~Colorizer() { - //if colorizer oibject is destroyed, set console color back to def col. + //if colorizer object is destroyed, set console color back to def col. #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), _defcolor); #elif defined(__linux__) || defined(__OSX__) @@ -112,7 +112,7 @@ namespace OpenMS #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) //set color of output - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),col.getColor()); + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), col.getColor()); // paste text o_stream << col.getText(); From 5749fa6dc6aaf59745ec58da6a3966f31d3e8257 Mon Sep 17 00:00:00 2001 From: Moritz Berger Date: Mon, 2 May 2022 15:16:42 +0200 Subject: [PATCH 09/23] changed pull request changes, now working with linux. windows functionality will be set with next commit --- src/openms/include/OpenMS/CONCEPT/Colorizer.h | 309 ++++++++---------- src/openms/source/CONCEPT/Colorizer.cpp | 116 +++---- 2 files changed, 196 insertions(+), 229 deletions(-) diff --git a/src/openms/include/OpenMS/CONCEPT/Colorizer.h b/src/openms/include/OpenMS/CONCEPT/Colorizer.h index 5dd849b65e4..d5540aaa01f 100644 --- a/src/openms/include/OpenMS/CONCEPT/Colorizer.h +++ b/src/openms/include/OpenMS/CONCEPT/Colorizer.h @@ -2,7 +2,7 @@ // OpenMS -- Open-Source Mass Spectrometry // -------------------------------------------------------------------------- // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen, -// ETH Zurich, and Freie Universitaet Berlin 2002-2021. +// 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 @@ -36,17 +36,68 @@ //#include +#include #include #include -#include //#include +namespace OpenMS +{ +#ifdef OPENMS_WINDOWSPLATFORM + namespace Internal + { + struct WindowsOSDefaultColor { + WindowsOSDefaultColor() + { + //! nicht hier initialisieren, da winows.h includiert werden müste. (in .cpp machen) + + std::cout << "saving default colors...\n"; + 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 + } + ~WindowsOSDefaultColor() + { + + std::cout << "restoring default colors...\n"; + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), default_cout_ ); + etConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), default_cerr_); + + } + + int default_cout_; + int default_cerr_; + }; // end WindowsOSDefaultColor + extern static const WindowsOSDefaultColor default_color____; -namespace OpenMS -{ + } // namespace Internal +#endif + +//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) @@ -54,221 +105,147 @@ namespace OpenMS */ class Colorizer { - public: - - enum class COLOR // todo: move this outside of class - { - black, - red, - green, - yellow, - blue, - magenta, - cyan, - white, - RESET, ///< reset the color to the previous (default?) color - }; - /** @name Constructors and Destructor */ //@{ - Colorizer(COLOR color); + Colorizer(const COLOR color); /// Copy constructor - //Colorizer(const Colorizer &rhs); + // Colorizer(const Colorizer &rhs); /// Destructor ~Colorizer(); //@} + void outputToStream(std::ostream &o_stream); - auto getColor(int i = -1); - std::string getText(); - bool getReset() - { - return this->reset; - } -//operator overloading - friend std::ostream &operator<<(std::ostream &o_stream, Colorizer& col); - - - + // operator overloading + friend std::ostream& operator<<(std::ostream& o_stream, Colorizer& col); + Colorizer& operator()() { reset_ = false; - this->_input.str(""); // clear the stream + this->input_.str(""); // clear the stream return *this; } - - - template - Colorizer& operator()(T s) - { - //clear was not possible (resets some flags in the sream) - //std::string str = ""; - this->_input.str(""); // clear the stringstream - this->_input << s; // add new data - - reset_ = true; - - return *this; - } - /* - Colorizer& operator()() - { - - this ->reset = false; - this->_input.str(""); - return *this; - } - */ -/* -//klammer operator, um die farbigen Text an eine funktion übergeben zu können. - template - friend std::string operator()(T s) + template + Colorizer& operator()(T s) { - - std::stringstream text; - text << this; - return text.str(); + this->input_.str(""); // clear the stringstream + this->input_ << s; // add new data + reset_ = true; + return *this; } -*/ - private: - //std::string _text; const int color_; std::stringstream input_; bool reset_ = true; -#ifdef OPENMS_WINDOWSPLATFORM - int default_color_; -#endif - //ewnum für farbauswahl /** - * @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(__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"}; -#elif defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) - inline static constexpr std::array colors_ {16,12,10,14,9,13,11,15,15}; + * @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) + * + */ +#ifdef OPENMS_WINDOWSPLATFORM + 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 make_black; - extern /*OPENMS_DLLAPI*/ Colorizer make_red; - extern /*OPENMS_DLLAPI*/ Colorizer make_green; - extern /*OPENMS_DLLAPI*/ Colorizer make_yellow; - extern /*OPENMS_DLLAPI*/ Colorizer make_blue; - extern /*OPENMS_DLLAPI*/ Colorizer make_magenta; - extern /*OPENMS_DLLAPI*/ Colorizer make_cyan; - extern /*OPENMS_DLLAPI*/ Colorizer make_white; - extern /*OPENMS_DLLAPI*/ Colorizer reset_color; ///< reset the color to default, alias for 'make_default_color' - extern /*OPENMS_DLLAPI*/ Colorizer make_default_color; ///< reset the color to default, alias for 'reset_color' - + // 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' + + // wahrscheinlich unnörig, da funktionalität unter windows nicht gegeben. -//definition of colorizing functions. (so function calls like process_string(green(string)) are possible.) - template + // definition of colorizing functions. (so function calls like process_string(green(string)) are possible.) + /* + template extern std::string black(T s = T("")) { - return color_black(s); -} - template + return color_black(s); + } + template extern std::string red(T s = T("")) { - std::stringstream text; - text << make_red(s); - return text.str(); -} - template + return make_red(s); + } + template extern std::string green(T s = T("")) { - std::stringstream text; - text << make_green(s); - return text.str(); -} - template - extern std::string yellow(T s = T("") ) + return make_green(s); + } + template + extern std::string yellow(T s = T("")) { - std::stringstream text; - text << make_yellow(s); - return text.str(); -} - template + return make_yellow(s); + } + template extern std::string blue(T s = T("")) { - std::stringstream text; - text << make_blue(s); - return text.str(); -} - template + return make_blue(s); + } + template extern std::string magenta(T s = T("")) { - std::stringstream text; - text << make_magenta(s); - return text.str(); -} - template + return make_magenta(s); + } + template extern std::string cyan(T s = T("")) { - std::stringstream text; - text << make_cyan(s); - return text.str(); -} - template + + return make_cyan(s); + } + template extern std::string white(T s = T("")) { - std::stringstream text; - text << make_white(s); - return text.str(); -} - template + return make_white(s); + } + template extern std::string def(T s = T("")) { - std::stringstream text; - text << make_def(s); - return text.str(); -} - -/* - Colorizer red(std::string text); - Colorizer green(std::string text); - Colorizer yellow(std::string text); - Colorizer cyan(std::string text); - - //Colorizer blue(const char *text); - Colorizer red(const char *text); - Colorizer green(const char *text); - Colorizer yellow(const char *text); - Colorizer cyan(const char *text);*/ -} - - - - - - - - - + return make_default_color(s); + } + + /* + Colorizer red(std::string text); + Colorizer green(std::string text); + Colorizer yellow(std::string text); + Colorizer cyan(std::string text); + + //Colorizer blue(const char *text); + Colorizer red(const char *text); + Colorizer green(const char *text); + Colorizer yellow(const char *text); + Colorizer cyan(const char *text);*/ +} // namespace OpenMS diff --git a/src/openms/source/CONCEPT/Colorizer.cpp b/src/openms/source/CONCEPT/Colorizer.cpp index 6e919457efb..6e9cae2c3dd 100644 --- a/src/openms/source/CONCEPT/Colorizer.cpp +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -2,7 +2,7 @@ // OpenMS -- Open-Source Mass Spectrometry // -------------------------------------------------------------------------- // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen, -// ETH Zurich, and Freie Universitaet Berlin 2002-2021. +// 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 @@ -32,19 +32,18 @@ // $Authors: Moritz Berger, Tetana Krymovska$ // -------------------------------------------------------------------------- -#include +#include //#include #include #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) -#include + #include #endif /** -#include - -#include #include +#include +#include **/ namespace OpenMS { @@ -53,27 +52,17 @@ namespace OpenMS */ //@{ // constructor - Colorizer::Colorizer(int color): _color(color) + Colorizer::Colorizer(const COLOR color) : color_((int)color) // color must be in initializer list, because of const keyword { - //If we are on windows, get the "default" console color and safe it in _defcolor - #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) - CONSOLE_SCREEN_BUFFER_INFO Info; - HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); - GetConsoleScreenBufferInfo(hStdout, &Info); - _defcolor = Info.wAttributes; - #endif - } /// Default destructor Colorizer::~Colorizer() { - //if colorizer object is destroyed, set console color back to def col. - #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), _defcolor); - #elif defined(__linux__) || defined(__OSX__) - std::cout << colors[8]; - #endif +// if colorizer object is destroyed, set console color back to def col. +#if defined(__linux__) || defined(__OSX__) + std::cout << colors_[8]; +#endif } //@} @@ -92,7 +81,7 @@ namespace OpenMS * 7=white * 8=default console color (reset) \return std::string contaaining ANSI escape characters for colors - */ + auto Colorizer::getColor(int i) { //checking if parameter is outside array size or not set at all @@ -101,36 +90,48 @@ namespace OpenMS } std::string Colorizer::getText() - { + { return _input.str(); - } - // overload the shift operator (<<) - std::ostream &operator<<(std::ostream &o_stream, OpenMS::Colorizer& col) + } + */ + + //Helper function, to manipulate the output stream in class. + void Colorizer::outputToStream(std::ostream& o_stream) { - // colorize string with color set in the object -#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) - - //set color of output - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), col.getColor()); + #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) + + // set color of output + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), this->colors[this->color_]); // paste text - o_stream << col.getText(); - + o_stream << this->input_; + // recover old Console font and set it as new one. - if(col.getReset()) + if (this->reset_) { - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), col._defcolor); + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), default_color___); } #elif defined(__linux__) || defined(__OSX__) - o_stream << col.getColor(); + o_stream << this->colors_[this->color_]; + + o_stream << this->input_.str(); - o_stream << col.getText(); - //if flag reset is set: reset comand line. else dont reset. - o_stream << ((col.getReset())? col.getColor(8):""); + // if flag reset is set: reset comand line. else dont reset. + if(this->reset_) + { + o_stream << this->colors_[8]; + } #endif + } + + // 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; } @@ -147,27 +148,16 @@ namespace OpenMS White 37 47 */ - //Objekte des typs colorizer - OpenMS::Colorizer color_black(0); - OpenMS::Colorizer make_red(1); - //std::string red(std::string text) - //{ - // return std::string(red(text)); - - //} - OpenMS::Colorizer make_green(2); - - - OpenMS::Colorizer make_yellow(3); - OpenMS::Colorizer make_blue(4); - OpenMS::Colorizer make_magenta(5); - OpenMS::Colorizer make_cyan(6); - OpenMS::Colorizer make_white(7); - OpenMS::Colorizer make_def(8); - - - - //free funct. make_color - - -} + // 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 default_color(COLOR::RESET); + +} // namespace OpenMS From 5a32c1360310e40bc229a0330a8354a9a75e3ee8 Mon Sep 17 00:00:00 2001 From: Moritz Berger Date: Mon, 2 May 2022 15:22:27 +0200 Subject: [PATCH 10/23] corrected include path, addition to last commit: removed ability to color inside function call --- src/openms/source/CONCEPT/Colorizer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openms/source/CONCEPT/Colorizer.cpp b/src/openms/source/CONCEPT/Colorizer.cpp index 6e9cae2c3dd..edb89bbedad 100644 --- a/src/openms/source/CONCEPT/Colorizer.cpp +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -32,8 +32,8 @@ // $Authors: Moritz Berger, Tetana Krymovska$ // -------------------------------------------------------------------------- -#include -//#include +//#include +#include #include #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) From 90b30ac992969aa9de1207b617c2380e29126ac2 Mon Sep 17 00:00:00 2001 From: morm24 <74315267+morm24@users.noreply.github.com> Date: Tue, 3 May 2022 08:46:51 +0200 Subject: [PATCH 11/23] Error in windows functionality: struct WindowsOSDefaultColor when compiling. --- src/openms/include/OpenMS/CONCEPT/Colorizer.h | 64 +++++--------- src/openms/source/CONCEPT/Colorizer.cpp | 83 ++++++++++++++----- 2 files changed, 81 insertions(+), 66 deletions(-) diff --git a/src/openms/include/OpenMS/CONCEPT/Colorizer.h b/src/openms/include/OpenMS/CONCEPT/Colorizer.h index d5540aaa01f..34343b6be98 100644 --- a/src/openms/include/OpenMS/CONCEPT/Colorizer.h +++ b/src/openms/include/OpenMS/CONCEPT/Colorizer.h @@ -44,50 +44,28 @@ namespace OpenMS { -#ifdef OPENMS_WINDOWSPLATFORM +#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) namespace Internal { struct WindowsOSDefaultColor { - WindowsOSDefaultColor() - { - //! nicht hier initialisieren, da winows.h includiert werden müste. (in .cpp machen) - - std::cout << "saving default colors...\n"; - 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 - } - ~WindowsOSDefaultColor() - { - - std::cout << "restoring default colors...\n"; - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), default_cout_ ); - etConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), default_cerr_); - - } - - - int default_cout_; - int default_cerr_; - }; // end WindowsOSDefaultColor + WindowsOSDefaultColor(); + ~WindowsOSDefaultColor(); + + //int get_default_cout(); - extern static const WindowsOSDefaultColor default_color____; + int default_cout_; + int default_cerr_; + }; // end WindowsOSDefaultColor + + //extern or static. both throw an error + static const WindowsOSDefaultColor default_color___(); } // namespace Internal #endif -//enum COLOR for easier Object initialisation. - enum class COLOR { + // enum COLOR for easier Object initialisation. + enum class COLOR + { black, red, green, @@ -118,8 +96,8 @@ namespace OpenMS ~Colorizer(); //@} - void outputToStream(std::ostream &o_stream); - + void outputToStream(std::ostream& o_stream); + // operator overloading friend std::ostream& operator<<(std::ostream& o_stream, Colorizer& col); @@ -148,7 +126,6 @@ namespace OpenMS bool reset_ = true; - /** * @brief constant string array which saves the Linux color codes. * 0=black @@ -162,7 +139,7 @@ namespace OpenMS * 8=default console color (reset) * */ -#ifdef OPENMS_WINDOWSPLATFORM +#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__) @@ -181,8 +158,8 @@ namespace OpenMS 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' + 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' // wahrscheinlich unnörig, da funktionalität unter windows nicht gegeben. @@ -222,7 +199,7 @@ namespace OpenMS template extern std::string cyan(T s = T("")) { - + return make_cyan(s); } template @@ -248,4 +225,3 @@ namespace OpenMS Colorizer yellow(const char *text); Colorizer cyan(const char *text);*/ } // namespace OpenMS - diff --git a/src/openms/source/CONCEPT/Colorizer.cpp b/src/openms/source/CONCEPT/Colorizer.cpp index edb89bbedad..610c3079dcf 100644 --- a/src/openms/source/CONCEPT/Colorizer.cpp +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -32,8 +32,14 @@ // $Authors: Moritz Berger, Tetana Krymovska$ // -------------------------------------------------------------------------- + +// include on FU-server (Linux) //#include -#include +// include on Windows PC +#include + +// include in project +//#include #include #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) @@ -48,6 +54,52 @@ namespace OpenMS { +#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) + namespace Internal + { + + WindowsOSDefaultColor::WindowsOSDefaultColor() + { + std::cout << "saving default colors...\n"; + 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 + } + WindowsOSDefaultColor::~WindowsOSDefaultColor() + { + std::cout << "restoring default colors...\n"; + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), default_cout_); + SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), default_cerr_); + } + extern const WindowsOSDefaultColor default_color___(); + + } // namespace Internal + +#endif + + + + + + + + + + + + + + /** @name Constructors and Destructor */ //@{ @@ -81,36 +133,23 @@ namespace OpenMS * 7=white * 8=default console color (reset) \return std::string contaaining ANSI escape characters for colors - - auto Colorizer::getColor(int i) - { - //checking if parameter is outside array size or not set at all - // Darf nicht NULL, da dadurch kein "black" mehr möglich ist - return ((i == -1 || i > 8 || i < 0) ? this->colors[this->_color] : this->colors[i]); - } - - std::string Colorizer::getText() - { - - return _input.str(); - } */ - //Helper function, to manipulate the output stream in class. + // Helper function, to manipulate the output stream in class. void Colorizer::outputToStream(std::ostream& o_stream) { - #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) +#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) // set color of output - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), this->colors[this->color_]); + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), this->colors_[this->color_]); // paste text - o_stream << this->input_; + o_stream << this->input_.str(); // recover old Console font and set it as new one. if (this->reset_) { - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), default_color___); + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), OpenMS::Internal::default_color___.default_cout_); } #elif defined(__linux__) || defined(__OSX__) @@ -119,7 +158,7 @@ namespace OpenMS o_stream << this->input_.str(); // if flag reset is set: reset comand line. else dont reset. - if(this->reset_) + if (this->reset_) { o_stream << this->colors_[8]; } @@ -157,7 +196,7 @@ namespace OpenMS OpenMS::Colorizer magenta(COLOR::magenta); OpenMS::Colorizer cyan(COLOR::cyan); OpenMS::Colorizer white(COLOR::white); - OpenMS::Colorizer reset_color(COLOR::RESET); - OpenMS::Colorizer default_color(COLOR::RESET); + OpenMS::Colorizer reset_color(COLOR::RESET); + //OpenMS::Colorizer reset_color(COLOR::RESET); } // namespace OpenMS From de986180780d42aac7a7ad36e31a78a093cdd32d Mon Sep 17 00:00:00 2001 From: Moritz Berger Date: Tue, 3 May 2022 10:04:35 +0200 Subject: [PATCH 12/23] struct error should be fixed now. --- src/openms/include/OpenMS/CONCEPT/Colorizer.h | 2 +- src/openms/source/CONCEPT/Colorizer.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openms/include/OpenMS/CONCEPT/Colorizer.h b/src/openms/include/OpenMS/CONCEPT/Colorizer.h index 34343b6be98..f5a9c7a7b97 100644 --- a/src/openms/include/OpenMS/CONCEPT/Colorizer.h +++ b/src/openms/include/OpenMS/CONCEPT/Colorizer.h @@ -58,7 +58,7 @@ namespace OpenMS }; // end WindowsOSDefaultColor //extern or static. both throw an error - static const WindowsOSDefaultColor default_color___(); + static const WindowsOSDefaultColor default_color___; } // namespace Internal #endif diff --git a/src/openms/source/CONCEPT/Colorizer.cpp b/src/openms/source/CONCEPT/Colorizer.cpp index 610c3079dcf..098ab86b2d3 100644 --- a/src/openms/source/CONCEPT/Colorizer.cpp +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -81,7 +81,7 @@ namespace OpenMS SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), default_cout_); SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), default_cerr_); } - extern const WindowsOSDefaultColor default_color___(); + //extern const WindowsOSDefaultColor default_color___; } // namespace Internal From 19cb2e5fd08bcd67ac83d7de28f3099f527164cf Mon Sep 17 00:00:00 2001 From: Moritz Berger Date: Tue, 3 May 2022 11:50:57 +0200 Subject: [PATCH 13/23] updatetd colorizer.cpp include path --- src/openms/source/CONCEPT/Colorizer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openms/source/CONCEPT/Colorizer.cpp b/src/openms/source/CONCEPT/Colorizer.cpp index 098ab86b2d3..3c99d39e01c 100644 --- a/src/openms/source/CONCEPT/Colorizer.cpp +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -36,10 +36,10 @@ // include on FU-server (Linux) //#include // include on Windows PC -#include +//#include // include in project -//#include +#include #include #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) From e58e558d106f373a18fee9cc28643d29b62de66e Mon Sep 17 00:00:00 2001 From: morm24 <74315267+morm24@users.noreply.github.com> Date: Tue, 3 May 2022 20:14:48 +0200 Subject: [PATCH 14/23] static WindowsOSDefaultColor struct now working under Windows (still verbose). --- src/openms/include/OpenMS/CONCEPT/Colorizer.h | 2 +- src/openms/source/CONCEPT/Colorizer.cpp | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/openms/include/OpenMS/CONCEPT/Colorizer.h b/src/openms/include/OpenMS/CONCEPT/Colorizer.h index f5a9c7a7b97..401379a7257 100644 --- a/src/openms/include/OpenMS/CONCEPT/Colorizer.h +++ b/src/openms/include/OpenMS/CONCEPT/Colorizer.h @@ -58,7 +58,7 @@ namespace OpenMS }; // end WindowsOSDefaultColor //extern or static. both throw an error - static const WindowsOSDefaultColor default_color___; + //extern const WindowsOSDefaultColor default_color___; } // namespace Internal #endif diff --git a/src/openms/source/CONCEPT/Colorizer.cpp b/src/openms/source/CONCEPT/Colorizer.cpp index 3c99d39e01c..07d45d2b23e 100644 --- a/src/openms/source/CONCEPT/Colorizer.cpp +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -36,10 +36,10 @@ // include on FU-server (Linux) //#include // include on Windows PC -//#include +#include // include in project -#include +//#include #include #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) @@ -81,7 +81,8 @@ namespace OpenMS SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), default_cout_); SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), default_cerr_); } - //extern const WindowsOSDefaultColor default_color___; + //static oder extern. beides geht nicht. + static const WindowsOSDefaultColor default_color___; } // namespace Internal @@ -149,7 +150,7 @@ namespace OpenMS // recover old Console font and set it as new one. if (this->reset_) { - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), OpenMS::Internal::default_color___.default_cout_); + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), Internal::default_color___.default_cout_); } #elif defined(__linux__) || defined(__OSX__) From 9af09bf293251bb2c5594eb7eb5744772f2b855c Mon Sep 17 00:00:00 2001 From: Moritz Berger Date: Wed, 4 May 2022 09:09:53 +0200 Subject: [PATCH 15/23] work in progress, started changing the Console Utils, tidying up Colorizer. --- .../OpenMS/APPLICATIONS/ConsoleUtils.h | 75 ++++++++++++++++++- src/openms/include/OpenMS/CONCEPT/Colorizer.h | 4 +- .../source/APPLICATIONS/ConsoleUtils.cpp | 38 ++++++++++ src/openms/source/CONCEPT/Colorizer.cpp | 63 ++++++++-------- 4 files changed, 148 insertions(+), 32 deletions(-) diff --git a/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h b/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h index d9f83cc9265..d318c524d20 100644 --- a/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h +++ b/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h @@ -48,7 +48,7 @@ namespace OpenMS /// 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 + /// 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 @@ -56,6 +56,18 @@ namespace OpenMS /// @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 int getConsoleWidth(); + + +#ifdef OPENMS_WINDOWSPLATFORM + + /// reset the color of the windows output handle + void resetCoutColor(); + /// reset the color of the windows error handle + void resetCerrColor(); + +#endif + private: /// width of console we are currently in (if not determinable, set to 80 as default) int console_width_; @@ -63,6 +75,9 @@ namespace OpenMS /// read console settings for output shaping int readConsoleSize_(); + /// return console width + int getConsoleWidth_(); + /// returns a console friendly version of input String breakString_(const String& input, const Size indentation, const Size max_lines); @@ -72,8 +87,66 @@ namespace OpenMS /// Copy C'tor ConsoleUtils(const ConsoleUtils &); + /// Destructor + ~ConsoleUtils(); + /// Assignment operator void operator=(ConsoleUtils const&); + +#ifdef OPENMS_WINDOWSPLATFORM + + /// reset the color of both output streams + void resetConsoleColor_(); + + /// Default console color for output stream + int default_cout_; + + /// Default console color for error stream + int default_cerr_; + +#endif + }; + + + + 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::getConsoleWidth_(); + } + + template + IndentedStringStream& operator<<(const T& data) + { + std::stringstream s; + s << data; + const auto& string_to_print = s.str(); + // length? + // needs splitting? + // update current_column_pos_ + + } + + template<> + IndentedStringStream& operator<< (const Colorizer& colorizer) + { + colorizer.applyColor(*stream_); + this->operator<<(colorizer.getDataAsString()); + colorizer.undoColor(*stream_); + } + + private: + std::ostream* stream_; + Size indentation_; + Size max_lines_; + int max_line_width_; + Size current_column_pos_; }; } // namespace OpenMS diff --git a/src/openms/include/OpenMS/CONCEPT/Colorizer.h b/src/openms/include/OpenMS/CONCEPT/Colorizer.h index 401379a7257..b468abac9d4 100644 --- a/src/openms/include/OpenMS/CONCEPT/Colorizer.h +++ b/src/openms/include/OpenMS/CONCEPT/Colorizer.h @@ -96,7 +96,9 @@ namespace OpenMS ~Colorizer(); //@} - void outputToStream(std::ostream& o_stream); + void outputToStream_(std::ostream& o_stream); + + void colorStream_ // operator overloading diff --git a/src/openms/source/APPLICATIONS/ConsoleUtils.cpp b/src/openms/source/APPLICATIONS/ConsoleUtils.cpp index cf5e69335ce..30f71bcd15f 100644 --- a/src/openms/source/APPLICATIONS/ConsoleUtils.cpp +++ b/src/openms/source/APPLICATIONS/ConsoleUtils.cpp @@ -54,16 +54,54 @@ namespace OpenMS { // initialize the console width readConsoleSize_(); + + //if operating OS is Windows: save default output color for cour and cerr +#ifdef OPENMS_WINDOWSPLATFORM + 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() + { +#ifdef OPENMS_WINDOWSPLATFORM + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), default_cout_); + SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), default_cerr_); +#endif } ConsoleUtils::ConsoleUtils(ConsoleUtils const& other) : console_width_(other.console_width_) { + #ifdef OPENMS_WINDOWSPLATFORM + default_cout_ = other.default_cout_; + default_cerr_ = other.default.cerr_; +#endif } void ConsoleUtils::operator=(const ConsoleUtils& other) { console_width_ = other.console_width_; +#ifdef OPENMS_WINDOWSPLATFORM + default_cout_ = other.default_cout_; + default_cerr_ = other.default.cerr_; +#endif + } + + int ConsoleUtils::getConsoleWidth_() + { + return console_width_; } int ConsoleUtils::readConsoleSize_() diff --git a/src/openms/source/CONCEPT/Colorizer.cpp b/src/openms/source/CONCEPT/Colorizer.cpp index 07d45d2b23e..b6bfb9f90b7 100644 --- a/src/openms/source/CONCEPT/Colorizer.cpp +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -34,12 +34,13 @@ // include on FU-server (Linux) -//#include +#include // include on Windows PC -#include +//#include // include in project //#include +#include #include #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) @@ -54,39 +55,39 @@ namespace OpenMS { -#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) - namespace Internal - { +// #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) +// namespace Internal +// { - WindowsOSDefaultColor::WindowsOSDefaultColor() - { - std::cout << "saving default colors...\n"; - CONSOLE_SCREEN_BUFFER_INFO Info; - HANDLE handle_stdout = GetStdHandle(STD_OUTPUT_HANDLE); - HANDLE handle_stderr = GetStdHandle(STD_ERROR_HANDLE); +// WindowsOSDefaultColor::WindowsOSDefaultColor() +// { +// std::cout << "saving default colors...\n"; +// CONSOLE_SCREEN_BUFFER_INFO Info; +// HANDLE handle_stdout = GetStdHandle(STD_OUTPUT_HANDLE); +// HANDLE handle_stderr = GetStdHandle(STD_ERROR_HANDLE); - GetConsoleScreenBufferInfo(handle_stdout, &Info); +// GetConsoleScreenBufferInfo(handle_stdout, &Info); - default_cout_ = Info.wAttributes; +// default_cout_ = Info.wAttributes; - GetConsoleScreenBufferInfo(handle_stderr, &Info); +// GetConsoleScreenBufferInfo(handle_stderr, &Info); - default_cerr_ = Info.wAttributes; +// default_cerr_ = Info.wAttributes; - /// get and remember 2 default colors - } - WindowsOSDefaultColor::~WindowsOSDefaultColor() - { - std::cout << "restoring default colors...\n"; - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), default_cout_); - SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), default_cerr_); - } - //static oder extern. beides geht nicht. - static const WindowsOSDefaultColor default_color___; +// /// get and remember 2 default colors +// } +// WindowsOSDefaultColor::~WindowsOSDefaultColor() +// { +// std::cout << "restoring default colors...\n"; +// SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), default_cout_); +// SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), default_cerr_); +// } +// //static oder extern. beides geht nicht. +// static const WindowsOSDefaultColor default_color___; - } // namespace Internal +// } // namespace Internal -#endif +// #endif @@ -137,7 +138,7 @@ namespace OpenMS */ // Helper function, to manipulate the output stream in class. - void Colorizer::outputToStream(std::ostream& o_stream) + void Colorizer::outputToStream_(std::ostream& o_stream) { #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) @@ -150,7 +151,9 @@ namespace OpenMS // recover old Console font and set it as new one. if (this->reset_) { - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), Internal::default_color___.default_cout_); + instance___.resetCerrColor(); + instance___.resetCourColor(); + //SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), //Internal::default_color___.default_cout_); } #elif defined(__linux__) || defined(__OSX__) @@ -171,7 +174,7 @@ namespace OpenMS std::ostream& operator<<(std::ostream& o_stream, OpenMS::Colorizer& col) { // colorize string with color set in the object - col.outputToStream(o_stream); + col.outputToStream_(o_stream); return o_stream; } From ec0508c995c27c0f509cbe704c73e65d64d9646d Mon Sep 17 00:00:00 2001 From: Moritz Berger Date: Fri, 6 May 2022 13:29:32 +0200 Subject: [PATCH 16/23] Colorizer functionality now working again and tested with linux (windows not tested). Basics of IndentedStringStream implemented. --- .../OpenMS/APPLICATIONS/ConsoleUtils.h | 101 ++++++----- src/openms/include/OpenMS/CONCEPT/Colorizer.h | 122 ++++---------- .../source/APPLICATIONS/ConsoleUtils.cpp | 83 +++++++-- src/openms/source/CONCEPT/Colorizer.cpp | 158 ++++++------------ 4 files changed, 210 insertions(+), 254 deletions(-) diff --git a/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h b/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h index d318c524d20..07d053e095c 100644 --- a/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h +++ b/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h @@ -35,13 +35,14 @@ #pragma once #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 (!) @@ -49,34 +50,54 @@ namespace OpenMS /// 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 indented blocks, which the console does not support - /// 'max_lines' gives the upper limit of lines returned after breaking is finished. + /// '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 int getConsoleWidth(); + const int getConsoleSize() + { + return console_width_; + } + + static ConsoleUtils getInstance() + { + return getInstance_(); + } -#ifdef OPENMS_WINDOWSPLATFORM +//#ifdef OPENMS_WINDOWSPLATFORM /// reset the color of the windows output handle void resetCoutColor(); /// reset the color of the windows error handle void resetCerrColor(); -#endif + /// reset the color of both output streams + void resetConsoleColor(); + + void setCoutColor(int color_code); + + void setCerrColor(int color_code); + +//#endif + + -private: + +/// 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_(); - /// return console width - int getConsoleWidth_(); + static ConsoleUtils& getInstance_(); /// returns a console friendly version of input String breakString_(const String& input, const Size indentation, const Size max_lines); @@ -85,7 +106,7 @@ namespace OpenMS ConsoleUtils(); /// Copy C'tor - ConsoleUtils(const ConsoleUtils &); + ConsoleUtils(const ConsoleUtils&); /// Destructor ~ConsoleUtils(); @@ -93,10 +114,7 @@ namespace OpenMS /// Assignment operator void operator=(ConsoleUtils const&); -#ifdef OPENMS_WINDOWSPLATFORM - - /// reset the color of both output streams - void resetConsoleColor_(); +//#ifdef OPENMS_WINDOWSPLATFORM /// Default console color for output stream int default_cout_; @@ -104,50 +122,51 @@ namespace OpenMS /// Default console color for error stream int default_cerr_; -#endif + +//#endif }; - + + + + + + + + + + + + + + + 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) + 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::getConsoleWidth_(); + max_line_width_ = ConsoleUtils::getInstance().getConsoleSize(); } + + template - IndentedStringStream& operator<<(const T& data) - { - std::stringstream s; - s << data; - const auto& string_to_print = s.str(); - // length? - // needs splitting? - // update current_column_pos_ + IndentedStringStream& operator<<(const T& data); - } - template<> - IndentedStringStream& operator<< (const Colorizer& colorizer) - { - colorizer.applyColor(*stream_); - this->operator<<(colorizer.getDataAsString()); - colorizer.undoColor(*stream_); - } + + template + IndentedStringStream& operator<<(const Colorizer& colorizer); private: std::ostream* stream_; - Size indentation_; - Size max_lines_; + int indentation_; + int max_lines_; int max_line_width_; - Size current_column_pos_; + int current_column_pos_; }; -} // namespace OpenMS +} // namespace OpenMS diff --git a/src/openms/include/OpenMS/CONCEPT/Colorizer.h b/src/openms/include/OpenMS/CONCEPT/Colorizer.h index b468abac9d4..db5dfb7080c 100644 --- a/src/openms/include/OpenMS/CONCEPT/Colorizer.h +++ b/src/openms/include/OpenMS/CONCEPT/Colorizer.h @@ -44,26 +44,7 @@ namespace OpenMS { -#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) - namespace Internal - { - struct WindowsOSDefaultColor { - WindowsOSDefaultColor(); - ~WindowsOSDefaultColor(); - - //int get_default_cout(); - - int default_cout_; - int default_cerr_; - }; // end WindowsOSDefaultColor - - //extern or static. both throw an error - //extern const WindowsOSDefaultColor default_color___; - - } // namespace Internal -#endif - - // enum COLOR for easier Object initialisation. + /// enum COLOR for easier Object initialisation. enum class COLOR { black, @@ -83,28 +64,37 @@ namespace OpenMS */ class Colorizer { - public: - /** @name Constructors and Destructor - */ - //@{ +public: + /// Constructor Colorizer(const COLOR color); /// Copy constructor // Colorizer(const Colorizer &rhs); + ///Assignment Operator + /// Destructor ~Colorizer(); - //@} - void outputToStream_(std::ostream& o_stream); + /// + void outputToStream(std::ostream& o_stream); - void colorStream_ + /// + void colorStream(std::ostream& stream); + /// + void resetColor(std::ostream& stream); - // operator overloading - friend std::ostream& operator<<(std::ostream& o_stream, Colorizer& col); + /// + bool getReset(); + /// + std::string getDataAsString(); + /// insetrion Operator + friend std::ostream& operator<<(std::ostream& o_stream, Colorizer& col); + + /// Bracket Operator Colorizer& operator()() { reset_ = false; @@ -112,7 +102,7 @@ namespace OpenMS return *this; } - + /// Bracket Operator template Colorizer& operator()(T s) { @@ -122,9 +112,14 @@ namespace OpenMS return *this; } - private: +private: + const int color_; + + /// input in Colorizer object to be colored std::stringstream input_; + + /// bool reset_ = true; @@ -142,9 +137,11 @@ namespace OpenMS * */ #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 @@ -163,67 +160,4 @@ namespace OpenMS 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' - - // wahrscheinlich unnörig, da funktionalität unter windows nicht gegeben. - - // definition of colorizing functions. (so function calls like process_string(green(string)) are possible.) - /* - template - extern std::string black(T s = T("")) - { - return color_black(s); - } - template - extern std::string red(T s = T("")) - { - return make_red(s); - } - template - extern std::string green(T s = T("")) - { - return make_green(s); - } - template - extern std::string yellow(T s = T("")) - { - return make_yellow(s); - } - template - extern std::string blue(T s = T("")) - { - return make_blue(s); - } - template - extern std::string magenta(T s = T("")) - { - return make_magenta(s); - } - template - extern std::string cyan(T s = T("")) - { - - return make_cyan(s); - } - template - extern std::string white(T s = T("")) - { - return make_white(s); - } - template - extern std::string def(T s = T("")) - { - return make_default_color(s); - } - - /* - Colorizer red(std::string text); - Colorizer green(std::string text); - Colorizer yellow(std::string text); - Colorizer cyan(std::string text); - - //Colorizer blue(const char *text); - Colorizer red(const char *text); - Colorizer green(const char *text); - Colorizer yellow(const char *text); - Colorizer cyan(const char *text);*/ } // namespace OpenMS diff --git a/src/openms/source/APPLICATIONS/ConsoleUtils.cpp b/src/openms/source/APPLICATIONS/ConsoleUtils.cpp index 30f71bcd15f..345d7de3fbd 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 defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) #include // for GetConsoleScreenBufferInfo() #undef min #undef max @@ -56,7 +56,7 @@ namespace OpenMS readConsoleSize_(); //if operating OS is Windows: save default output color for cour and cerr -#ifdef OPENMS_WINDOWSPLATFORM +#ifdef defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) CONSOLE_SCREEN_BUFFER_INFO Info; HANDLE handle_stdout = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE handle_stderr = GetStdHandle(STD_ERROR_HANDLE); @@ -75,33 +75,33 @@ namespace OpenMS ConsoleUtils::~ConsoleUtils() { -#ifdef OPENMS_WINDOWSPLATFORM - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), default_cout_); - SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), default_cerr_); +#ifdef defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) + setCoutColor(default_cout_); + setCerrColor(default_cerr_); #endif } ConsoleUtils::ConsoleUtils(ConsoleUtils const& other) : console_width_(other.console_width_) { - #ifdef OPENMS_WINDOWSPLATFORM +#ifdef defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) default_cout_ = other.default_cout_; - default_cerr_ = other.default.cerr_; + default_cerr_ = other.default_cerr_; #endif } void ConsoleUtils::operator=(const ConsoleUtils& other) { console_width_ = other.console_width_; -#ifdef OPENMS_WINDOWSPLATFORM +#ifdef defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) default_cout_ = other.default_cout_; default_cerr_ = other.default.cerr_; #endif } - int ConsoleUtils::getConsoleWidth_() + String ConsoleUtils::breakString(const String& input, const Size indentation, const Size max_lines) { - return console_width_; + return getInstance().breakString_(input, indentation, max_lines); } int ConsoleUtils::readConsoleSize_() @@ -131,7 +131,7 @@ namespace OpenMS OPENMS_LOG_DEBUG << "output shaping: COLUMNS env does not exist!" << std::endl; } -#ifdef OPENMS_WINDOWSPLATFORM +#ifdef defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) HANDLE hOut; CONSOLE_SCREEN_BUFFER_INFO SBInfo; hOut = GetStdHandle(STD_OUTPUT_HANDLE); @@ -180,10 +180,10 @@ 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) @@ -247,4 +247,61 @@ namespace OpenMS return ListUtils::concatenate(result, "\n"); } +#ifdef defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) + 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 + + + + +///IndetedStringStream class: + + +template + IndentedStringStream& IndentedStringStream::operator<<(const T& data) + { + std::stringstream s; + s << data; + const auto& string_to_print = s.str(); + // length? + // needs splitting? + // update current_column_pos_ + + + /* + Was ist hier genaau zu tun? wie sollen diese Operatoren aufgerufen werden? + */ + } + +template + IndentedStringStream& IndentedStringStream::operator<<(const Colorizer& colorizer) + { + + colorizer.colorStream(*stream_); + this->operator<<(colorizer.getDataAsString()); + (colorizer.getReset())?:colorizer.resetColor(*stream_); + + + } + + + + + } diff --git a/src/openms/source/CONCEPT/Colorizer.cpp b/src/openms/source/CONCEPT/Colorizer.cpp index b6bfb9f90b7..50e65d6a87e 100644 --- a/src/openms/source/CONCEPT/Colorizer.cpp +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -35,78 +35,28 @@ // include on FU-server (Linux) #include + // include on Windows PC //#include // include in project //#include -#include + + + #include #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) #include + #include #endif -/** -#include -#include -#include -**/ namespace OpenMS { -// #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) -// namespace Internal -// { - -// WindowsOSDefaultColor::WindowsOSDefaultColor() -// { -// std::cout << "saving default colors...\n"; -// 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 -// } -// WindowsOSDefaultColor::~WindowsOSDefaultColor() -// { -// std::cout << "restoring default colors...\n"; -// SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), default_cout_); -// SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), default_cerr_); -// } -// //static oder extern. beides geht nicht. -// static const WindowsOSDefaultColor default_color___; - -// } // namespace Internal - -// #endif - - - - - - - - - - - - - - - /** @name Constructors and Destructor - */ - //@{ // constructor - Colorizer::Colorizer(const COLOR color) : color_((int)color) // color must be in initializer list, because of const keyword + Colorizer::Colorizer(const COLOR color) : + color_((int)color) // color must be in initializer list, because of const keyword { } @@ -118,55 +68,57 @@ namespace OpenMS std::cout << colors_[8]; #endif } - //@} - - /** - \fn getColor(int i) - \details this function return the ANSI escape sequence for the color, saved in the Colorizer Object. - \return std::string contaaining ANSI escape characters for colors - \arg i codes for following colors: - * 0=black - * 1=red - * 2=green - * 3=yellow - * 4=blue - * 5=magenta - * 6=cyan - * 7=white - * 8=default console color (reset) - \return std::string contaaining ANSI escape characters for colors - */ - // Helper function, to manipulate the output stream in class. - void Colorizer::outputToStream_(std::ostream& o_stream) + /// + void Colorizer::colorStream(std::ostream& stream) { #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) - // set color of output - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), this->colors_[this->color_]); + ConsoleUtils::getInstance().setCoutColor(colors_[color_]); +#elif defined(__linux__) || defined(__OSX__) + // write coloring escape codes into the string + stream << this->colors_[this->color_]; +#endif + } - // paste text - o_stream << this->input_.str(); + /// + void Colorizer::resetColor(std::ostream& stream) + { +#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) + ConsoleUtils::getInstance().resetCerrColor(); + ConsoleUtils::getInstance().resetCoutColor(); +#elif defined(__linux__) || defined(__OSX__) + stream << this->colors_[8]; +#endif + } - // recover old Console font and set it as new one. - if (this->reset_) - { - instance___.resetCerrColor(); - instance___.resetCourColor(); - //SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), //Internal::default_color___.default_cout_); - } + /// + bool Colorizer::getReset() + { + return reset_; + } -#elif defined(__linux__) || defined(__OSX__) - o_stream << this->colors_[this->color_]; + /// + 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_) { - o_stream << this->colors_[8]; + resetColor(o_stream); } -#endif + } @@ -174,23 +126,10 @@ namespace OpenMS std::ostream& operator<<(std::ostream& o_stream, OpenMS::Colorizer& col) { // colorize string with color set in the object - col.outputToStream_(o_stream); + col.outputToStream(o_stream); return o_stream; } - - // All color types (Linux/OSX) - /* - Black 30 40 - Red 31 41 - Green 32 42 - Yellow 33 43 - Blue 34 44 - Magenta 35 45 - Cyan 36 46 - White 37 47 - */ - // Objekte des typs colorizer OpenMS::Colorizer black(COLOR::black); OpenMS::Colorizer red(COLOR::red); @@ -203,4 +142,11 @@ namespace OpenMS OpenMS::Colorizer reset_color(COLOR::RESET); //OpenMS::Colorizer reset_color(COLOR::RESET); + + + + + + + } // namespace OpenMS From ef356a54f738018025e82ebbd75669749b2fbd4c Mon Sep 17 00:00:00 2001 From: Moritz Berger Date: Wed, 11 May 2022 10:03:51 +0200 Subject: [PATCH 17/23] kompiliert, fehler in FOrmatierung, und zugriffsfehler innerhalb der breakstring func. keine Testklassen --- .../OpenMS/APPLICATIONS/ConsoleUtils.h | 42 +++++++--- src/openms/include/OpenMS/CONCEPT/Colorizer.h | 2 +- .../source/APPLICATIONS/ConsoleUtils.cpp | 78 +++++++++---------- src/openms/source/APPLICATIONS/TOPPBase.cpp | 46 ++++++----- src/openms/source/CONCEPT/Colorizer.cpp | 56 ++++++++----- 5 files changed, 132 insertions(+), 92 deletions(-) diff --git a/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h b/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h index 07d053e095c..2f04397d537 100644 --- a/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h +++ b/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h @@ -35,7 +35,9 @@ #pragma once #include +#include #include +#include namespace OpenMS { @@ -55,7 +57,7 @@ namespace OpenMS /// @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); const int getConsoleSize() { @@ -97,10 +99,11 @@ namespace OpenMS /// read console settings for output shaping int readConsoleSize_(); - static ConsoleUtils& getInstance_(); + + 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(); @@ -108,8 +111,8 @@ namespace OpenMS /// Copy C'tor ConsoleUtils(const ConsoleUtils&); - /// Destructor - ~ConsoleUtils(); + // /// Destructor + // ~ConsoleUtils(); /// Assignment operator void operator=(ConsoleUtils const&); @@ -151,14 +154,31 @@ namespace OpenMS } + 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_); + + current_column_pos_ = result.back().size() -1; + + + + *stream_ << ListUtils::concatenate(result," "); + + return *this; - template - IndentedStringStream& operator<<(const T& data); - - + } +///741 = offset +///763 = indent - template - IndentedStringStream& operator<<(const Colorizer& colorizer); + IndentedStringStream& operator<<(Colorizer& colorizer); private: std::ostream* stream_; diff --git a/src/openms/include/OpenMS/CONCEPT/Colorizer.h b/src/openms/include/OpenMS/CONCEPT/Colorizer.h index db5dfb7080c..b17c87b3a9a 100644 --- a/src/openms/include/OpenMS/CONCEPT/Colorizer.h +++ b/src/openms/include/OpenMS/CONCEPT/Colorizer.h @@ -80,7 +80,7 @@ namespace OpenMS void outputToStream(std::ostream& o_stream); /// - void colorStream(std::ostream& stream); + void colorStream(std::ostream& stream) const; /// void resetColor(std::ostream& stream); diff --git a/src/openms/source/APPLICATIONS/ConsoleUtils.cpp b/src/openms/source/APPLICATIONS/ConsoleUtils.cpp index 345d7de3fbd..d72c60b9343 100644 --- a/src/openms/source/APPLICATIONS/ConsoleUtils.cpp +++ b/src/openms/source/APPLICATIONS/ConsoleUtils.cpp @@ -37,7 +37,7 @@ #include #include -#ifdef defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) +#ifdef OPENMS_WINDOWSPLATTFORM #include // for GetConsoleScreenBufferInfo() #undef min #undef max @@ -56,7 +56,7 @@ namespace OpenMS readConsoleSize_(); //if operating OS is Windows: save default output color for cour and cerr -#ifdef defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) +#ifdef OPENMS_WINDOWSPLATTFORM CONSOLE_SCREEN_BUFFER_INFO Info; HANDLE handle_stdout = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE handle_stderr = GetStdHandle(STD_ERROR_HANDLE); @@ -75,7 +75,7 @@ namespace OpenMS ConsoleUtils::~ConsoleUtils() { -#ifdef defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) +#ifdef OPENMS_WINDOWSPLATTFORM setCoutColor(default_cout_); setCerrColor(default_cerr_); #endif @@ -84,7 +84,7 @@ namespace OpenMS ConsoleUtils::ConsoleUtils(ConsoleUtils const& other) : console_width_(other.console_width_) { -#ifdef defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) +#ifdef OPENMS_WINDOWSPLATTFORM default_cout_ = other.default_cout_; default_cerr_ = other.default_cerr_; #endif @@ -93,15 +93,15 @@ namespace OpenMS void ConsoleUtils::operator=(const ConsoleUtils& other) { console_width_ = other.console_width_; -#ifdef defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) +#ifdef OPENMS_WINDOWSPLATTFORM default_cout_ = other.default_cout_; default_cerr_ = other.default.cerr_; #endif } - String ConsoleUtils::breakString(const String& input, const Size indentation, const Size max_lines) + OpenMS::StringList ConsoleUtils::breakString(const String& input, const Size indentation, const Size max_lines, const Size curser_pos) { - return getInstance().breakString_(input, indentation, max_lines); + return getInstance().breakString_(input, indentation, max_lines, curser_pos); } int ConsoleUtils::readConsoleSize_() @@ -131,7 +131,7 @@ namespace OpenMS OPENMS_LOG_DEBUG << "output shaping: COLUMNS env does not exist!" << std::endl; } -#ifdef defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) +#ifdef OPENMS_WINDOWSPLATTFORM HANDLE hOut; CONSOLE_SCREEN_BUFFER_INFO SBInfo; hOut = GetStdHandle(STD_OUTPUT_HANDLE); @@ -186,19 +186,33 @@ namespace OpenMS 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 @@ -244,10 +258,11 @@ 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 defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) +#ifdef OPENMS_WINDOWSPLATTFORM void ConsoleUtils::resetConsoleColor() { setCoutColor(default_cout_); @@ -268,36 +283,19 @@ void setCerrColor(int color_code) #endif - - -///IndetedStringStream class: - - -template - IndentedStringStream& IndentedStringStream::operator<<(const T& data) - { - std::stringstream s; - s << data; - const auto& string_to_print = s.str(); - // length? - // needs splitting? - // update current_column_pos_ - - - /* - Was ist hier genaau zu tun? wie sollen diese Operatoren aufgerufen werden? - */ - } - -template - IndentedStringStream& IndentedStringStream::operator<<(const Colorizer& colorizer) + + IndentedStringStream& IndentedStringStream::operator<<(Colorizer& colorizer) { - colorizer.colorStream(*stream_); this->operator<<(colorizer.getDataAsString()); - (colorizer.getReset())?:colorizer.resetColor(*stream_); - + 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 b5ce0036ba9..89e4afed6db 100755 --- a/src/openms/source/APPLICATIONS/TOPPBase.cpp +++ b/src/openms/source/APPLICATIONS/TOPPBase.cpp @@ -541,11 +541,16 @@ namespace OpenMS bool verbose = getFlag_("-helphelp"); String docurl = getDocumentationURL(); + + IndentedStringStream indentedStream0 (cerr, 0,10); // common output - cerr << "\n" + indentedStream0 << "\n" << red("Diese Zeile wurde manuell eingerfuegt. (TOPPBase.cpp zeile 546)") << "\n" - << ConsoleUtils::breakString(tool_name_ + " -- " + tool_description_, 0, 10) << "\n" - << ConsoleUtils::breakString(String("Full documentation: ") + docurl, 0, 10) << "\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()) @@ -560,7 +565,7 @@ namespace OpenMS // 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) { @@ -592,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) { @@ -619,7 +626,7 @@ namespace OpenMS subsection_description = current_TOPP_subsection; } - cerr << yellow(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 { @@ -629,9 +636,9 @@ namespace OpenMS //NAME + ARGUMENT String str_tmp = " -"; - str_tmp += green(it->name) + " " + blue(it->argument); + str_tmp += (it->name) + " " + (it->argument); if (it->required) - str_tmp += blue('*'); + str_tmp += ('*'); if (it->type == ParameterInformation::NEWLINE) str_tmp = ""; @@ -692,7 +699,7 @@ namespace OpenMS || it->type == ParameterInformation::OUTPUT_FILE_LIST) add = " formats"; - addons.push_back(String("valid") + add + ": " + magenta(ListUtils::concatenate(copy, ", "))); // concatenate restrictions by comma + addons.push_back(String("valid") + add + ": " + ListUtils::concatenate(copy, ", ")); // concatenate restrictions by comma } break; @@ -729,14 +736,14 @@ namespace OpenMS { desc_tmp += String(" (") + ListUtils::concatenate(addons, " ") + ")"; } - + IndentedStringStream indentedStream2 (cerr, offset,10); if (it->type == ParameterInformation::TEXT) - cerr << (ConsoleUtils::breakString(red(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) { @@ -747,7 +754,7 @@ namespace OpenMS indent = max((UInt)it->first.size(), indent); } indent += 6; - + //output cerr << "\n" << "The following configuration subsections are valid:" << "\n"; @@ -755,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; } diff --git a/src/openms/source/CONCEPT/Colorizer.cpp b/src/openms/source/CONCEPT/Colorizer.cpp index 50e65d6a87e..e112290e8c2 100644 --- a/src/openms/source/CONCEPT/Colorizer.cpp +++ b/src/openms/source/CONCEPT/Colorizer.cpp @@ -34,29 +34,28 @@ // include on FU-server (Linux) -#include +// #include // include on Windows PC //#include // include in project -//#include +#include #include -#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) - #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 + Colorizer::Colorizer(const COLOR color) : color_((int)color) // color must be in initializer list, because of const keyword { } @@ -70,11 +69,22 @@ namespace OpenMS } /// - void Colorizer::colorStream(std::ostream& stream) + void Colorizer::colorStream(std::ostream& stream) const { #if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) - // set color of output - ConsoleUtils::getInstance().setCoutColor(colors_[color_]); + + 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_]; @@ -84,9 +94,20 @@ namespace OpenMS /// void Colorizer::resetColor(std::ostream& stream) { -#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) - ConsoleUtils::getInstance().resetCerrColor(); - ConsoleUtils::getInstance().resetCoutColor(); +#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 @@ -107,7 +128,7 @@ namespace OpenMS // Helper function, to manipulate the output stream in class. void Colorizer::outputToStream(std::ostream& o_stream) { - ///color the stream (or console) + /// color the stream (or console) colorStream(o_stream); // paste text @@ -118,7 +139,6 @@ namespace OpenMS { resetColor(o_stream); } - } @@ -140,13 +160,7 @@ namespace OpenMS OpenMS::Colorizer cyan(COLOR::cyan); OpenMS::Colorizer white(COLOR::white); OpenMS::Colorizer reset_color(COLOR::RESET); - //OpenMS::Colorizer reset_color(COLOR::RESET); - - - - - - + // OpenMS::Colorizer reset_color(COLOR::RESET); } // namespace OpenMS From c76c8dec76920587c7fadbd5fe303d53db196a83 Mon Sep 17 00:00:00 2001 From: morm24 <74315267+morm24@users.noreply.github.com> Date: Thu, 12 May 2022 11:47:00 +0200 Subject: [PATCH 18/23] Update src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h Co-authored-by: Chris Bielow --- src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h b/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h index 2f04397d537..3ec6e1f25cc 100644 --- a/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h +++ b/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h @@ -166,7 +166,14 @@ namespace OpenMS OpenMS::StringList result = ConsoleUtils::breakString(string_to_print,indentation_,max_lines_,current_column_pos_); - current_column_pos_ = result.back().size() -1; + 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(); + } From 61a9b7d1002a3f9e602596fd7ef97d4e8e2e9a3a Mon Sep 17 00:00:00 2001 From: morm24 <74315267+morm24@users.noreply.github.com> Date: Thu, 12 May 2022 11:47:49 +0200 Subject: [PATCH 19/23] Update src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h Co-authored-by: Chris Bielow --- src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h b/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h index 3ec6e1f25cc..cd133b46507 100644 --- a/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h +++ b/src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h @@ -177,7 +177,7 @@ namespace OpenMS - *stream_ << ListUtils::concatenate(result," "); + *stream_ << ListUtils::concatenate(result, '\n'); return *this; From bf33585cc06dc31f6d94f63951fe3d9f7a27986e Mon Sep 17 00:00:00 2001 From: morm24 <74315267+morm24@users.noreply.github.com> Date: Thu, 12 May 2022 11:48:01 +0200 Subject: [PATCH 20/23] Update src/openms/include/OpenMS/CONCEPT/Colorizer.h Co-authored-by: Chris Bielow --- src/openms/include/OpenMS/CONCEPT/Colorizer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openms/include/OpenMS/CONCEPT/Colorizer.h b/src/openms/include/OpenMS/CONCEPT/Colorizer.h index b17c87b3a9a..eb1f20f0bb7 100644 --- a/src/openms/include/OpenMS/CONCEPT/Colorizer.h +++ b/src/openms/include/OpenMS/CONCEPT/Colorizer.h @@ -98,7 +98,7 @@ namespace OpenMS Colorizer& operator()() { reset_ = false; - this->input_.str(""); // clear the stream + this->input_.str(""); // clear the stringstream return *this; } From 9d833a79cfaa3ae5da24d2451f83911e4116bc48 Mon Sep 17 00:00:00 2001 From: morm24 <74315267+morm24@users.noreply.github.com> Date: Thu, 12 May 2022 11:51:01 +0200 Subject: [PATCH 21/23] Update src/openms/source/APPLICATIONS/ConsoleUtils.cpp Co-authored-by: Chris Bielow --- .../source/APPLICATIONS/ConsoleUtils.cpp | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/src/openms/source/APPLICATIONS/ConsoleUtils.cpp b/src/openms/source/APPLICATIONS/ConsoleUtils.cpp index d72c60b9343..539164e67d7 100644 --- a/src/openms/source/APPLICATIONS/ConsoleUtils.cpp +++ b/src/openms/source/APPLICATIONS/ConsoleUtils.cpp @@ -81,22 +81,8 @@ namespace OpenMS #endif } - ConsoleUtils::ConsoleUtils(ConsoleUtils const& other) : - console_width_(other.console_width_) - { -#ifdef OPENMS_WINDOWSPLATTFORM - default_cout_ = other.default_cout_; - default_cerr_ = other.default_cerr_; -#endif - } - - void ConsoleUtils::operator=(const ConsoleUtils& other) - { - console_width_ = other.console_width_; -#ifdef OPENMS_WINDOWSPLATTFORM - default_cout_ = other.default_cout_; - default_cerr_ = other.default.cerr_; -#endif + ConsoleUtils::ConsoleUtils(ConsoleUtils const& other) = default; + void ConsoleUtils::operator=(const ConsoleUtils& other) = default; } OpenMS::StringList ConsoleUtils::breakString(const String& input, const Size indentation, const Size max_lines, const Size curser_pos) From 5af7fa1934a421680a8a71130e75e172c02cf9e9 Mon Sep 17 00:00:00 2001 From: morm24 <74315267+morm24@users.noreply.github.com> Date: Thu, 12 May 2022 11:52:19 +0200 Subject: [PATCH 22/23] Update src/openms/source/APPLICATIONS/ConsoleUtils.cpp Co-authored-by: Chris Bielow --- src/openms/source/APPLICATIONS/ConsoleUtils.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/openms/source/APPLICATIONS/ConsoleUtils.cpp b/src/openms/source/APPLICATIONS/ConsoleUtils.cpp index 539164e67d7..1100a1dd2e6 100644 --- a/src/openms/source/APPLICATIONS/ConsoleUtils.cpp +++ b/src/openms/source/APPLICATIONS/ConsoleUtils.cpp @@ -76,8 +76,7 @@ namespace OpenMS ConsoleUtils::~ConsoleUtils() { #ifdef OPENMS_WINDOWSPLATTFORM - setCoutColor(default_cout_); - setCerrColor(default_cerr_); + resetConsoleColor(); #endif } From 40835d8e05884cfe257de58475cf146a04a60360 Mon Sep 17 00:00:00 2001 From: morm24 <74315267+morm24@users.noreply.github.com> Date: Thu, 12 May 2022 11:52:32 +0200 Subject: [PATCH 23/23] Update src/openms/source/APPLICATIONS/ConsoleUtils.cpp Co-authored-by: Chris Bielow --- src/openms/source/APPLICATIONS/ConsoleUtils.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/openms/source/APPLICATIONS/ConsoleUtils.cpp b/src/openms/source/APPLICATIONS/ConsoleUtils.cpp index 1100a1dd2e6..d775fb91a05 100644 --- a/src/openms/source/APPLICATIONS/ConsoleUtils.cpp +++ b/src/openms/source/APPLICATIONS/ConsoleUtils.cpp @@ -257,7 +257,6 @@ namespace OpenMS void setCoutColor(int color_code) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_code); - } void setCerrColor(int color_code)