2022-05-28 17:54:53 +00:00
|
|
|
local currentRunner = 'hybrid'
|
|
|
|
local runners = {}
|
|
|
|
|
2022-05-28 22:56:40 +00:00
|
|
|
-- lsp shut up
|
|
|
|
hilbish = hilbish
|
|
|
|
|
2022-05-28 22:55:18 +00:00
|
|
|
--- Get a runner by name.
|
|
|
|
--- @param name string
|
|
|
|
--- @return table
|
2022-05-28 22:56:40 +00:00
|
|
|
function hilbish.runner.get(name)
|
2022-05-28 17:54:53 +00:00
|
|
|
local r = runners[name]
|
|
|
|
|
|
|
|
if not r then
|
|
|
|
error(string.format('runner %s does not exist', name))
|
|
|
|
end
|
|
|
|
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
2022-05-28 22:55:18 +00:00
|
|
|
--- Adds a runner to the table of available runners. If runner is a table,
|
|
|
|
--- it must have the run function in it.
|
|
|
|
--- @param name string
|
|
|
|
--- @param runner function | table
|
2022-05-28 22:56:40 +00:00
|
|
|
function hilbish.runner.add(name, runner)
|
2022-05-28 17:54:53 +00:00
|
|
|
if type(name) ~= 'string' then
|
|
|
|
error 'expected runner name to be a table'
|
|
|
|
end
|
|
|
|
|
2022-05-28 22:22:20 +00:00
|
|
|
if type(runner) == 'function' then
|
|
|
|
runner = {run = runner} -- this probably looks confusing
|
|
|
|
end
|
|
|
|
|
2022-05-28 17:54:53 +00:00
|
|
|
if type(runner) ~= 'table' then
|
2022-05-28 22:22:20 +00:00
|
|
|
error 'expected runner to be a table or function'
|
2022-05-28 17:54:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if runners[name] then
|
|
|
|
error(string.format('runner %s already exists', name))
|
|
|
|
end
|
|
|
|
|
2022-05-28 22:56:40 +00:00
|
|
|
hilbish.runner.set(name, runner)
|
2022-05-28 17:54:53 +00:00
|
|
|
end
|
|
|
|
|
2022-05-28 22:55:18 +00:00
|
|
|
--- Sets a runner by name. The runner table must have the run function in it.
|
|
|
|
--- @param name string
|
|
|
|
--- @param runner table
|
2022-05-28 22:56:40 +00:00
|
|
|
function hilbish.runner.set(name, runner)
|
2022-05-28 17:54:53 +00:00
|
|
|
if not runner.run or type(runner.run) ~= 'function' then
|
|
|
|
error 'run function in runner missing'
|
|
|
|
end
|
|
|
|
|
|
|
|
runners[name] = runner
|
|
|
|
end
|
|
|
|
|
2022-05-28 22:55:18 +00:00
|
|
|
--- Executes cmd with a runner. If runnerName isn't passed, it uses
|
|
|
|
--- the user's current runner.
|
|
|
|
--- @param cmd string
|
|
|
|
--- @param runnerName string?
|
|
|
|
--- @return string, number, string
|
2022-05-28 22:56:40 +00:00
|
|
|
function hilbish.runner.exec(cmd, runnerName)
|
2022-05-28 17:54:53 +00:00
|
|
|
if not runnerName then runnerName = currentRunner end
|
|
|
|
|
2022-05-28 22:56:40 +00:00
|
|
|
local r = hilbish.runner.get(runnerName)
|
2022-05-28 17:54:53 +00:00
|
|
|
|
|
|
|
return r.run(cmd)
|
|
|
|
end
|
|
|
|
|
2022-05-28 22:55:18 +00:00
|
|
|
--- Sets the current interactive/command line runner mode.
|
|
|
|
--- @param name string
|
2022-05-28 22:56:40 +00:00
|
|
|
function hilbish.runner.setCurrent(name)
|
|
|
|
local r = hilbish.runner.get(name)
|
2022-05-28 17:54:53 +00:00
|
|
|
currentRunner = name
|
|
|
|
|
|
|
|
hilbish.runner.setMode(r.run)
|
|
|
|
end
|
|
|
|
|
2022-05-28 22:56:40 +00:00
|
|
|
hilbish.runner.add('hybrid', function(input)
|
2022-05-28 22:47:08 +00:00
|
|
|
local cmdStr = hilbish.aliases.resolve(input)
|
|
|
|
|
|
|
|
local _, _, err = hilbish.runner.lua(cmdStr)
|
|
|
|
if not err then
|
|
|
|
return input, 0, nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return hilbish.runner.sh(input)
|
|
|
|
end)
|
|
|
|
|
2022-05-28 22:56:40 +00:00
|
|
|
hilbish.runner.add('hybridRev', function(input)
|
2022-05-28 22:47:08 +00:00
|
|
|
local _, _, err = hilbish.runner.sh(input)
|
|
|
|
if not err then
|
|
|
|
return input, 0, nil
|
|
|
|
end
|
|
|
|
|
|
|
|
local cmdStr = hilbish.aliases.resolve(input)
|
|
|
|
return hilbish.runner.lua(cmdStr)
|
|
|
|
end)
|
|
|
|
|
2022-05-28 22:56:40 +00:00
|
|
|
hilbish.runner.add('lua', function(input)
|
2022-05-28 22:47:08 +00:00
|
|
|
local cmdStr = hilbish.aliases.resolve(input)
|
|
|
|
return hilbish.runner.lua(cmdStr)
|
|
|
|
end)
|
|
|
|
|
2022-05-28 22:56:40 +00:00
|
|
|
hilbish.runner.add('sh', function(input)
|
2022-05-28 22:47:08 +00:00
|
|
|
return hilbish.runner.sh(input)
|
|
|
|
end)
|
|
|
|
|