-
-
Notifications
You must be signed in to change notification settings - Fork 24.6k
Expand file tree
/
Copy pathprint_string.cpp
More file actions
356 lines (312 loc) · 10.5 KB
/
print_string.cpp
File metadata and controls
356 lines (312 loc) · 10.5 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**************************************************************************/
/* print_string.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "print_string.h"
#include "core/core_globals.h"
#include "core/os/os.h"
#include <cstdio>
static PrintHandlerList *print_handler_list = nullptr;
static thread_local bool is_printing = false;
static void __print_fallback(const String &p_string, bool p_err) {
fprintf(p_err ? stderr : stdout, "While attempting to print a message, another message was printed:\n%s\n", p_string.utf8().get_data());
}
void add_print_handler(PrintHandlerList *p_handler) {
_global_lock();
p_handler->next = print_handler_list;
print_handler_list = p_handler;
_global_unlock();
}
void remove_print_handler(const PrintHandlerList *p_handler) {
_global_lock();
PrintHandlerList *prev = nullptr;
PrintHandlerList *l = print_handler_list;
while (l) {
if (l == p_handler) {
if (prev) {
prev->next = l->next;
} else {
print_handler_list = l->next;
}
break;
}
prev = l;
l = l->next;
}
//OS::get_singleton()->print("print handler list is %p\n",print_handler_list);
_global_unlock();
ERR_FAIL_NULL(l);
}
void __print_line(const String &p_string) {
if (!CoreGlobals::print_line_enabled) {
return;
}
if (is_printing) {
__print_fallback(p_string, false);
return;
}
is_printing = true;
OS::get_singleton()->print("%s\n", p_string.utf8().get_data());
_global_lock();
PrintHandlerList *l = print_handler_list;
while (l) {
l->printfunc(l->userdata, p_string, false, false);
l = l->next;
}
_global_unlock();
is_printing = false;
}
void __print_line_rich(const String &p_string) {
if (!CoreGlobals::print_line_enabled) {
return;
}
// Convert a subset of BBCode tags to ANSI escape codes for correct display in the terminal.
// Support of those ANSI escape codes varies across terminal emulators,
// especially for italic and strikethrough.
String output;
int pos = 0;
while (pos <= p_string.length()) {
int brk_pos = p_string.find_char('[', pos);
if (brk_pos < 0) {
brk_pos = p_string.length();
}
String txt = brk_pos > pos ? p_string.substr(pos, brk_pos - pos) : "";
if (brk_pos == p_string.length()) {
output += txt;
break;
}
int brk_end = p_string.find_char(']', brk_pos + 1);
if (brk_end == -1) {
txt += p_string.substr(brk_pos);
output += txt;
break;
}
pos = brk_end + 1;
output += txt;
String tag = p_string.substr(brk_pos + 1, brk_end - brk_pos - 1);
if (tag == "b") {
output += "\u001b[1m";
} else if (tag == "/b") {
output += "\u001b[22m";
} else if (tag == "i") {
output += "\u001b[3m";
} else if (tag == "/i") {
output += "\u001b[23m";
} else if (tag == "u") {
output += "\u001b[4m";
} else if (tag == "/u") {
output += "\u001b[24m";
} else if (tag == "s") {
output += "\u001b[9m";
} else if (tag == "/s") {
output += "\u001b[29m";
} else if (tag == "indent") {
output += " ";
} else if (tag == "/indent") {
output += "";
} else if (tag == "code") {
output += "\u001b[2m";
} else if (tag == "/code") {
output += "\u001b[22m";
} else if (tag == "url") {
output += "";
} else if (tag == "/url") {
output += "";
} else if (tag == "center") {
output += "\n\t\t\t";
} else if (tag == "/center") {
output += "";
} else if (tag == "right") {
output += "\n\t\t\t\t\t\t";
} else if (tag == "/right") {
output += "";
} else if (tag.begins_with("color=")) {
String color_name = tag.trim_prefix("color=");
if (color_name == "black") {
output += "\u001b[30m";
} else if (color_name == "red") {
output += "\u001b[91m";
} else if (color_name == "green") {
output += "\u001b[92m";
} else if (color_name == "lime") {
output += "\u001b[92m";
} else if (color_name == "yellow") {
output += "\u001b[93m";
} else if (color_name == "blue") {
output += "\u001b[94m";
} else if (color_name == "magenta") {
output += "\u001b[95m";
} else if (color_name == "pink") {
output += "\u001b[38;5;218m";
} else if (color_name == "purple") {
output += "\u001b[38;5;98m";
} else if (color_name == "cyan") {
output += "\u001b[96m";
} else if (color_name == "white") {
output += "\u001b[97m";
} else if (color_name == "orange") {
output += "\u001b[38;5;208m";
} else if (color_name == "gray") {
output += "\u001b[90m";
} else {
Color c = Color::from_string(color_name, Color());
output += vformat("\u001b[38;2;%d;%d;%dm", c.r * 255, c.g * 255, c.b * 255);
}
} else if (tag == "/color") {
output += "\u001b[39m";
} else if (tag.begins_with("bgcolor=")) {
String color_name = tag.trim_prefix("bgcolor=");
if (color_name == "black") {
output += "\u001b[40m";
} else if (color_name == "red") {
output += "\u001b[101m";
} else if (color_name == "green") {
output += "\u001b[102m";
} else if (color_name == "lime") {
output += "\u001b[102m";
} else if (color_name == "yellow") {
output += "\u001b[103m";
} else if (color_name == "blue") {
output += "\u001b[104m";
} else if (color_name == "magenta") {
output += "\u001b[105m";
} else if (color_name == "pink") {
output += "\u001b[48;5;218m";
} else if (color_name == "purple") {
output += "\u001b[48;5;98m";
} else if (color_name == "cyan") {
output += "\u001b[106m";
} else if (color_name == "white") {
output += "\u001b[107m";
} else if (color_name == "orange") {
output += "\u001b[48;5;208m";
} else if (color_name == "gray") {
output += "\u001b[100m";
} else {
Color c = Color::from_string(color_name, Color());
output += vformat("\u001b[48;2;%d;%d;%dm", c.r * 255, c.g * 255, c.b * 255);
}
} else if (tag == "/bgcolor") {
output += "\u001b[49m";
} else if (tag.begins_with("fgcolor=")) {
String color_name = tag.trim_prefix("fgcolor=");
if (color_name == "black") {
output += "\u001b[30;40m";
} else if (color_name == "red") {
output += "\u001b[91;101m";
} else if (color_name == "green") {
output += "\u001b[92;102m";
} else if (color_name == "lime") {
output += "\u001b[92;102m";
} else if (color_name == "yellow") {
output += "\u001b[93;103m";
} else if (color_name == "blue") {
output += "\u001b[94;104m";
} else if (color_name == "magenta") {
output += "\u001b[95;105m";
} else if (color_name == "pink") {
output += "\u001b[38;5;218;48;5;218m";
} else if (color_name == "purple") {
output += "\u001b[38;5;98;48;5;98m";
} else if (color_name == "cyan") {
output += "\u001b[96;106m";
} else if (color_name == "white") {
output += "\u001b[97;107m";
} else if (color_name == "orange") {
output += "\u001b[38;5;208;48;5;208m";
} else if (color_name == "gray") {
output += "\u001b[90;100m";
} else {
Color c = Color::from_string(color_name, Color());
output += vformat("\u001b[38;2;%d;%d;%d;48;2;%d;%d;%dm", c.r * 255, c.g * 255, c.b * 255, c.r * 255, c.g * 255, c.b * 255);
}
} else if (tag == "/fgcolor") {
output += "\u001b[39;49m";
} else {
output += "[";
pos = brk_pos + 1;
}
}
output += "\u001b[0m"; // Reset.
if (is_printing) {
__print_fallback(output, false);
return;
}
is_printing = true;
OS::get_singleton()->print_rich("%s\n", output.utf8().get_data());
_global_lock();
PrintHandlerList *l = print_handler_list;
while (l) {
l->printfunc(l->userdata, p_string, false, true);
l = l->next;
}
_global_unlock();
is_printing = false;
}
void print_raw(const String &p_string) {
if (is_printing) {
__print_fallback(p_string, true);
return;
}
is_printing = true;
OS::get_singleton()->print("%s", p_string.utf8().get_data());
is_printing = false;
}
void print_error(const String &p_string) {
if (!CoreGlobals::print_error_enabled) {
return;
}
if (is_printing) {
__print_fallback(p_string, true);
return;
}
is_printing = true;
OS::get_singleton()->printerr("%s\n", p_string.utf8().get_data());
_global_lock();
PrintHandlerList *l = print_handler_list;
while (l) {
l->printfunc(l->userdata, p_string, true, false);
l = l->next;
}
_global_unlock();
is_printing = false;
}
bool is_print_verbose_enabled() {
return OS::get_singleton()->is_stdout_verbose();
}
String stringify_variants(const Span<Variant> &p_vars) {
if (p_vars.is_empty()) {
return String();
}
String result = String(p_vars[0]);
for (const Variant &v : Span(p_vars.ptr() + 1, p_vars.size() - 1)) {
result += ' ';
result += v.operator String();
}
return result;
}