Add format_stringarray function
This commit is contained in:
parent
0c9f600328
commit
3760f674cb
2 changed files with 34 additions and 0 deletions
|
@ -27,3 +27,20 @@ If `flatten` is set to `true`, the decimal part will be discarded.
|
||||||
Converts a number of gibibytes to mebibytes.
|
Converts a number of gibibytes to mebibytes.
|
||||||
|
|
||||||
If `flatten` is set to `true`, the decimal part will be discarded.
|
If `flatten` is set to `true`, the decimal part will be discarded.
|
||||||
|
### *String* <u>format_stringarray</u>(*Array[String]* <u>array</u>, *String* <u>item_before</u> = *""*, *String* <u>item_after</u> = *""*, *String* <u>separator_list</u> = *", "*, *String* <u>separator_final</u> = *" & "*)
|
||||||
|
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]"))
|
||||||
|
```
|
||||||
|
|
17
src/Misc.gd
17
src/Misc.gd
|
@ -43,3 +43,20 @@ func mib2gib(mib: float, flatten: bool = true) -> float:
|
||||||
func gib2mib(gib: float, flatten: bool = true) -> float:
|
func gib2mib(gib: float, flatten: bool = true) -> float:
|
||||||
if flatten: return int(gib*1024)
|
if flatten: return int(gib*1024)
|
||||||
return 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
|
||||||
|
|
Loading…
Reference in a new issue