123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- /**************************************************************************/
- /* 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 <stdio.h>
- static PrintHandlerList *print_handler_list = nullptr;
- 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;
- }
- 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();
- }
- 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, p_string.length() - 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 += vformat("[%s]", tag);
- }
- }
- output += "\u001b[0m"; // Reset.
- 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();
- }
- void print_error(const String &p_string) {
- if (!CoreGlobals::print_error_enabled) {
- return;
- }
- 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();
- }
- bool is_print_verbose_enabled() {
- return OS::get_singleton()->is_stdout_verbose();
- }
- String stringify_variants(const Variant &p_var) {
- return p_var.operator String();
- }
|