Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
52d7559
Added class and header Colorizer.* into concept. Basic functionality …
Apr 14, 2022
e7ab5b1
Merge branch 'OpenMS:develop' into coloring
morm24 Apr 14, 2022
11df650
updated colorizer class
Apr 22, 2022
38b5ec6
Coloring aktualisiert, lokases verzeichnis auf neusten stand gebracht.
Apr 22, 2022
bfbad5e
added full functionality for windows use
Apr 22, 2022
04ea4f4
added colorizer to sources.cmake
Apr 27, 2022
c5c3447
colorizer in TOPPBase hinzugefügt, Testcodes eingefügt
Apr 28, 2022
87d41b7
changed to colorizing functions, with std::string output rather than …
Apr 28, 2022
2eb89f1
added few comments, clead functions, ready for Pull request (overview)
Apr 29, 2022
a80ff58
Apply suggestions from code review
morm24 May 2, 2022
5749fa6
changed pull request changes, now working with linux. windows functio…
May 2, 2022
5a32c13
corrected include path, addition to last commit: removed ability to c…
May 2, 2022
90b30ac
Error in windows functionality: struct WindowsOSDefaultColor when com…
morm24 May 3, 2022
de98618
struct error should be fixed now.
May 3, 2022
19cb2e5
updatetd colorizer.cpp include path
May 3, 2022
e58e558
static WindowsOSDefaultColor struct now working under Windows (still …
morm24 May 3, 2022
9af09bf
work in progress, started changing the Console Utils, tidying up Colo…
May 4, 2022
ec0508c
Colorizer functionality now working again and tested with linux (wind…
May 6, 2022
ef356a5
kompiliert, fehler in FOrmatierung, und zugriffsfehler innerhalb der …
May 11, 2022
c76c8de
Update src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h
morm24 May 12, 2022
61a9b7d
Update src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h
morm24 May 12, 2022
bf33585
Update src/openms/include/OpenMS/CONCEPT/Colorizer.h
morm24 May 12, 2022
9d833a7
Update src/openms/source/APPLICATIONS/ConsoleUtils.cpp
morm24 May 12, 2022
5af7fa1
Update src/openms/source/APPLICATIONS/ConsoleUtils.cpp
morm24 May 12, 2022
40835d8
Update src/openms/source/APPLICATIONS/ConsoleUtils.cpp
morm24 May 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 127 additions & 8 deletions src/openms/include/OpenMS/APPLICATIONS/ConsoleUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,165 @@
#pragma once

#include <OpenMS/DATASTRUCTURES/String.h>
#include <OpenMS/DATASTRUCTURES/StringListUtils.h>
#include <OpenMS/CONCEPT/Colorizer.h>
#include <iostream>

namespace OpenMS
{

class OPENMS_DLLAPI ConsoleUtils
{
public:
public:
/// make a string console friendly
/// by breaking it into multiple lines according to the console width
/// The 'indentation' gives the number of spaces which is prepended beginning at the second (!)
/// line, so one gets a left aligned block which has some space to the left.
/// An indentation of 0 results in the native console's default behaviour: just break at the end of
/// its width and start a new line.
/// but usually one wants nicely intended blocks, which the console does not support
/// 'max_lines' gives the upper limit of lines returned after breaking is finished.
/// but usually one wants nicely indented blocks, which the console does not support
/// 'max_lines' gives the upper limit of lines returned after breaking is finished.
/// Excess lines are removed and replaced by '...', BUT the last line will be preserved.
/// @param input String to be split
/// @param indentation Number of spaces to use for lines 2 until last line.
/// @param max_lines Limit of output lines (all others are removed)
static String breakString(const String& input, const Size indentation, const Size max_lines);
static OpenMS::StringList breakString(const String& input, const Size indentation, const Size max_lines, const Size curser_pos = 0);

private:
const int getConsoleSize()
{
return console_width_;
}

static ConsoleUtils getInstance()
{
return getInstance_();
}


//#ifdef OPENMS_WINDOWSPLATFORM

/// reset the color of the windows output handle
void resetCoutColor();
/// reset the color of the windows error handle
void resetCerrColor();

/// reset the color of both output streams
void resetConsoleColor();

void setCoutColor(int color_code);

void setCerrColor(int color_code);

//#endif




/// Destructor
~ConsoleUtils();

private:
/// width of console we are currently in (if not determinable, set to 80 as default)
int console_width_;

/// read console settings for output shaping
int readConsoleSize_();


static ConsoleUtils& getInstance_();

/// returns a console friendly version of input
String breakString_(const String& input, const Size indentation, const Size max_lines);
OpenMS::StringList breakString_(const String& input, const Size indentation, const Size max_lines, const Size curser_pos);

/// C'tor
ConsoleUtils();

/// Copy C'tor
ConsoleUtils(const ConsoleUtils &);
ConsoleUtils(const ConsoleUtils&);

// /// Destructor
// ~ConsoleUtils();

/// Assignment operator
void operator=(ConsoleUtils const&);

//#ifdef OPENMS_WINDOWSPLATFORM

/// Default console color for output stream
int default_cout_;

/// Default console color for error stream
int default_cerr_;


//#endif
};

} // namespace OpenMS
















class IndentedStringStream
{
public:
IndentedStringStream(std::ostream& stream, const Size indentation, const Size max_lines) : stream_(&stream), indentation_(indentation), max_lines_(max_lines), current_column_pos_(0)
{
max_line_width_ = ConsoleUtils::getInstance().getConsoleSize();
}


template<typename T>
IndentedStringStream& operator<<(const T& data)
{

std::stringstream s;
s << data;
const std::string& string_to_print = s.str();

//wie viele zeilen sind es / viel viele zeichen in letrzter zeile

OpenMS::StringList result = ConsoleUtils::breakString(string_to_print,indentation_,max_lines_,current_column_pos_);

if (result.size()>=2)
{ // we completed the previous line, so start counting from the latest incomplete line
current_column_pos_ = result.back().size();
}
else
{ // only one line; simply forward the column position
current_column_pos_ += result.back().size();
}



*stream_ << ListUtils::concatenate(result, '\n');

return *this;

}
///741 = offset
///763 = indent

IndentedStringStream& operator<<(Colorizer& colorizer);

private:
std::ostream* stream_;
int indentation_;
int max_lines_;
int max_line_width_;
int current_column_pos_;
};


} // namespace OpenMS
163 changes: 163 additions & 0 deletions src/openms/include/OpenMS/CONCEPT/Colorizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// --------------------------------------------------------------------------
// OpenMS -- Open-Source Mass Spectrometry
// --------------------------------------------------------------------------
// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
// ETH Zurich, and Freie Universitaet Berlin 2002-2022.
//
// This software is released under a three-clause BSD license:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of any author or any participating institution
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
// For a full list of authors, refer to the file AUTHORS.
// --------------------------------------------------------------------------
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
// INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// --------------------------------------------------------------------------
// $Maintainer: Moritz Berger, Tetana Krymovska$
// $Authors: Moritz Berger, Tetana Krymovska$
// --------------------------------------------------------------------------

#pragma once

//#include <OpenMS/KERNEL/MSExperiment.h>

#include <array>
#include <iosfwd>
#include <sstream>
//#include <OPENMS_DLLAPI>


namespace OpenMS
{
/// enum COLOR for easier Object initialisation.
enum class COLOR
{
black,
red,
green,
yellow,
blue,
magenta,
cyan,
white,
RESET, ///< reset the color to the previous (default?) color
};

/**
* @brief A class, that provides options for colored output with the "<<" operator for output streams (cout, cerr)
*
*/
class Colorizer
{
public:
/// Constructor
Colorizer(const COLOR color);

/// Copy constructor
// Colorizer(const Colorizer &rhs);

///Assignment Operator

/// Destructor
~Colorizer();

///
void outputToStream(std::ostream& o_stream);

///
void colorStream(std::ostream& stream) const;

///
void resetColor(std::ostream& stream);

///
bool getReset();

///
std::string getDataAsString();

/// insetrion Operator
friend std::ostream& operator<<(std::ostream& o_stream, Colorizer& col);

/// Bracket Operator
Colorizer& operator()()
{
reset_ = false;
this->input_.str(""); // clear the stringstream
return *this;
}

/// Bracket Operator
template<typename T>
Colorizer& operator()(T s)
{
this->input_.str(""); // clear the stringstream
this->input_ << s; // add new data
reset_ = true;
return *this;
}

private:

const int color_;

/// input in Colorizer object to be colored
std::stringstream input_;

///
bool reset_ = true;


/**
* @brief constant string array which saves the Linux color codes.
* 0=black
* 1=red
* 2=green
* 3=yellow
* 4=blue
* 5=magenta
* 6=cyan
* 7=white
* 8=default console color (reset)
*
*/
#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64)
///
inline static constexpr std::array<const int, 9> colors_ {16, 12, 10, 14, 9, 13, 11, 15, 15};

#elif defined(__linux__) || defined(__OSX__)
///
inline static constexpr std::array<const char*, 9> colors_ {"\033[30m", "\033[31m", "\033[32m", "\033[33m", "\033[34m", "\033[35m", "\033[36m", "\033[37m", "\033[0m"};

#endif
};


// declaration of all colorizer object.
extern /*OPENMS_DLLAPI*/ Colorizer black;
extern /*OPENMS_DLLAPI*/ Colorizer red;
extern /*OPENMS_DLLAPI*/ Colorizer green;
extern /*OPENMS_DLLAPI*/ Colorizer yellow;
extern /*OPENMS_DLLAPI*/ Colorizer blue;
extern /*OPENMS_DLLAPI*/ Colorizer magenta;
extern /*OPENMS_DLLAPI*/ Colorizer cyan;
extern /*OPENMS_DLLAPI*/ Colorizer white;
extern /*OPENMS_DLLAPI*/ Colorizer reset_color; ///< reset the color to default, alias for 'make_default_color'
//extern /*OPENMS_DLLAPI*/ Colorizer default_color; ///< reset the color to default, alias for 'reset_color'

} // namespace OpenMS
1 change: 1 addition & 0 deletions src/openms/include/OpenMS/CONCEPT/sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading