OpenMS
Colorizer.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Chris Bielow, Moritz Berger $
6 // $Authors: Chris Bielow, Moritz Berger $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/config.h>
12 
13 #include <array>
14 #include <iosfwd>
15 #include <sstream>
16 
17 namespace OpenMS
18 {
20  enum class ConsoleColor
21  { // note: the order is important here! See Colorizer::colors_
22  RED,
23  GREEN,
24  YELLOW,
25  BLUE,
26  MAGENTA,
27  CYAN,
28  UNDERLINE,
29  BRIGHT,
30  INVERT,
31  };
32 
70  class OPENMS_DLLAPI Colorizer
71  {
72  public:
74  friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream& o_stream, Colorizer& col);
75  friend class IndentedStream; // to manipulate the internal string data before writing to a stream
76 
78  Colorizer(const ConsoleColor color);
79 
81  Colorizer() = delete;
82 
84  //. Explicitly deleted here, since stringstream member has no copy c'tor either
86  Colorizer(const Colorizer& rhs) = delete;
87 
89  ~Colorizer() = default;
90 
96  {
97  input_.str(""); // clear the internal buffer
98  undo_ = false;
99  undo_all_ = false;
100  undos_only = false;
101  return *this;
102  }
103 
107  template<typename T>
109  {
110  input_.str(""); // clear the internal buffer
111  input_ << s; // add new data
112  undo_ = true;
113  undo_all_ = false;
114  undos_only = false;
115  return *this;
116  }
117 
121 
124 
130  static bool isTTY(const std::ostream& stream);
131 
132  protected:
134  void outputToStream_(std::ostream& o_stream);
135 
137  static void colorStream_(std::ostream& stream, const char* ANSI_command);
138 
141 
144  bool undo_ = true;
145 
148  bool undo_all_ = true;
149 
152  bool undos_only = false;
153 
156  const std::stringstream& getInternalChars_() const
157  {
158  return input_;
159  }
160 
161 
164  void setInternalChars_(const std::string& data)
165  {
166  input_.str(data);
167  }
168 
169 
174  {
175  const char* enable;
176  const char* disable;
177  };
178 
182  inline static const constexpr std::array<ColorWithUndo_, 9> colors_ {
183  ColorWithUndo_ {"\033[91m", "\033[39m"}, // red
184  ColorWithUndo_ {"\033[92m", "\033[39m"}, // green
185  ColorWithUndo_ {"\033[93m", "\033[39m"}, // yellow
186  ColorWithUndo_ {"\033[94m", "\033[39m"}, // blue
187  ColorWithUndo_ {"\033[95m", "\033[39m"}, // magenta
188  ColorWithUndo_ {"\033[96m", "\033[39m"}, // cyan
189  ColorWithUndo_ {"\033[4m", "\033[24m"}, // underline
190  ColorWithUndo_ {"\033[1m", "\033[22m"}, // bright/intensified
191  ColorWithUndo_ {"\033[7m", "\033[27m"}, // invert FG/BG
192  };
193  const char* color_undo_all_ = "\033[0m";
194 
197  std::stringstream input_;
198 
199  }; // class Colorizer
200 
201  // declaration of all global colorizer objects
202  extern OPENMS_DLLAPI Colorizer red;
203  extern OPENMS_DLLAPI Colorizer green;
204  extern OPENMS_DLLAPI Colorizer yellow;
205  extern OPENMS_DLLAPI Colorizer blue;
206  extern OPENMS_DLLAPI Colorizer magenta;
207  extern OPENMS_DLLAPI Colorizer cyan;
208  extern OPENMS_DLLAPI Colorizer bright;
209  extern OPENMS_DLLAPI Colorizer underline;
210  extern OPENMS_DLLAPI Colorizer invert;
211 
217  OPENMS_DLLAPI std::ostream& operator<<(std::ostream& o_stream, OpenMS::Colorizer& col);
218 } // namespace OpenMS
Color and style the fonts shown on cout/cerr (or other streams)
Definition: Colorizer.h:71
std::stringstream input_
Definition: Colorizer.h:197
Colorizer()=delete
Constructor (deleted)
const char * enable
ANSI code to activate the color/style.
Definition: Colorizer.h:175
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:176
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:156
~Colorizer()=default
Destructor.
Colorizer & operator()()
Definition: Colorizer.h:95
Colorizer & undoAll()
prepare this colorizer to reset the console to its default colors/style.
void setInternalChars_(const std::string &data)
Definition: Colorizer.h:164
Colorizer & operator()(T s)
Definition: Colorizer.h:108
const ConsoleColor color_
color of the stream (const; set in C'tor)
Definition: Colorizer.h:140
Colorizer & undo()
Definition: Colorizer.h:174
Class for writing data which spans multiple lines with an indentation for each line (all except the f...
Definition: IndentedStream.h:36
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
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:21
@ 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