diff options
| author | jgmdev <jgmdev@gmail.com> | 2022-05-12 22:15:29 -0400 |
|---|---|---|
| committer | jgmdev <jgmdev@gmail.com> | 2022-05-12 22:15:29 -0400 |
| commit | 59d91087e9b1ff3990f2d5543432531ee11ae5b0 (patch) | |
| tree | 3f914e2ad518954a96693acf86302aa0c85e8ea7 /data/core/command.lua | |
| parent | fd0a433f59ddcd6c6d1f8de83db2da1079b93868 (diff) | |
| download | pragtical-59d91087e9b1ff3990f2d5543432531ee11ae5b0.tar.gz pragtical-59d91087e9b1ff3990f2d5543432531ee11ae5b0.zip | |
adjust and consolidate duplicated predicate code
Diffstat (limited to 'data/core/command.lua')
| -rw-r--r-- | data/core/command.lua | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/data/core/command.lua b/data/core/command.lua index bdc1ed34..39582de3 100644 --- a/data/core/command.lua +++ b/data/core/command.lua @@ -6,15 +6,33 @@ command.map = {} local always_true = function() return true end -function command.add(predicate, map) +---Used iternally by command.add, statusview, and contextmenu to generate a +---function with a condition to evaluate returning the boolean result of this +---evaluation. +--- +---If a string predicate is given it is treated as a require import that should +---return a valid object which is checked against the current active view, the +---sames applies if a table is given. A function that returns a boolean can be +---used instead to perform a custom evaluation, setting to nil means always +---evaluates to true. +--- +---@param predicate string | table | function +---@return function +function command.generate_predicate(predicate) predicate = predicate or always_true if type(predicate) == "string" then predicate = require(predicate) end if type(predicate) == "table" then local class = predicate - predicate = function() return core.active_view:is(class) end + predicate = function() return core.active_view:extends(class) end end + return predicate +end + + +function command.add(predicate, map) + predicate = command.generate_predicate(predicate) for name, fn in pairs(map) do assert(not command.map[name], "command already exists: " .. name) command.map[name] = { predicate = predicate, perform = fn } |
