Skip to content

Commit 497ab50

Browse files
committed
Added 2 Text Actions
+ REPLACE_TEXT + REMOVE_TEXT
1 parent 9c8166e commit 497ab50

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/main/java/io/github/techstreet/dfscript/script/action/ScriptActionType.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,50 @@ public enum ScriptActionType {
16491649
list.sort(new ScriptValueComparator());
16501650

16511651
ctx.context().setVariable(ctx.variable("Result").name(), new ScriptListValue(list));
1652+
})),
1653+
1654+
REPLACE_TEXT(builder -> builder.name("Replace Text")
1655+
.description("Searches for part of a text and replaces it.")
1656+
.icon(Items.LEAD)
1657+
.arg("Result", ScriptActionArgumentType.VARIABLE)
1658+
.arg("Text to change", ScriptActionArgumentType.TEXT)
1659+
.arg("Text part to replace", ScriptActionArgumentType.TEXT)
1660+
.arg("Replacement", ScriptActionArgumentType.TEXT)
1661+
.category(ScriptActionCategory.TEXTS)
1662+
.action(ctx -> {
1663+
String result = ctx.value("Text to change").asText();
1664+
1665+
result = result.replace(ctx.value("Text part to replace").asText(), ctx.value("Replacement").asText());
1666+
1667+
ctx.context().setVariable(ctx.variable("Result").name(), new ScriptTextValue(result));
1668+
})),
1669+
1670+
REMOVE_TEXT(builder -> builder.name("Remove Text")
1671+
.description("Searches for part of a text and replaces it.")
1672+
.icon(Items.WRITABLE_BOOK)
1673+
.arg("Result", ScriptActionArgumentType.VARIABLE)
1674+
.arg("Text to change", ScriptActionArgumentType.TEXT)
1675+
.arg("Text to remove", ScriptActionArgumentType.TEXT, b -> b.plural(true))
1676+
.category(ScriptActionCategory.TEXTS)
1677+
.action(ctx -> {
1678+
String result = null;
1679+
1680+
if(ctx.argMap().containsKey("Text to change"))
1681+
{
1682+
result = ctx.value("Text to change").asText();
1683+
}
1684+
else
1685+
{
1686+
result = ctx.value("Result").asText();
1687+
}
1688+
1689+
List<ScriptValue> textsToRemove = ctx.pluralValue("Text to remove");
1690+
1691+
for(int i = 0; i < textsToRemove.size(); i++) {
1692+
result = result.replace(textsToRemove.get(i).asText(), "");
1693+
}
1694+
1695+
ctx.context().setVariable(ctx.variable("Result").name(), new ScriptTextValue(result));
16521696
}));
16531697

16541698
private Consumer<ScriptActionContext> action = (ctx) -> {

0 commit comments

Comments
 (0)