print_string.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #include "print_string.h"
  31. #include "core/core_globals.h"
  32. #include "core/os/os.h"
  33. #include <stdio.h>
  34. static PrintHandlerList *print_handler_list = nullptr;
  35. void add_print_handler(PrintHandlerList *p_handler) {
  36. _global_lock();
  37. p_handler->next = print_handler_list;
  38. print_handler_list = p_handler;
  39. _global_unlock();
  40. }
  41. void remove_print_handler(const PrintHandlerList *p_handler) {
  42. _global_lock();
  43. PrintHandlerList *prev = nullptr;
  44. PrintHandlerList *l = print_handler_list;
  45. while (l) {
  46. if (l == p_handler) {
  47. if (prev) {
  48. prev->next = l->next;
  49. } else {
  50. print_handler_list = l->next;
  51. }
  52. break;
  53. }
  54. prev = l;
  55. l = l->next;
  56. }
  57. //OS::get_singleton()->print("print handler list is %p\n",print_handler_list);
  58. _global_unlock();
  59. ERR_FAIL_NULL(l);
  60. }
  61. void __print_line(const String &p_string) {
  62. if (!CoreGlobals::print_line_enabled) {
  63. return;
  64. }
  65. OS::get_singleton()->print("%s\n", p_string.utf8().get_data());
  66. _global_lock();
  67. PrintHandlerList *l = print_handler_list;
  68. while (l) {
  69. l->printfunc(l->userdata, p_string, false, false);
  70. l = l->next;
  71. }
  72. _global_unlock();
  73. }
  74. void __print_line_rich(const String &p_string) {
  75. if (!CoreGlobals::print_line_enabled) {
  76. return;
  77. }
  78. // Convert a subset of BBCode tags to ANSI escape codes for correct display in the terminal.
  79. // Support of those ANSI escape codes varies across terminal emulators,
  80. // especially for italic and strikethrough.
  81. String output;
  82. int pos = 0;
  83. while (pos <= p_string.length()) {
  84. int brk_pos = p_string.find_char('[', pos);
  85. if (brk_pos < 0) {
  86. brk_pos = p_string.length();
  87. }
  88. String txt = brk_pos > pos ? p_string.substr(pos, brk_pos - pos) : "";
  89. if (brk_pos == p_string.length()) {
  90. output += txt;
  91. break;
  92. }
  93. int brk_end = p_string.find_char(']', brk_pos + 1);
  94. if (brk_end == -1) {
  95. txt += p_string.substr(brk_pos, p_string.length() - brk_pos);
  96. output += txt;
  97. break;
  98. }
  99. pos = brk_end + 1;
  100. output += txt;
  101. String tag = p_string.substr(brk_pos + 1, brk_end - brk_pos - 1);
  102. if (tag == "b") {
  103. output += "\u001b[1m";
  104. } else if (tag == "/b") {
  105. output += "\u001b[22m";
  106. } else if (tag == "i") {
  107. output += "\u001b[3m";
  108. } else if (tag == "/i") {
  109. output += "\u001b[23m";
  110. } else if (tag == "u") {
  111. output += "\u001b[4m";
  112. } else if (tag == "/u") {
  113. output += "\u001b[24m";
  114. } else if (tag == "s") {
  115. output += "\u001b[9m";
  116. } else if (tag == "/s") {
  117. output += "\u001b[29m";
  118. } else if (tag == "indent") {
  119. output += " ";
  120. } else if (tag == "/indent") {
  121. output += "";
  122. } else if (tag == "code") {
  123. output += "\u001b[2m";
  124. } else if (tag == "/code") {
  125. output += "\u001b[22m";
  126. } else if (tag == "url") {
  127. output += "";
  128. } else if (tag == "/url") {
  129. output += "";
  130. } else if (tag == "center") {
  131. output += "\n\t\t\t";
  132. } else if (tag == "center") {
  133. output += "";
  134. } else if (tag == "right") {
  135. output += "\n\t\t\t\t\t\t";
  136. } else if (tag == "/right") {
  137. output += "";
  138. } else if (tag.begins_with("color=")) {
  139. String color_name = tag.trim_prefix("color=");
  140. if (color_name == "black") {
  141. output += "\u001b[30m";
  142. } else if (color_name == "red") {
  143. output += "\u001b[91m";
  144. } else if (color_name == "green") {
  145. output += "\u001b[92m";
  146. } else if (color_name == "lime") {
  147. output += "\u001b[92m";
  148. } else if (color_name == "yellow") {
  149. output += "\u001b[93m";
  150. } else if (color_name == "blue") {
  151. output += "\u001b[94m";
  152. } else if (color_name == "magenta") {
  153. output += "\u001b[95m";
  154. } else if (color_name == "pink") {
  155. output += "\u001b[38;5;218m";
  156. } else if (color_name == "purple") {
  157. output += "\u001b[38;5;98m";
  158. } else if (color_name == "cyan") {
  159. output += "\u001b[96m";
  160. } else if (color_name == "white") {
  161. output += "\u001b[97m";
  162. } else if (color_name == "orange") {
  163. output += "\u001b[38;5;208m";
  164. } else if (color_name == "gray") {
  165. output += "\u001b[90m";
  166. } else {
  167. Color c = Color::from_string(color_name, Color());
  168. output += vformat("\u001b[38;2;%d;%d;%dm", c.r * 255, c.g * 255, c.b * 255);
  169. }
  170. } else if (tag == "/color") {
  171. output += "\u001b[39m";
  172. } else if (tag.begins_with("bgcolor=")) {
  173. String color_name = tag.trim_prefix("bgcolor=");
  174. if (color_name == "black") {
  175. output += "\u001b[40m";
  176. } else if (color_name == "red") {
  177. output += "\u001b[101m";
  178. } else if (color_name == "green") {
  179. output += "\u001b[102m";
  180. } else if (color_name == "lime") {
  181. output += "\u001b[102m";
  182. } else if (color_name == "yellow") {
  183. output += "\u001b[103m";
  184. } else if (color_name == "blue") {
  185. output += "\u001b[104m";
  186. } else if (color_name == "magenta") {
  187. output += "\u001b[105m";
  188. } else if (color_name == "pink") {
  189. output += "\u001b[48;5;218m";
  190. } else if (color_name == "purple") {
  191. output += "\u001b[48;5;98m";
  192. } else if (color_name == "cyan") {
  193. output += "\u001b[106m";
  194. } else if (color_name == "white") {
  195. output += "\u001b[107m";
  196. } else if (color_name == "orange") {
  197. output += "\u001b[48;5;208m";
  198. } else if (color_name == "gray") {
  199. output += "\u001b[100m";
  200. } else {
  201. Color c = Color::from_string(color_name, Color());
  202. output += vformat("\u001b[48;2;%d;%d;%dm", c.r * 255, c.g * 255, c.b * 255);
  203. }
  204. } else if (tag == "/bgcolor") {
  205. output += "\u001b[49m";
  206. } else if (tag.begins_with("fgcolor=")) {
  207. String color_name = tag.trim_prefix("fgcolor=");
  208. if (color_name == "black") {
  209. output += "\u001b[30;40m";
  210. } else if (color_name == "red") {
  211. output += "\u001b[91;101m";
  212. } else if (color_name == "green") {
  213. output += "\u001b[92;102m";
  214. } else if (color_name == "lime") {
  215. output += "\u001b[92;102m";
  216. } else if (color_name == "yellow") {
  217. output += "\u001b[93;103m";
  218. } else if (color_name == "blue") {
  219. output += "\u001b[94;104m";
  220. } else if (color_name == "magenta") {
  221. output += "\u001b[95;105m";
  222. } else if (color_name == "pink") {
  223. output += "\u001b[38;5;218;48;5;218m";
  224. } else if (color_name == "purple") {
  225. output += "\u001b[38;5;98;48;5;98m";
  226. } else if (color_name == "cyan") {
  227. output += "\u001b[96;106m";
  228. } else if (color_name == "white") {
  229. output += "\u001b[97;107m";
  230. } else if (color_name == "orange") {
  231. output += "\u001b[38;5;208;48;5;208m";
  232. } else if (color_name == "gray") {
  233. output += "\u001b[90;100m";
  234. } else {
  235. Color c = Color::from_string(color_name, Color());
  236. 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);
  237. }
  238. } else if (tag == "/fgcolor") {
  239. output += "\u001b[39;49m";
  240. } else {
  241. output += vformat("[%s]", tag);
  242. }
  243. }
  244. output += "\u001b[0m"; // Reset.
  245. OS::get_singleton()->print_rich("%s\n", output.utf8().get_data());
  246. _global_lock();
  247. PrintHandlerList *l = print_handler_list;
  248. while (l) {
  249. l->printfunc(l->userdata, p_string, false, true);
  250. l = l->next;
  251. }
  252. _global_unlock();
  253. }
  254. void print_error(const String &p_string) {
  255. if (!CoreGlobals::print_error_enabled) {
  256. return;
  257. }
  258. OS::get_singleton()->printerr("%s\n", p_string.utf8().get_data());
  259. _global_lock();
  260. PrintHandlerList *l = print_handler_list;
  261. while (l) {
  262. l->printfunc(l->userdata, p_string, true, false);
  263. l = l->next;
  264. }
  265. _global_unlock();
  266. }
  267. bool is_print_verbose_enabled() {
  268. return OS::get_singleton()->is_stdout_verbose();
  269. }
  270. String stringify_variants(const Variant &p_var) {
  271. return p_var.operator String();
  272. }