2
2
mirror of https://github.com/Hilbis/Hilbish synced 2025-04-27 07:53:23 +00:00

feat: add names to processors, allow skipping based on name

This commit is contained in:
sammyette 2025-04-23 18:37:03 -04:00
parent 38b5aec99e
commit f48272bbea
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 26 additions and 6 deletions

View File

@ -1,6 +1,7 @@
local fs = require 'fs' local fs = require 'fs'
hilbish.processors.add { hilbish.processors.add {
name = 'hilbish.autocd',
func = function(path) func = function(path)
if hilbish.opts.autocd then if hilbish.opts.autocd then
local ok, stat = pcall(fs.stat, path) local ok, stat = pcall(fs.stat, path)

View File

@ -6,6 +6,10 @@ hilbish.processors = {
} }
function hilbish.processors.add(processor) function hilbish.processors.add(processor)
if not processor.name then
error 'processor is missing name'
end
if not processor.func then if not processor.func then
error 'processor is missing function' error 'processor is missing function'
end end
@ -14,17 +18,32 @@ function hilbish.processors.add(processor)
table.sort(hilbish.processors.list, function(a, b) return a.priority < b.priority end) table.sort(hilbish.processors.list, function(a, b) return a.priority < b.priority end)
end end
local function contains(search, needle)
for _, p in ipairs(search) do
if p == needle then
return true
end
end
return false
end
--- Run all command processors, in order by priority. --- Run all command processors, in order by priority.
--- It returns the processed command (which may be the same as the passed command) --- 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. --- and a boolean which states whether to proceed with command execution.
function hilbish.processors.execute(command) function hilbish.processors.execute(command, opts)
opts = opts or {}
opts.skip = opts.skip or {}
local continue = true local continue = true
for _, processor in ipairs(hilbish.processors.list) do for _, processor in ipairs(hilbish.processors.list) do
local processed = processor.func(command) if not contains(opts.skip, processor.name) then
if processed.command then command = processed.command end local processed = processor.func(command)
if not processed.continue then if processed.command then command = processed.command end
continue = false if not processed.continue then
break continue = false
break
end
end end
end end