Skip to content

Commit

Permalink
Add More Conditions to runif
Browse files Browse the repository at this point in the history
  • Loading branch information
wilyt1 committed Dec 16, 2023
1 parent 2f3c1e1 commit 0566caa
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
64 changes: 62 additions & 2 deletions Cmdr/BuiltInCommands/Utility/runif.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
)
Expand Down
17 changes: 16 additions & 1 deletion Cmdr/BuiltInTypes/ConditionFunction.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0566caa

Please sign in to comment.