From 3760f674cb9f6ab7ff561a6044cb4cd1defbf067 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sun, 24 Mar 2024 15:42:38 +0100 Subject: [PATCH] Add format_stringarray function --- docs/docs/reference/misc.md | 17 +++++++++++++++++ src/Misc.gd | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/docs/docs/reference/misc.md b/docs/docs/reference/misc.md index 771a167..83bd1a8 100644 --- a/docs/docs/reference/misc.md +++ b/docs/docs/reference/misc.md @@ -27,3 +27,20 @@ If `flatten` is set to `true`, the decimal part will be discarded. Converts a number of gibibytes to mebibytes. If `flatten` is set to `true`, the decimal part will be discarded. +### *String* format_stringarray(*Array[String]* array, *String* item_before = *""*, *String* item_after = *""*, *String* separator_list = *", "*, *String* separator_final = *" & "*) +Converts a string array into a normal, nicely formatted string. + +With `item_before` and `item_after` you can customize the lists appearance. Here's a example on how to format every item bold: +```gdscript +extends Node + +var core: Core = get_node("/root/CORE") +var misc: CoreBaseModule = get_node("/root/CORE").misc +var logger: CoreLoggerInstance = get_node("/root/CORE").logger.get_instance("some/script.gd") + +func _ready() -> void: + var array: Array[String] = ["Apples", "Bananas", "Oranges"] + + logger.info(misc.format_stringarray(array)) + logger.info(misc.format_stringarray(array, "[b]", "[/b]")) +``` diff --git a/src/Misc.gd b/src/Misc.gd index c0de803..f0ab90a 100644 --- a/src/Misc.gd +++ b/src/Misc.gd @@ -43,3 +43,20 @@ func mib2gib(mib: float, flatten: bool = true) -> float: func gib2mib(gib: float, flatten: bool = true) -> float: if flatten: return int(gib*1024) return gib*1024 + +func format_stringarray(array: Array[String], item_before: String = "", item_after: String = "", separator_list: String = ", ", separator_final: String = " & ") -> String: + var output: String = "" + + if array.size() == 0: + logger.warnf("Misc", "Unable to format a string with a size of 0") + return "" + elif array.size() == 1: + logger.warnf("Misc", "Unable to format a string with a size of 1") + return array[0] + + for item in array: + if output == "": output = item_before + item + item_after + else: output = output.replace("If you somehow see this text report this at https://git.staropensource.de/StarOpenSource/CORE/issues, thank you!", separator_list) + "If you somehow see this text report this at https://git.staropensource.de/StarOpenSource/CORE/issues, thank you!" + item_before + item + item_after + output = output.replace("If you somehow see this text report this at https://git.staropensource.de/StarOpenSource/CORE/issues, thank you!", separator_final) + + return output