refactor: set functions in hilbish.runner directly

runners
TorchedSammy 2022-05-28 18:56:40 -04:00
parent b1153ca4b1
commit d3d15bc85a
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 15 additions and 18 deletions

View File

@ -1,11 +1,13 @@
local currentRunner = 'hybrid' local currentRunner = 'hybrid'
local runnerHandler = {}
local runners = {} local runners = {}
-- lsp shut up
hilbish = hilbish
--- Get a runner by name. --- Get a runner by name.
--- @param name string --- @param name string
--- @return table --- @return table
function runnerHandler.get(name) function hilbish.runner.get(name)
local r = runners[name] local r = runners[name]
if not r then if not r then
@ -19,7 +21,7 @@ end
--- it must have the run function in it. --- it must have the run function in it.
--- @param name string --- @param name string
--- @param runner function | table --- @param runner function | table
function runnerHandler.add(name, runner) function hilbish.runner.add(name, runner)
if type(name) ~= 'string' then if type(name) ~= 'string' then
error 'expected runner name to be a table' error 'expected runner name to be a table'
end end
@ -36,13 +38,13 @@ function runnerHandler.add(name, runner)
error(string.format('runner %s already exists', name)) error(string.format('runner %s already exists', name))
end end
runnerHandler.set(name, runner) hilbish.runner.set(name, runner)
end end
--- Sets a runner by name. The runner table must have the run function in it. --- Sets a runner by name. The runner table must have the run function in it.
--- @param name string --- @param name string
--- @param runner table --- @param runner table
function runnerHandler.set(name, runner) function hilbish.runner.set(name, runner)
if not runner.run or type(runner.run) ~= 'function' then if not runner.run or type(runner.run) ~= 'function' then
error 'run function in runner missing' error 'run function in runner missing'
end end
@ -55,29 +57,24 @@ end
--- @param cmd string --- @param cmd string
--- @param runnerName string? --- @param runnerName string?
--- @return string, number, string --- @return string, number, string
function runnerHandler.exec(cmd, runnerName) function hilbish.runner.exec(cmd, runnerName)
if not runnerName then runnerName = currentRunner end if not runnerName then runnerName = currentRunner end
local r = runnerHandler.get(runnerName) local r = hilbish.runner.get(runnerName)
return r.run(cmd) return r.run(cmd)
end end
-- lsp shut up
hilbish = hilbish
--- Sets the current interactive/command line runner mode. --- Sets the current interactive/command line runner mode.
--- @param name string --- @param name string
function runnerHandler.setCurrent(name) function hilbish.runner.setCurrent(name)
local r = runnerHandler.get(name) local r = hilbish.runner.get(name)
currentRunner = name currentRunner = name
hilbish.runner.setMode(r.run) hilbish.runner.setMode(r.run)
end end
-- add functions to hilbish.runner hilbish.runner.add('hybrid', function(input)
for k, v in pairs(runnerHandler) do hilbish.runner[k] = v end
runnerHandler.add('hybrid', function(input)
local cmdStr = hilbish.aliases.resolve(input) local cmdStr = hilbish.aliases.resolve(input)
local _, _, err = hilbish.runner.lua(cmdStr) local _, _, err = hilbish.runner.lua(cmdStr)
@ -88,7 +85,7 @@ runnerHandler.add('hybrid', function(input)
return hilbish.runner.sh(input) return hilbish.runner.sh(input)
end) end)
runnerHandler.add('hybridRev', function(input) hilbish.runner.add('hybridRev', function(input)
local _, _, err = hilbish.runner.sh(input) local _, _, err = hilbish.runner.sh(input)
if not err then if not err then
return input, 0, nil return input, 0, nil
@ -98,12 +95,12 @@ runnerHandler.add('hybridRev', function(input)
return hilbish.runner.lua(cmdStr) return hilbish.runner.lua(cmdStr)
end) end)
runnerHandler.add('lua', function(input) hilbish.runner.add('lua', function(input)
local cmdStr = hilbish.aliases.resolve(input) local cmdStr = hilbish.aliases.resolve(input)
return hilbish.runner.lua(cmdStr) return hilbish.runner.lua(cmdStr)
end) end)
runnerHandler.add('sh', function(input) hilbish.runner.add('sh', function(input)
return hilbish.runner.sh(input) return hilbish.runner.sh(input)
end) end)