1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
27 * Helpers for coloring output.
30 #ifndef _FORMATTER_HPP_
31 #define _FORMATTER_HPP_
40 * See also http://bytes.com/topic/c/answers/63822-design-question-little-c-header-colorizing-text-linux-comments-ideas
45 virtual ~Attribute() {}
47 virtual void apply(std::ostream &) const {}
60 virtual ~Formatter() {}
62 virtual Attribute *normal(void) const { return new Attribute; }
63 virtual Attribute *bold(void) const { return new Attribute; }
64 virtual Attribute *italic(void) const { return new Attribute; }
65 virtual Attribute *strike(void) const { return new Attribute; }
66 virtual Attribute *color(Color) const { return new Attribute; }
70 class AnsiAttribute : public Attribute {
74 AnsiAttribute(const char *_escape) : escape(_escape) {}
75 void apply(std::ostream& os) const {
76 os << "\33[" << escape;
82 * Formatter for plain-text files which outputs ANSI escape codes. See
83 * http://en.wikipedia.org/wiki/ANSI_escape_code for more information
84 * concerning ANSI escape codes.
86 class AnsiFormatter : public Formatter {
89 virtual Attribute *normal(void) const { return new AnsiAttribute("0m"); }
90 virtual Attribute *bold(void) const { return new AnsiAttribute("1m"); }
91 virtual Attribute *italic(void) const { return new AnsiAttribute("3m"); }
92 virtual Attribute *strike(void) const { return new AnsiAttribute("9m"); }
93 virtual Attribute *color(Color c) const {
94 static const char *color_escapes[] = {
99 return new AnsiAttribute(color_escapes[c]);
104 inline std::ostream& operator<<(std::ostream& os, const Attribute *attr) {
116 #ifndef COMMON_LVB_LEADING_BYTE
117 #define COMMON_LVB_LEADING_BYTE 0x0100
120 #ifndef COMMON_LVB_TRAILING_BYTE
121 #define COMMON_LVB_TRAILING_BYTE 0x0200
124 #ifndef COMMON_LVB_GRID_HORIZONTAL
125 #define COMMON_LVB_GRID_HORIZONTAL 0x0400
128 #ifndef COMMON_LVB_GRID_LVERTICAL
129 #define COMMON_LVB_GRID_LVERTICAL 0x0800
132 #ifndef COMMON_LVB_GRID_RVERTICAL
133 #define COMMON_LVB_GRID_RVERTICAL 0x1000
136 #ifndef COMMON_LVB_REVERSE_VIDEO
137 #define COMMON_LVB_REVERSE_VIDEO 0x4000
140 #ifndef COMMON_LVB_UNDERSCORE
141 #define COMMON_LVB_UNDERSCORE 0x8000
145 class WindowsAttribute : public Attribute {
149 WindowsAttribute(WORD _wAttributes) : wAttributes(_wAttributes) {}
150 void apply(std::ostream& os) const {
151 DWORD nStdHandleOutput;
152 if (os == std::cout) {
153 nStdHandleOutput = STD_OUTPUT_HANDLE;
154 } else if (os == std::cerr) {
155 nStdHandleOutput = STD_ERROR_HANDLE;
159 HANDLE hConsoleOutput = GetStdHandle(nStdHandleOutput);
160 if (hConsoleOutput == INVALID_HANDLE_VALUE) {
164 SetConsoleTextAttribute(hConsoleOutput, wAttributes);
170 * Formatter for the Windows Console.
172 class WindowsFormatter : public Formatter {
175 virtual Attribute *normal(void) const { return new WindowsAttribute(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); }
176 virtual Attribute *bold(void) const { return new WindowsAttribute(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); }
177 virtual Attribute *italic(void) const { return new WindowsAttribute(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); }
178 virtual Attribute *strike(void) const { return new WindowsAttribute(COMMON_LVB_REVERSE_VIDEO | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); }
179 virtual Attribute *color(Color c) const {
180 static const WORD color_escapes[] = {
181 FOREGROUND_RED | FOREGROUND_INTENSITY,
182 FOREGROUND_GREEN | FOREGROUND_INTENSITY,
183 FOREGROUND_BLUE | FOREGROUND_INTENSITY,
185 return new WindowsAttribute(color_escapes[c]);
192 inline Formatter *defaultFormatter(bool color = true) {
195 return new WindowsFormatter;
197 return new AnsiFormatter;
200 return new Formatter;
205 } /* namespace formatter */
208 #endif /* _FORMATTER_HPP_ */