-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextModeFormatting.h
217 lines (175 loc) · 5.28 KB
/
textModeFormatting.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/// textModeFormatting.h
// Version 2.0
/** Utility to produce colored output in a text mode window.
* It provides an intuitive way to use ANSI color codes (ISO/IEC 6429).
* It can be disabled at compile time with: #define TEXT_MODE_FORMATTING_OFF
*/
/* Copyright (C) 2012-2015 Pietro Mele
* Released under a BSD license.
*/
/* Usage example:
#include "textModeFormatting.h"
using namespace textModeFormatting;
cout << Format(ny) << "Text..." << Reset();
cout << Format(2) << "Text..." << Reset();
*/
#ifndef TEXT_MODE_FORMATTING_H
#define TEXT_MODE_FORMATTING_H
#ifndef TEXT_MODE_FORMATTING_OFF
#include <string>
#endif // TEXT_MODE_FORMATTING_OFF
#ifdef _WIN32
#include <tchar.h>
#include <strsafe.h>
#include <windows.h>
#endif
namespace textModeFormatting
{
// HTML format
static const int nHtmlLevels = 64;
static const char htmlLevel[][nHtmlLevels] = {
"color:grey;", /* not used */
"color:cyan;", /* VERBOSE_ */
"color:white;", /* DETAIL_ */
"color:green;", /* INFO_ */
"color:yellow;", /* WARNING_ */
"color:red;", /* ERROR_ */
"background-color:yellow;color:red;", /* SEVERE_ */
"background-color:red;color:yellow;", /* FATAL_ */
"background-color:grey;" /* NO_LEVEL_ */
};
#ifndef _WIN32
// Predefined text formats
static const char
d[] = "0;37m", /* detail, grey */
n[] = "0m", /* normal, white */
b[] = "1m", /* bold, white */
ng[] = "0;32m", /* normal, green */
bg[] = "1;32m", /* bold, green */
nc[] = "0;36m", /* normal, cyan */
bc[] = "1;36m", /* bold, cyan */
ny[] = "0;33m", /* normal, yellow */
by[] = "1;33m", /* bold, yellow */
nr[] = "0;31m", /* normal, red */
br[] = "1;31m", /* bold, red */
gb[] = "7;32m", /* green background */
yb[] = "7;33m", /* yellow background */
rb[] = "7;31m", /* red background */
bly[] = "5;33m", /* blinking yellow */
blr[] = "5;31m"; /* blinking red */
#ifdef TEXTMODEFORMATTING_DEFAULT_COLORS
static const int nLevels = 16;
// Predefined text formats as levels
static const char level[][nLevels] = {
"0;37m", /* detail, grey */
"0m", /* normal, white */
"1m", /* bold, white */
"0;32m", /* normal, green */
"1;32m", /* bold, green */
"0;36m", /* normal, cyan */
"1;36m", /* bold, cyan */
"0;33m", /* normal, yellow */
"1;33m", /* bold, yellow */
"0;31m", /* normal, red */
"1;31m", /* bold, red */
"7;32m", /* green background */
"7;33m", /* yellow background */
"7;31m", /* red background */
"5;33m", /* blinking yellow */
"5;31m" /* blinking red */
};
#else // TEXTMODEFORMATTING_DEFAULT_COLORS
static const int nLevels = 8;
// Predefined text formats as levels
static const char level[][nLevels] = {
"0m", /* not used */
"0;36m", /* VERBOSE_, detail, cyan */
"0m", /* DETAIL_, normal, white */
"0;32m", /* INFO_, normal, green */
"0;33m", /* WARNING_, normal, yellow */
"1;31m", /* ERROR_, bold, red */
"7;31m", /* SEVERE_, red background */
"5;31m", /* FATAL_, blinking red */
"7;37m" /* NO_LEVEL_, normal, grey background */
};
#endif // TEXTMODEFORMATTING_DEFAULT_COLORS
#else // _WIN32
// Predefined text formats
static const int
d = 7, /* detail, grey */
n = 7, /* normal, white */
b = 7, /* bold, white */
ng = 10, /* normal, green */
bg = 10, /* bold, green */
nc = 11, /* normal, cyan */
bc = 11, /* bold, cyan */
ny = 14, /* normal, yellow */
by = 14, /* bold, yellow */
nr = 12, /* normal, red */
br = 12, /* bold, red */
gb = 10, /* green background */
yb = 14, /* yellow background */
rb = 12, /* red background */
bly = 14, /* blinking yellow */
blr = 12; /* blinking red */
static const int nLevels = 8;
// Predefined text formats as levels
static const int level[][nLevels] = {
{ 11 }, /* not used */
{ 11 }, /* VERBOSE_, cyan */
{ 07 }, /* DETAIL_, white */
{ 10 }, /* INFO_, green */
{ 14 }, /* WARNING_, yellow */
{ 12 }, /* ERROR_, red */
{ 13 }, /* SEVERE_, purple */
{ 13 }, /* FATAL_, purple */
{ 07 } /* NO_LEVEL_, white */
};
#endif // _WIN32
#ifndef TEXT_MODE_FORMATTING_OFF
#ifndef _WIN32
static const char* Format(const char* _format) {
static std::string format;
format = "\033[";
format.append(_format);
return format.c_str();
}
static const char* Format(int _formatId) {
static std::string format;
format = "\033[";
format.append(level[_formatId%nLevels]);
return format.c_str();
}
static const char* Reset() {
static char format[] = "\033[0m\0";
return format;
}
#else // _WIN32
static const char* Format(int _formatId) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), level[_formatId%nLevels][0]);
static char format[] = "";
return format;
}
static const char* Reset() {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 07);
static char format[] = "";
return format;
}
#endif // _WIN32
#else // TEXT_MODE_FORMATTING_OFF
static const char* Format(const char* _format) {
static char format[] = "";
return format;
}
static const char* Format(int _formatId) {
static char format[] = "";
return format;
}
static const char* Reset() {
static char format[] = "";
return format;
}
#endif // TEXT_MODE_FORMATTING_OFF
} // textModeFormatting
#endif // TEXT_MODE_FORMATTING_H