print_string.cpp 9.5 KB

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