diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-06-28 23:35:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-28 23:35:53 +0200 |
commit | b730d2ee09a8fdc4eaa44efe9afc083d907f80c3 (patch) | |
tree | 0ed1a5563e639656ccc63749a2f53add1733965d /modules | |
parent | 0cd049e4112ab8d4ee2ebd6869d114bdf754c3bb (diff) | |
parent | c6291bcd8a49055ce2158f88759a487de5b8d1bd (diff) |
Merge pull request #60675 from voylin/Add-BBCode-support-for-printing-output
Adding print_rich() for printing with BBCode
Diffstat (limited to 'modules')
-rw-r--r-- | modules/gdscript/tests/gdscript_test_runner.cpp | 2 | ||||
-rw-r--r-- | modules/gdscript/tests/gdscript_test_runner.h | 2 | ||||
-rw-r--r-- | modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs | 24 | ||||
-rw-r--r-- | modules/mono/glue/gd_glue.cpp | 22 |
4 files changed, 48 insertions, 2 deletions
diff --git a/modules/gdscript/tests/gdscript_test_runner.cpp b/modules/gdscript/tests/gdscript_test_runner.cpp index de5cd10e7c..ff4832bde0 100644 --- a/modules/gdscript/tests/gdscript_test_runner.cpp +++ b/modules/gdscript/tests/gdscript_test_runner.cpp @@ -363,7 +363,7 @@ void GDScriptTest::disable_stdout() { OS::get_singleton()->set_stderr_enabled(false); } -void GDScriptTest::print_handler(void *p_this, const String &p_message, bool p_error) { +void GDScriptTest::print_handler(void *p_this, const String &p_message, bool p_error, bool p_rich) { TestResult *result = (TestResult *)p_this; result->output += p_message + "\n"; } diff --git a/modules/gdscript/tests/gdscript_test_runner.h b/modules/gdscript/tests/gdscript_test_runner.h index d6c6419e21..ee21afd9c9 100644 --- a/modules/gdscript/tests/gdscript_test_runner.h +++ b/modules/gdscript/tests/gdscript_test_runner.h @@ -86,7 +86,7 @@ private: TestResult execute_test_code(bool p_is_generating); public: - static void print_handler(void *p_this, const String &p_message, bool p_error); + static void print_handler(void *p_this, const String &p_message, bool p_error, bool p_rich); static void error_handler(void *p_this, const char *p_function, const char *p_file, int p_line, const char *p_error, const char *p_explanation, bool p_editor_notify, ErrorHandlerType p_type); TestResult run_test(); bool generate_output(); diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs index 74aa38386f..bb076a9633 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs @@ -239,6 +239,27 @@ namespace Godot } /// <summary> + /// Converts one or more arguments of any type to string in the best way possible and prints them to the console. The following BBCode tags are supported: b, i, u, s, indent, code, url, center, right, color, bgcolor, fgcolor. Color tags only support named colors such as [code]red[/code], [i]not[/i] hexadecimal color codes. Unsupported tags will be left as-is in standard output. + /// When printing to standard output, the supported subset of BBCode is converted to ANSI escape codes for the terminal emulator to display. Displaying ANSI escape codes is currently only supported on Linux and macOS. Support for ANSI escape codes may vary across terminal emulators, especially for italic and strikethrough. + /// + /// Note: Consider using <see cref="PushError(string)"/> and <see cref="PushWarning(string)"/> + /// to print error and warning messages instead of <see cref="Print(object[])"/> or <see cref="PrintRich(object[])"/>. + /// This distinguishes them from print messages used for debugging purposes, + /// while also displaying a stack trace when an error or warning is printed. + /// </summary> + /// <example> + /// <code> + /// GD.PrintRich("[b]Hello world![/b]"); // Prints out "Hello world!" in bold. + /// </code> + /// </example> + /// <param name="what">Arguments that will be printed.</param> + /// </summary> + public static void PrintRich(params object[] what) + { + godot_icall_GD_print_rich(GetPrintParams(what)); + } + + /// <summary> /// Prints the current stack trace information to the console. /// </summary> public static void PrintStack() @@ -562,6 +583,9 @@ namespace Godot internal static extern void godot_icall_GD_print(object[] what); [MethodImpl(MethodImplOptions.InternalCall)] + internal static extern void godot_icall_GD_print_rich(object[] what); + + [MethodImpl(MethodImplOptions.InternalCall)] internal static extern void godot_icall_GD_printerr(object[] what); [MethodImpl(MethodImplOptions.InternalCall)] diff --git a/modules/mono/glue/gd_glue.cpp b/modules/mono/glue/gd_glue.cpp index 8aead217cf..8b1c2b729e 100644 --- a/modules/mono/glue/gd_glue.cpp +++ b/modules/mono/glue/gd_glue.cpp @@ -90,6 +90,27 @@ void godot_icall_GD_print(MonoArray *p_what) { print_line(str); } +void godot_icall_GD_print_rich(MonoArray *p_what) { + String str; + int length = mono_array_length(p_what); + + for (int i = 0; i < length; i++) { + MonoObject *elem = mono_array_get(p_what, MonoObject *, i); + + MonoException *exc = nullptr; + String elem_str = GDMonoMarshal::mono_object_to_variant_string(elem, &exc); + + if (exc) { + GDMonoUtils::set_pending_exception(exc); + return; + } + + str += elem_str; + } + + print_line_rich(str); +} + void godot_icall_GD_printerr(MonoArray *p_what) { String str; int length = mono_array_length(p_what); @@ -300,6 +321,7 @@ void godot_register_gd_icalls() { GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_pusherror", godot_icall_GD_pusherror); GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_pushwarning", godot_icall_GD_pushwarning); GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_print", godot_icall_GD_print); + GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_print_rich", godot_icall_GD_print_rich); GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_printerr", godot_icall_GD_printerr); GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_printraw", godot_icall_GD_printraw); GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_prints", godot_icall_GD_prints); |