mirror of
https://codeberg.org/StreamGraph/StreamGraph.git
synced 2024-11-13 19:49:55 +01:00
add string formatting nodes (#170)
Reviewed-on: https://codeberg.org/StreamGraph/StreamGraph/pulls/170 Co-authored-by: Lera Elvoé <yagich@poto.cafe> Co-committed-by: Lera Elvoé <yagich@poto.cafe>
This commit is contained in:
parent
9590b6ae4f
commit
84bc201fdd
2 changed files with 96 additions and 0 deletions
48
classes/deck/nodes/general/format_string.gd
Normal file
48
classes/deck/nodes/general/format_string.gd
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
# (c) 2023-present Eroax
|
||||||
|
# (c) 2023-present Yagich
|
||||||
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
extends DeckNode
|
||||||
|
|
||||||
|
|
||||||
|
func _init() -> void:
|
||||||
|
name = "String Positional Format"
|
||||||
|
node_type = "format_string"
|
||||||
|
description = "Replaces '%s' characters in a string."
|
||||||
|
|
||||||
|
add_input_port(
|
||||||
|
DeckType.Types.STRING,
|
||||||
|
"String to format",
|
||||||
|
"field",
|
||||||
|
)
|
||||||
|
|
||||||
|
add_input_port(
|
||||||
|
DeckType.Types.ARRAY,
|
||||||
|
"Replacements",
|
||||||
|
)
|
||||||
|
|
||||||
|
add_output_port(
|
||||||
|
DeckType.Types.STRING,
|
||||||
|
"Result string",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func _replace(string: String, replacements: Array) -> String:
|
||||||
|
return string % replacements
|
||||||
|
|
||||||
|
|
||||||
|
func _value_request(on_output_port: int) -> Variant:
|
||||||
|
var string: String = DeckType.convert_value(await resolve_input_port_value_async(0), DeckType.Types.STRING)
|
||||||
|
var replacements: Array = DeckType.convert_value(await resolve_input_port_value_async(1), DeckType.Types.ARRAY)
|
||||||
|
|
||||||
|
return _replace(string, replacements)
|
||||||
|
|
||||||
|
|
||||||
|
func _receive(on_input_port: int, data: Variant) -> void:
|
||||||
|
if on_input_port == 0:
|
||||||
|
var string: String = DeckType.convert_value(data, DeckType.Types.STRING)
|
||||||
|
var replacements: Array = DeckType.convert_value(await resolve_input_port_value_async(1), DeckType.Types.ARRAY)
|
||||||
|
send(0, _replace(string, replacements))
|
||||||
|
else:
|
||||||
|
var string: String = DeckType.convert_value(await resolve_input_port_value_async(0), DeckType.Types.STRING)
|
||||||
|
var replacements: Array = DeckType.convert_value(data, DeckType.Types.ARRAY)
|
||||||
|
send(0, _replace(string, replacements))
|
48
classes/deck/nodes/general/template_string.gd
Normal file
48
classes/deck/nodes/general/template_string.gd
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
# (c) 2023-present Eroax
|
||||||
|
# (c) 2023-present Yagich
|
||||||
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
extends DeckNode
|
||||||
|
|
||||||
|
|
||||||
|
func _init() -> void:
|
||||||
|
name = "String Template Format"
|
||||||
|
node_type = "template_string"
|
||||||
|
description = "Replaces variables in a string."
|
||||||
|
|
||||||
|
add_input_port(
|
||||||
|
DeckType.Types.STRING,
|
||||||
|
"String to format",
|
||||||
|
"field",
|
||||||
|
)
|
||||||
|
|
||||||
|
add_input_port(
|
||||||
|
DeckType.Types.DICTIONARY,
|
||||||
|
"Replacements",
|
||||||
|
)
|
||||||
|
|
||||||
|
add_output_port(
|
||||||
|
DeckType.Types.STRING,
|
||||||
|
"Result string",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func _replace(string: String, replacements: Dictionary) -> String:
|
||||||
|
return string.format(replacements)
|
||||||
|
|
||||||
|
|
||||||
|
func _value_request(on_output_port: int) -> Variant:
|
||||||
|
var string: String = DeckType.convert_value(await resolve_input_port_value_async(0), DeckType.Types.STRING)
|
||||||
|
var replacements: Dictionary = DeckType.convert_value(await resolve_input_port_value_async(1), DeckType.Types.DICTIONARY)
|
||||||
|
|
||||||
|
return _replace(string, replacements)
|
||||||
|
|
||||||
|
|
||||||
|
func _receive(on_input_port: int, data: Variant) -> void:
|
||||||
|
if on_input_port == 0:
|
||||||
|
var string: String = DeckType.convert_value(data, DeckType.Types.STRING)
|
||||||
|
var replacements: Dictionary = DeckType.convert_value(await resolve_input_port_value_async(1), DeckType.Types.DICTIONARY)
|
||||||
|
send(0, _replace(string, replacements))
|
||||||
|
else:
|
||||||
|
var string: String = DeckType.convert_value(await resolve_input_port_value_async(0), DeckType.Types.STRING)
|
||||||
|
var replacements: Dictionary = DeckType.convert_value(data, DeckType.Types.DICTIONARY)
|
||||||
|
send(0, _replace(string, replacements))
|
Loading…
Reference in a new issue