From 0566caabf6530e0d704984a136a1473ab9874027 Mon Sep 17 00:00:00 2001 From: Wil <63251107+wilyt1@users.noreply.github.com> Date: Fri, 15 Dec 2023 20:52:04 -0500 Subject: [PATCH] Add More Conditions to runif --- Cmdr/BuiltInCommands/Utility/runif.lua | 64 ++++++++++++++++++++++++- Cmdr/BuiltInTypes/ConditionFunction.lua | 17 ++++++- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/Cmdr/BuiltInCommands/Utility/runif.lua b/Cmdr/BuiltInCommands/Utility/runif.lua index 3602622c..33b83c1c 100644 --- a/Cmdr/BuiltInCommands/Utility/runif.lua +++ b/Cmdr/BuiltInCommands/Utility/runif.lua @@ -4,6 +4,66 @@ local conditions = { return text:sub(#arg + 1) end end, + + contains = function(text, arg) + if text:find(arg, 1, true) then + return text + end + end, + + endsWith = function(text, arg) + if text:sub(-#arg) == arg then + return text:sub(1, -#arg - 1) + end + end, + + pattern = function(text, arg) + if text:match(arg) then + return text + end + end, + + equals = function(text, arg) + if text == arg then + return text + end + end, + + notEquals = function(text, arg) + if text ~= arg then + return text + end + end, + + greaterThan = function(text, arg) + if tonumber(text) > tonumber(arg) then + return text + end + end, + + lessThan = function(text, arg) + if tonumber(text) < tonumber(arg) then + return text + end + end, + + greaterThanOrEqual = function(text, arg) + if tonumber(text) >= tonumber(arg) then + return text + end + end, + + lessThanOrEqual = function(text, arg) + if tonumber(text) <= tonumber(arg) then + return text + end + end, + + length = function(text, arg) + if #text == tonumber(arg) then + return text + end + end, } return { @@ -42,9 +102,9 @@ return { return ("Condition %q is not valid."):format(condition) end - local text = conditionFunc(testAgainst, arg) + local success, text = pcall(conditionFunc, testAgainst, arg) - if text then + if success and text then return context.Dispatcher:EvaluateAndRun( context.Cmdr.Util.RunEmbeddedCommands(context.Dispatcher, command or text) ) diff --git a/Cmdr/BuiltInTypes/ConditionFunction.lua b/Cmdr/BuiltInTypes/ConditionFunction.lua index f6feef6a..3729f4dc 100644 --- a/Cmdr/BuiltInTypes/ConditionFunction.lua +++ b/Cmdr/BuiltInTypes/ConditionFunction.lua @@ -1,3 +1,18 @@ return function(registry) - registry:RegisterType("conditionFunction", registry.Cmdr.Util.MakeEnumType("ConditionFunction", { "startsWith" })) + registry:RegisterType( + "conditionFunction", + registry.Cmdr.Util.MakeEnumType("ConditionFunction", { + "startsWith", + "contains", + "endsWith", + "pattern", + "equals", + "notEquals", + "greaterThan", + "lessThan", + "greaterThanOrEqual", + "lessThanOrEqual", + "length", + }) + ) end