OpenMS
Colorizer.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2023.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Chris Bielow, Moritz Berger $
32 // $Authors: Chris Bielow, Moritz Berger $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/config.h>
38 
39 #include <array>
40 #include <iosfwd>
41 #include <sstream>
42 
43 namespace OpenMS
44 {
46  enum class ConsoleColor
47  { // note: the order is important here! See Colorizer::colors_
48  RED,
49  GREEN,
50  YELLOW,
51  BLUE,
52  MAGENTA,
53  CYAN,
54  UNDERLINE,
55  BRIGHT,
56  INVERT,
57  };
58 
96  class OPENMS_DLLAPI Colorizer
97  {
98  public:
100  friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream& o_stream, Colorizer& col);
101  friend class IndentedStream; // to manipulate the internal string data before writing to a stream
102 
104  Colorizer(const ConsoleColor color);
105 
107  Colorizer() = delete;
108 
110  //. Explicitly deleted here, since stringstream member has no copy c'tor either
112  Colorizer(const Colorizer& rhs) = delete;
113 
115  ~Colorizer() = default;
116 
122  {
123  input_.str(""); // clear the internal buffer
124  undo_ = false;
125  undo_all_ = false;
126  undos_only = false;
127  return *this;
128  }
129 
133  template<typename T>
135  {
136  input_.str(""); // clear the internal buffer
137  input_ << s; // add new data
138  undo_ = true;
139  undo_all_ = false;
140  undos_only = false;
141  return *this;
142  }
143 
147 
150 
156  static bool isTTY(const std::ostream& stream);
157 
158  protected:
160  void outputToStream_(std::ostream& o_stream);
161 
163  static void colorStream_(std::ostream& stream, const char* ANSI_command);
164 
167 
170  bool undo_ = true;
171 
174  bool undo_all_ = true;
175 
178  bool undos_only = false;
179 
182  const std::stringstream& getInternalChars_() const
183  {
184  return input_;
185  }
186 
187 
190  void setInternalChars_(const std::string& data)
191  {
192  input_.str(data);
193  }
194 
195 
200  {
201  const char* enable;
202  const char* disable;
203  };
204 
208  inline static const constexpr std::array<ColorWithUndo_, 9> colors_ {
209  ColorWithUndo_ {"\033[91m", "\033[39m"}, // red
210  ColorWithUndo_ {"\033[92m", "\033[39m"}, // green
211  ColorWithUndo_ {"\033[93m", "\033[39m"}, // yellow
212  ColorWithUndo_ {"\033[94m", "\033[39m"}, // blue
213  ColorWithUndo_ {"\033[95m", "\033[39m"}, // magenta
214  ColorWithUndo_ {"\033[96m", "\033[39m"}, // cyan
215  ColorWithUndo_ {"\033[4m", "\033[24m"}, // underline
216  ColorWithUndo_ {"\033[1m", "\033[22m"}, // bright/intensified
217  ColorWithUndo_ {"\033[7m", "\033[27m"}, // invert FG/BG
218  };
219  const char* color_undo_all_ = "\033[0m";
220 
223  std::stringstream input_;
224 
225  }; // class Colorizer
226 
227  // declaration of all global colorizer objects
228  extern OPENMS_DLLAPI Colorizer red;
229  extern OPENMS_DLLAPI Colorizer green;
230  extern OPENMS_DLLAPI Colorizer yellow;
231  extern OPENMS_DLLAPI Colorizer blue;
232  extern OPENMS_DLLAPI Colorizer magenta;
233  extern OPENMS_DLLAPI Colorizer cyan;
234  extern OPENMS_DLLAPI Colorizer bright;
235  extern OPENMS_DLLAPI Colorizer underline;
236  extern OPENMS_DLLAPI Colorizer invert;
237 
243  OPENMS_DLLAPI std::ostream& operator<<(std::ostream& o_stream, OpenMS::Colorizer& col);
244 } // namespace OpenMS
Color and style the fonts shown on cout/cerr (or other streams)
Definition: Colorizer.h:97
std::stringstream input_
Definition: Colorizer.h:223
Colorizer()=delete
Constructor (deleted)
const char * enable
ANSI code to activate the color/style.
Definition: Colorizer.h:201
friend std::ostream & operator<<(std::ostream &o_stream, Colorizer &col)
stream insertion, e.g. std::cout << red() << "this is red" << red.undo();
static bool isTTY(const std::ostream &stream)
const char * disable
ANSO code to undo the color/style.
Definition: Colorizer.h:202
static void colorStream_(std::ostream &stream, const char *ANSI_command)
color the stream using the given color
Colorizer(const ConsoleColor color)
Constructor.
Colorizer(const Colorizer &rhs)=delete
Copy constructor (deleted)
void outputToStream_(std::ostream &o_stream)
write the content of input_ to the stream
const std::stringstream & getInternalChars_() const
Definition: Colorizer.h:182
~Colorizer()=default
Destructor.
Colorizer & operator()()
Definition: Colorizer.h:121
Colorizer & undoAll()
prepare this colorizer to reset the console to its default colors/style.
void setInternalChars_(const std::string &data)
Definition: Colorizer.h:190
Colorizer & operator()(T s)
Definition: Colorizer.h:134
const ConsoleColor color_
color of the stream (const; set in C'tor)
Definition: Colorizer.h:166
Colorizer & undo()
Definition: Colorizer.h:200
Class for writing data which spans multiple lines with an indentation for each line (all except the f...
Definition: IndentedStream.h:62
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48
Colorizer blue
Colorizer green
Colorizer bright
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
Colorizer yellow
ConsoleColor
Text colors/styles supported by Colorizer.
Definition: Colorizer.h:47
@ INVERT
invert foreground and background color (inverting twice stays inverted)
@ BRIGHT
keeps the foreground color, but makes it brighter
Colorizer cyan
Colorizer invert
Colorizer magenta
Colorizer red
Colorizer underline