JAPAn
Just Another Parity Analyzer
Loading...
Searching...
No Matches
QwColor.h
Go to the documentation of this file.
1/*!
2 * \file QwColor.h
3 * \brief ANSI color codes and color management for terminal output
4 */
5
6#pragma once
7
8// System headers
9#include <map>
10#include <cstdio>
11#include <iostream>
12
13// This header contains the terminals color codes.
14// This might be useful to distinguish many output of QwAnalysis into
15// terminals.
16
17// Simply use printf function
18// printf("%s Bold text print %s\n", BOLD, NORMAL);
19// printf("%s RED text %s\n", RED, NORMAL);
20
21// detailed information
22// see http://www.faqs.org/docs/abs/HTML/colorizing.html
23// http://linuxgazette.net/issue65/padala.html
24
25// Also see a listing of the full ANSI escape sequences at:
26// http://en.wikipedia.org/wiki/ANSI_escape_code
27
28// Note that you should replace "\E" with "\033" as the
29// escape character because only "\033" conforms to the ISO
30// standard, while "\E" is a GNU extension.
31
32#define BLACK "\033[30m"
33#define RED "\033[31m"
34#define GREEN "\033[32m"
35#define BROWN "\033[33m"
36#define BLUE "\033[34m"
37#define MAGENTA "\033[35m"
38#define CYAN "\033[36m"
39#define WHITE "\033[37m"
40
41#define BOLD "\033[1m"
42/* Code "\033[2m" sets the intensity to faint, but it is not
43 * universally supported.
44 * Code "\033[22m" sets the intensity to normal (not bold or faint).
45 */
46
47/* The bolded codes below could alternately be achieved by
48 * two codes; i.e. BOLDRED could be done by the concatenation
49 * of BOLD and RED.
50 */
51#define BOLDRED "\033[31;1m"
52#define BOLDGREEN "\033[32;1m"
53#define BOLDBROWN "\033[33;1m"
54#define BOLDBLUE "\033[34;1m"
55#define BOLDMAGENTA "\033[35;1m"
56#define BOLDCYAN "\033[36;1m"
57#define BOLDWHITE "\033[37;1m"
58
59#define BACKRED "\033[41m"
60#define BACKGREEN "\033[42m"
61#define BACKBLUE "\033[44m"
62
63/* Code "\033[0m" clears all formatting codes.
64 */
65#define NORMAL "\033[0m"
66
67// How to use the Color Code with printf
68// printf("%sName%s", BOLD, NORMAL);
69// printf("[%s%+4.2e%s +- %s%+4.2e%s]", RED, out[0], NORMAL, BLUE, out[1], NORMAL);
70
71
72// All colors need to go in a namespace because of conflicts with ROOT colors.
73// Really, ROOT should have its colors in a namespace.
85
86// Map of the enum to the defined strings
87typedef std::map<Qw::EQwColor, std::string> QwColorMap;
88
89/**
90 * \class QwColor
91 * \ingroup QwAnalysis
92 * \brief A color changing class for the output stream
93 *
94 * This class changes the color in the output stream by inserting 'magic'
95 * escape sequences that are interpreted by smart terminals. On dumb terminals
96 * this will probably just print garbage. To use the color, a QwColor object
97 * should be streamed to the output stream:
98 * \code
99 * QwMessage << "Hello, " << QwColor(Qw::kRed) << "Qweak!" << QwLog::endl;
100 * \endcode
101 */
103{
104 public:
105
106 /// Default constructor
108 : foreground(f) { };
109 virtual ~QwColor() { };
110
111 /// \brief Output stream operator
112 friend std::ostream& operator<<(std::ostream& out, const QwColor& color);
113
114 protected:
115
117 QwColorMap map;
118 map[Qw::kBlack] = BLACK;
119 map[Qw::kRed] = RED;
120 map[Qw::kGreen] = GREEN;
121 map[Qw::kBrown] = BROWN;
122 map[Qw::kBlue] = BLUE;
123 map[Qw::kMagenta] = MAGENTA;
124 map[Qw::kCyan] = CYAN;
125 map[Qw::kWhite] = WHITE;
126 //
127 map[Qw::kBold] = BOLD;
128 //
129 map[Qw::kBoldRed] = BOLDRED;
132 map[Qw::kBoldBlue] = BOLDBLUE;
134 map[Qw::kBoldCyan] = BOLDCYAN;
136 //
137 map[Qw::kBackRed] = BACKRED;
139 map[Qw::kBackBlue] = BACKBLUE;
140 //
141 map[Qw::kDefaultForeground] = "\033[39m";
142 map[Qw::kDefaultBackground] = "\033[49m";
143 //
144 map[Qw::kNormal] = NORMAL;
145 return map;
146 };
148
149 private:
150
151 Qw::EQwColor foreground; ///< Foreground color
152
153}; // class QwColor
154
155/// Output stream operator which uses the enum-to-escape-code mapping
156inline std::ostream& operator<<(std::ostream& out, const QwColor& color)
157{
158 return out << color.kColorMap[color.foreground];
159}
#define BOLDBLUE
Definition QwColor.h:54
#define NORMAL
Definition QwColor.h:65
std::map< Qw::EQwColor, std::string > QwColorMap
Definition QwColor.h:87
#define BOLD
Definition QwColor.h:41
#define BOLDGREEN
Definition QwColor.h:52
#define BOLDBROWN
Definition QwColor.h:53
#define MAGENTA
Definition QwColor.h:37
#define BLUE
Definition QwColor.h:36
#define BACKRED
Definition QwColor.h:59
#define BLACK
Definition QwColor.h:32
#define WHITE
Definition QwColor.h:39
#define RED
Definition QwColor.h:33
#define BACKGREEN
Definition QwColor.h:60
#define BOLDWHITE
Definition QwColor.h:57
#define BROWN
Definition QwColor.h:35
#define BOLDRED
Definition QwColor.h:51
#define BOLDMAGENTA
Definition QwColor.h:55
#define GREEN
Definition QwColor.h:34
#define BACKBLUE
Definition QwColor.h:61
#define CYAN
Definition QwColor.h:38
#define BOLDCYAN
Definition QwColor.h:56
std::ostream & operator<<(std::ostream &out, const QwColor &color)
Output stream operator which uses the enum-to-escape-code mapping.
Definition QwColor.h:156
Definition QwColor.h:74
EQwColor
Definition QwColor.h:76
@ kDefaultBackground
Definition QwColor.h:80
@ kWhite
Definition QwColor.h:77
@ kBackBlue
Definition QwColor.h:80
@ kCyan
Definition QwColor.h:77
@ kBold
Definition QwColor.h:78
@ kBoldCyan
Definition QwColor.h:79
@ kBrown
Definition QwColor.h:77
@ kBoldWhite
Definition QwColor.h:79
@ kBoldRed
Definition QwColor.h:79
@ kBoldGreen
Definition QwColor.h:79
@ kBoldBrown
Definition QwColor.h:79
@ kBoldBlue
Definition QwColor.h:79
@ kBoldMagenta
Definition QwColor.h:79
@ kBlue
Definition QwColor.h:77
@ kRed
Definition QwColor.h:77
@ kGreen
Definition QwColor.h:77
@ kDefaultForeground
Definition QwColor.h:77
@ kBackRed
Definition QwColor.h:80
@ kBackGreen
Definition QwColor.h:80
@ kBlack
Definition QwColor.h:77
@ kMagenta
Definition QwColor.h:77
@ kNormal
Definition QwColor.h:81
A color changing class for the output stream.
Definition QwColor.h:103
QwColor(const Qw::EQwColor f=Qw::kDefaultForeground, const Qw::EQwColor b=Qw::kDefaultBackground)
Default constructor.
Definition QwColor.h:107
static QwColorMap CreateColorMap()
Definition QwColor.h:116
virtual ~QwColor()
Definition QwColor.h:109
static QwColorMap kColorMap
Definition QwColor.h:147
Qw::EQwColor foreground
Foreground color.
Definition QwColor.h:151
friend std::ostream & operator<<(std::ostream &out, const QwColor &color)
Output stream operator.
Definition QwColor.h:156