Allow command predicates to manage parameters passed to the commands

When a command is performed with parameters, those are now passed to the
predicate.
The predicate can then return, after the validity boolean, any other
value that will then be passed to the actual command.
If the predicate only returns the validity boolean, the original
parameters are passed through to the actual command.

This allows predicates to manipulate the received parameters, and allows
them to pass the result of an expensive computation to the actual
command, which won't have to recalculate it.

String and table predicates will now also return `core.active_view`.
This commit is contained in:
Guldoman
2022-08-16 22:13:16 +02:00
parent 6ccc5f6dde
commit cf29a6a45f
13 changed files with 223 additions and 233 deletions
+12 -4
View File
@@ -33,9 +33,9 @@ function command.generate_predicate(predicate)
if type(predicate) == "table" then
local class = predicate
if not strict then
predicate = function() return core.active_view:extends(class) end
predicate = function(...) return core.active_view:extends(class), core.active_view, ... end
else
predicate = function() return core.active_view:is(class) end
predicate = function(...) return core.active_view:is(class), core.active_view, ... end
end
end
return predicate
@@ -76,8 +76,16 @@ end
local function perform(name, ...)
local cmd = command.map[name]
if cmd and cmd.predicate(...) then
cmd.perform(...)
if not cmd then return false end
local res = { cmd.predicate(...) }
if table.remove(res, 1) then
if #res > 0 then
-- send values returned from predicate
cmd.perform(table.unpack(res))
else
-- send original parameters
cmd.perform(...)
end
return true
end
return false