1 | /**************************************************************************/ |
2 | /* print_string.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #include "print_string.h" |
32 | |
33 | #include "core/core_globals.h" |
34 | #include "core/os/os.h" |
35 | |
36 | #include <stdio.h> |
37 | |
38 | static PrintHandlerList *print_handler_list = nullptr; |
39 | |
40 | void add_print_handler(PrintHandlerList *p_handler) { |
41 | _global_lock(); |
42 | p_handler->next = print_handler_list; |
43 | print_handler_list = p_handler; |
44 | _global_unlock(); |
45 | } |
46 | |
47 | void remove_print_handler(const PrintHandlerList *p_handler) { |
48 | _global_lock(); |
49 | |
50 | PrintHandlerList *prev = nullptr; |
51 | PrintHandlerList *l = print_handler_list; |
52 | |
53 | while (l) { |
54 | if (l == p_handler) { |
55 | if (prev) { |
56 | prev->next = l->next; |
57 | } else { |
58 | print_handler_list = l->next; |
59 | } |
60 | break; |
61 | } |
62 | prev = l; |
63 | l = l->next; |
64 | } |
65 | //OS::get_singleton()->print("print handler list is %p\n",print_handler_list); |
66 | |
67 | _global_unlock(); |
68 | ERR_FAIL_NULL(l); |
69 | } |
70 | |
71 | void __print_line(String p_string) { |
72 | if (!CoreGlobals::print_line_enabled) { |
73 | return; |
74 | } |
75 | |
76 | OS::get_singleton()->print("%s\n" , p_string.utf8().get_data()); |
77 | |
78 | _global_lock(); |
79 | PrintHandlerList *l = print_handler_list; |
80 | while (l) { |
81 | l->printfunc(l->userdata, p_string, false, false); |
82 | l = l->next; |
83 | } |
84 | |
85 | _global_unlock(); |
86 | } |
87 | |
88 | void __print_line_rich(String p_string) { |
89 | if (!CoreGlobals::print_line_enabled) { |
90 | return; |
91 | } |
92 | |
93 | // Convert a subset of BBCode tags to ANSI escape codes for correct display in the terminal. |
94 | // Support of those ANSI escape codes varies across terminal emulators, |
95 | // especially for italic and strikethrough. |
96 | String p_string_ansi = p_string; |
97 | |
98 | p_string_ansi = p_string_ansi.replace("[b]" , "\u001b[1m" ); |
99 | p_string_ansi = p_string_ansi.replace("[/b]" , "\u001b[22m" ); |
100 | p_string_ansi = p_string_ansi.replace("[i]" , "\u001b[3m" ); |
101 | p_string_ansi = p_string_ansi.replace("[/i]" , "\u001b[23m" ); |
102 | p_string_ansi = p_string_ansi.replace("[u]" , "\u001b[4m" ); |
103 | p_string_ansi = p_string_ansi.replace("[/u]" , "\u001b[24m" ); |
104 | p_string_ansi = p_string_ansi.replace("[s]" , "\u001b[9m" ); |
105 | p_string_ansi = p_string_ansi.replace("[/s]" , "\u001b[29m" ); |
106 | |
107 | p_string_ansi = p_string_ansi.replace("[indent]" , " " ); |
108 | p_string_ansi = p_string_ansi.replace("[/indent]" , "" ); |
109 | p_string_ansi = p_string_ansi.replace("[code]" , "\u001b[2m" ); |
110 | p_string_ansi = p_string_ansi.replace("[/code]" , "\u001b[22m" ); |
111 | p_string_ansi = p_string_ansi.replace("[url]" , "" ); |
112 | p_string_ansi = p_string_ansi.replace("[/url]" , "" ); |
113 | p_string_ansi = p_string_ansi.replace("[center]" , "\n\t\t\t" ); |
114 | p_string_ansi = p_string_ansi.replace("[/center]" , "" ); |
115 | p_string_ansi = p_string_ansi.replace("[right]" , "\n\t\t\t\t\t\t" ); |
116 | p_string_ansi = p_string_ansi.replace("[/right]" , "" ); |
117 | |
118 | if (p_string_ansi.contains("[color" )) { |
119 | p_string_ansi = p_string_ansi.replace("[color=black]" , "\u001b[30m" ); |
120 | p_string_ansi = p_string_ansi.replace("[color=red]" , "\u001b[91m" ); |
121 | p_string_ansi = p_string_ansi.replace("[color=green]" , "\u001b[92m" ); |
122 | p_string_ansi = p_string_ansi.replace("[color=lime]" , "\u001b[92m" ); |
123 | p_string_ansi = p_string_ansi.replace("[color=yellow]" , "\u001b[93m" ); |
124 | p_string_ansi = p_string_ansi.replace("[color=blue]" , "\u001b[94m" ); |
125 | p_string_ansi = p_string_ansi.replace("[color=magenta]" , "\u001b[95m" ); |
126 | p_string_ansi = p_string_ansi.replace("[color=pink]" , "\u001b[38;5;218m" ); |
127 | p_string_ansi = p_string_ansi.replace("[color=purple]" , "\u001b[38;5;98m" ); |
128 | p_string_ansi = p_string_ansi.replace("[color=cyan]" , "\u001b[96m" ); |
129 | p_string_ansi = p_string_ansi.replace("[color=white]" , "\u001b[97m" ); |
130 | p_string_ansi = p_string_ansi.replace("[color=orange]" , "\u001b[38;5;208m" ); |
131 | p_string_ansi = p_string_ansi.replace("[color=gray]" , "\u001b[90m" ); |
132 | p_string_ansi = p_string_ansi.replace("[/color]" , "\u001b[39m" ); |
133 | } |
134 | if (p_string_ansi.contains("[bgcolor" )) { |
135 | p_string_ansi = p_string_ansi.replace("[bgcolor=black]" , "\u001b[40m" ); |
136 | p_string_ansi = p_string_ansi.replace("[bgcolor=red]" , "\u001b[101m" ); |
137 | p_string_ansi = p_string_ansi.replace("[bgcolor=green]" , "\u001b[102m" ); |
138 | p_string_ansi = p_string_ansi.replace("[bgcolor=lime]" , "\u001b[102m" ); |
139 | p_string_ansi = p_string_ansi.replace("[bgcolor=yellow]" , "\u001b[103m" ); |
140 | p_string_ansi = p_string_ansi.replace("[bgcolor=blue]" , "\u001b[104m" ); |
141 | p_string_ansi = p_string_ansi.replace("[bgcolor=magenta]" , "\u001b[105m" ); |
142 | p_string_ansi = p_string_ansi.replace("[bgcolor=pink]" , "\u001b[48;5;218m" ); |
143 | p_string_ansi = p_string_ansi.replace("[bgcolor=purple]" , "\u001b[48;5;98m" ); |
144 | p_string_ansi = p_string_ansi.replace("[bgcolor=cyan]" , "\u001b[106m" ); |
145 | p_string_ansi = p_string_ansi.replace("[bgcolor=white]" , "\u001b[107m" ); |
146 | p_string_ansi = p_string_ansi.replace("[bgcolor=orange]" , "\u001b[48;5;208m" ); |
147 | p_string_ansi = p_string_ansi.replace("[bgcolor=gray]" , "\u001b[100m" ); |
148 | p_string_ansi = p_string_ansi.replace("[/bgcolor]" , "\u001b[49m" ); |
149 | } |
150 | if (p_string_ansi.contains("[fgcolor" )) { |
151 | p_string_ansi = p_string_ansi.replace("[fgcolor=black]" , "\u001b[30;40m" ); |
152 | p_string_ansi = p_string_ansi.replace("[fgcolor=red]" , "\u001b[91;101m" ); |
153 | p_string_ansi = p_string_ansi.replace("[fgcolor=green]" , "\u001b[92;102m" ); |
154 | p_string_ansi = p_string_ansi.replace("[fgcolor=lime]" , "\u001b[92;102m" ); |
155 | p_string_ansi = p_string_ansi.replace("[fgcolor=yellow]" , "\u001b[93;103m" ); |
156 | p_string_ansi = p_string_ansi.replace("[fgcolor=blue]" , "\u001b[94;104m" ); |
157 | p_string_ansi = p_string_ansi.replace("[fgcolor=magenta]" , "\u001b[95;105m" ); |
158 | p_string_ansi = p_string_ansi.replace("[fgcolor=pink]" , "\u001b[38;5;218;48;5;218m" ); |
159 | p_string_ansi = p_string_ansi.replace("[fgcolor=purple]" , "\u001b[38;5;98;48;5;98m" ); |
160 | p_string_ansi = p_string_ansi.replace("[fgcolor=cyan]" , "\u001b[96;106m" ); |
161 | p_string_ansi = p_string_ansi.replace("[fgcolor=white]" , "\u001b[97;107m" ); |
162 | p_string_ansi = p_string_ansi.replace("[fgcolor=orange]" , "\u001b[38;5;208;48;5;208m" ); |
163 | p_string_ansi = p_string_ansi.replace("[fgcolor=gray]" , "\u001b[90;100m" ); |
164 | p_string_ansi = p_string_ansi.replace("[/fgcolor]" , "\u001b[39;49m" ); |
165 | } |
166 | |
167 | p_string_ansi += "\u001b[0m" ; // Reset. |
168 | |
169 | OS::get_singleton()->print_rich("%s\n" , p_string_ansi.utf8().get_data()); |
170 | |
171 | _global_lock(); |
172 | PrintHandlerList *l = print_handler_list; |
173 | while (l) { |
174 | l->printfunc(l->userdata, p_string, false, true); |
175 | l = l->next; |
176 | } |
177 | |
178 | _global_unlock(); |
179 | } |
180 | |
181 | void print_error(String p_string) { |
182 | if (!CoreGlobals::print_error_enabled) { |
183 | return; |
184 | } |
185 | |
186 | OS::get_singleton()->printerr("%s\n" , p_string.utf8().get_data()); |
187 | |
188 | _global_lock(); |
189 | PrintHandlerList *l = print_handler_list; |
190 | while (l) { |
191 | l->printfunc(l->userdata, p_string, true, false); |
192 | l = l->next; |
193 | } |
194 | |
195 | _global_unlock(); |
196 | } |
197 | |
198 | bool is_print_verbose_enabled() { |
199 | return OS::get_singleton()->is_stdout_verbose(); |
200 | } |
201 | |
202 | String stringify_variants(Variant p_var) { |
203 | return p_var.operator String(); |
204 | } |
205 | |