mirror of
https://github.com/Hilbis/Hilbish
synced 2025-06-14 16:32:03 +00:00
feat: add processors api (#343)
* feat: add processors api * fix: make processor work properly, implement in exec code * feat: add names to processors, allow skipping based on name * feat: add opt to skip processors * feat: allow processors to set if history should be saved
This commit is contained in:
parent
aec17d1077
commit
1bb433dc64
@ -6,7 +6,7 @@ vars:
|
|||||||
PREFIX: '{{default "/usr/local" .PREFIX}}'
|
PREFIX: '{{default "/usr/local" .PREFIX}}'
|
||||||
bindir__: '{{.PREFIX}}/bin'
|
bindir__: '{{.PREFIX}}/bin'
|
||||||
BINDIR: '{{default .bindir__ .BINDIR}}'
|
BINDIR: '{{default .bindir__ .BINDIR}}'
|
||||||
libdir__: '{{.PREFIX}}/share/hilbish'
|
libdir__: ''
|
||||||
LIBDIR: '{{default .libdir__ .LIBDIR}}'
|
LIBDIR: '{{default .libdir__ .LIBDIR}}'
|
||||||
goflags__: '-ldflags "-s -w -X main.dataDir={{.LIBDIR}}"'
|
goflags__: '-ldflags "-s -w -X main.dataDir={{.LIBDIR}}"'
|
||||||
GOFLAGS: '{{default .goflags__ .GOFLAGS}}'
|
GOFLAGS: '{{default .goflags__ .GOFLAGS}}'
|
||||||
|
@ -76,3 +76,8 @@ of an exact match.
|
|||||||
#### Default: `true`
|
#### Default: `true`
|
||||||
If this is enabled, when a background job is finished,
|
If this is enabled, when a background job is finished,
|
||||||
a [notification](../notifications) will be sent.
|
a [notification](../notifications) will be sent.
|
||||||
|
|
||||||
|
### `processorSkipList`
|
||||||
|
#### Value: `table`
|
||||||
|
#### Default: `{}`
|
||||||
|
A table listing the names of command processors to skip.
|
||||||
|
@ -19,6 +19,7 @@ table.insert(package.searchers, function(module)
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
require 'nature.hilbish'
|
require 'nature.hilbish'
|
||||||
|
require 'nature.processors'
|
||||||
|
|
||||||
require 'nature.commands'
|
require 'nature.commands'
|
||||||
require 'nature.completions'
|
require 'nature.completions'
|
||||||
|
@ -1,18 +1,27 @@
|
|||||||
local fs = require 'fs'
|
local fs = require 'fs'
|
||||||
|
|
||||||
local oldShRunner = hilbish.runner.sh
|
hilbish.processors.add {
|
||||||
function hilbish.runner.sh(input)
|
name = 'hilbish.autocd',
|
||||||
local res = oldShRunner(input)
|
func = function(path)
|
||||||
|
if hilbish.opts.autocd then
|
||||||
|
local ok, stat = pcall(fs.stat, path)
|
||||||
|
if ok and stat.isDir then
|
||||||
|
local oldPath = hilbish.cwd()
|
||||||
|
|
||||||
if res.exit ~= 0 and hilbish.opts.autocd then
|
local absPath = fs.abs(path)
|
||||||
local ok, stat = pcall(fs.stat, res.input)
|
fs.cd(path)
|
||||||
if ok and stat.isDir then
|
|
||||||
-- discard here to not append the cd, which will be in history
|
bait.throw('cd', path, oldPath)
|
||||||
local _, exitCode, err = hilbish.runner.sh('cd ' .. res.input)
|
bait.throw('hilbish.cd', absPath, oldPath)
|
||||||
res.exitCode = exitCode
|
|
||||||
res.err = err
|
end
|
||||||
|
return {
|
||||||
|
continue = not ok
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return {
|
||||||
|
continue = true
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
}
|
||||||
return res
|
|
||||||
end
|
|
||||||
|
@ -2,7 +2,7 @@ hilbish.opts = {}
|
|||||||
|
|
||||||
local function setupOpt(name, default)
|
local function setupOpt(name, default)
|
||||||
hilbish.opts[name] = default
|
hilbish.opts[name] = default
|
||||||
pcall(require, 'nature.opts.' .. name)
|
local ok, err = pcall(require, 'nature.opts.' .. name)
|
||||||
end
|
end
|
||||||
|
|
||||||
local defaultOpts = {
|
local defaultOpts = {
|
||||||
@ -15,7 +15,8 @@ The nice lil shell for {blue}Lua{reset} fanatics!
|
|||||||
fuzzy = false,
|
fuzzy = false,
|
||||||
notifyJobFinish = true,
|
notifyJobFinish = true,
|
||||||
crimmas = true,
|
crimmas = true,
|
||||||
tips = true
|
tips = true,
|
||||||
|
processorSkipList = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
for optsName, default in pairs(defaultOpts) do
|
for optsName, default in pairs(defaultOpts) do
|
||||||
|
57
nature/processors.lua
Normal file
57
nature/processors.lua
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
-- @module hilbish.processors
|
||||||
|
|
||||||
|
hilbish.processors = {
|
||||||
|
list = {},
|
||||||
|
sorted = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
function hilbish.processors.add(processor)
|
||||||
|
if not processor.name then
|
||||||
|
error 'processor is missing name'
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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.
|
||||||
|
--- 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, opts)
|
||||||
|
opts = opts or {}
|
||||||
|
opts.skip = opts.skip or {}
|
||||||
|
|
||||||
|
local continue = true
|
||||||
|
local history
|
||||||
|
for _, processor in ipairs(hilbish.processors.list) do
|
||||||
|
if not contains(opts.skip, processor.name) then
|
||||||
|
local processed = processor.func(command)
|
||||||
|
if processed.history ~= nil then history = processed.history end
|
||||||
|
if processed.command then command = processed.command end
|
||||||
|
if not processed.continue then
|
||||||
|
continue = false
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
command = command,
|
||||||
|
continue = continue,
|
||||||
|
history = history
|
||||||
|
}
|
||||||
|
end
|
@ -122,12 +122,22 @@ end
|
|||||||
-- @param input string
|
-- @param input string
|
||||||
-- @param priv bool
|
-- @param priv bool
|
||||||
function hilbish.runner.run(input, priv)
|
function hilbish.runner.run(input, priv)
|
||||||
local command = hilbish.aliases.resolve(input)
|
bait.throw('command.preprocess', input)
|
||||||
bait.throw('command.preexec', input, command)
|
local processed = hilbish.processors.execute(input, {
|
||||||
|
skip = hilbish.opts.processorSkipList
|
||||||
|
})
|
||||||
|
priv = processed.history ~= nil and (not processed.history) or priv
|
||||||
|
if not processed.continue then
|
||||||
|
finishExec(0, '', true)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local command = hilbish.aliases.resolve(processed.command)
|
||||||
|
bait.throw('command.preexec', processed.command, command)
|
||||||
|
|
||||||
::rerun::
|
::rerun::
|
||||||
local runner = hilbish.runner.get(currentRunner)
|
local runner = hilbish.runner.get(currentRunner)
|
||||||
local ok, out = pcall(runner.run, input)
|
local ok, out = pcall(runner.run, processed.command)
|
||||||
if not ok then
|
if not ok then
|
||||||
io.stderr:write(out .. '\n')
|
io.stderr:write(out .. '\n')
|
||||||
finishExec(124, out.input, priv)
|
finishExec(124, out.input, priv)
|
||||||
@ -135,9 +145,9 @@ function hilbish.runner.run(input, priv)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if out.continue then
|
if out.continue then
|
||||||
local contInput = continuePrompt(input, out.newline)
|
local contInput = continuePrompt(processed.command, out.newline)
|
||||||
if contInput then
|
if contInput then
|
||||||
input = contInput
|
processed.command = contInput
|
||||||
goto rerun
|
goto rerun
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user