diff --git a/nature/processors.lua b/nature/processors.lua new file mode 100644 index 0000000..7ad0da0 --- /dev/null +++ b/nature/processors.lua @@ -0,0 +1,32 @@ +-- @module hilbish.processors + +hilbish.processors = { + list = {} + sorted = {} +} + +function hilbish.processors.add(processor) + if not processor.func then + error 'processor is missing function' + end + + table.insert(hilbish.processors.list, processor) + table.sort(hilbish.processors.list, function(a, b) return a.priority < b.priority end) +end + +--- Run all command processors, in order by priority. +--- It returns the processed command (which may be the same as the passed command) +--- and a boolean which states whether to proceed with command execution. +function hilbish.processors.execute(command) + local continue = true + for _, processor in ipairs(hilbish.processors.list) do + local processed = hilbish.processors.func(command) + if processed.command then command = processed.command end + if not processed.continue then + continue = false + break + end + end + + return command, continue +end