mirror of https://github.com/Hilbis/Hilbish
Compare commits
No commits in common. "eca808a85289c0dc8773cb72d4971ee9f5abc02d" and "dce49574e63467c217b0125c63f5b8953943ddd9" have entirely different histories.
eca808a852
...
dce49574e6
|
@ -46,8 +46,6 @@ includes git commit, branch, and (new!!) release name.
|
|||
- Added `fg` and `bg` builtins
|
||||
- `job.foreground()` and `job.background()`, when `job` is a job object,
|
||||
foreground and backgrounds a job respectively.
|
||||
- Friendlier functions to the `hilbish.runner` interface, which also allow
|
||||
having and using multiple runners.
|
||||
|
||||
### Changed
|
||||
- **Breaking Change:** Upgraded to Lua 5.4.
|
||||
|
|
|
@ -35,21 +35,8 @@ The exit code has to be a number, it will be 0 otherwise and the error can be
|
|||
`nil` to indicate no error.
|
||||
|
||||
## Functions
|
||||
These are the "low level" functions for the `hilbish.runner` interface.
|
||||
These are the functions for the `hilbish.runner` interface
|
||||
|
||||
+ setMode(mode) > The same as `hilbish.runnerMode`
|
||||
+ sh(input) -> input, code, err > Runs `input` in Hilbish's sh interpreter
|
||||
+ lua(input) -> input, code, err > Evals `input` as Lua code
|
||||
|
||||
The others here are defined in Lua and have EmmyLua documentation.
|
||||
These functions should be preferred over the previous ones.
|
||||
+ setCurrent(mode) > The same as `setMode`, but works with runners managed
|
||||
via the functions below.
|
||||
+ add(name, runner) > Adds a runner to a table of available runners. The `runner`
|
||||
argument is either a function or a table with a run callback.
|
||||
+ set(name, runner) > The same as `add` but requires passing a table and
|
||||
overwrites if the `name`d runner already exists.
|
||||
+ get(name) > runner > Gets a runner by name. It is a table with at least a
|
||||
run function, to run input.
|
||||
+ exec(cmd, runnerName) > Runs `cmd` with a runner. If `runnerName` isn't passed,
|
||||
the current runner mode is used.
|
||||
|
|
|
@ -1,13 +1,8 @@
|
|||
local currentRunner = 'hybrid'
|
||||
local runnerHandler = {}
|
||||
local runners = {}
|
||||
|
||||
-- lsp shut up
|
||||
hilbish = hilbish
|
||||
|
||||
--- Get a runner by name.
|
||||
--- @param name string
|
||||
--- @return table
|
||||
function hilbish.runner.get(name)
|
||||
function runnerHandler.get(name)
|
||||
local r = runners[name]
|
||||
|
||||
if not r then
|
||||
|
@ -17,34 +12,23 @@ function hilbish.runner.get(name)
|
|||
return r
|
||||
end
|
||||
|
||||
--- 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
|
||||
function hilbish.runner.add(name, runner)
|
||||
function runnerHandler.add(name, runner)
|
||||
if type(name) ~= 'string' then
|
||||
error 'expected runner name to be a table'
|
||||
end
|
||||
|
||||
if type(runner) == 'function' then
|
||||
runner = {run = runner} -- this probably looks confusing
|
||||
end
|
||||
|
||||
if type(runner) ~= 'table' then
|
||||
error 'expected runner to be a table or function'
|
||||
error 'expected runner to be a table'
|
||||
end
|
||||
|
||||
if runners[name] then
|
||||
error(string.format('runner %s already exists', name))
|
||||
end
|
||||
|
||||
hilbish.runner.set(name, runner)
|
||||
runnerHandler.set(name, runner)
|
||||
end
|
||||
|
||||
--- Sets a runner by name. The runner table must have the run function in it.
|
||||
--- @param name string
|
||||
--- @param runner table
|
||||
function hilbish.runner.set(name, runner)
|
||||
function runnerHandler.set(name, runner)
|
||||
if not runner.run or type(runner.run) ~= 'function' then
|
||||
error 'run function in runner missing'
|
||||
end
|
||||
|
@ -52,55 +36,21 @@ function hilbish.runner.set(name, runner)
|
|||
runners[name] = runner
|
||||
end
|
||||
|
||||
--- 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
|
||||
function hilbish.runner.exec(cmd, runnerName)
|
||||
function runnerHandler.exec(cmd, runnerName)
|
||||
if not runnerName then runnerName = currentRunner end
|
||||
|
||||
local r = hilbish.runner.get(runnerName)
|
||||
local r = runnerHandler.get(runnerName)
|
||||
|
||||
return r.run(cmd)
|
||||
end
|
||||
|
||||
--- Sets the current interactive/command line runner mode.
|
||||
--- @param name string
|
||||
function hilbish.runner.setCurrent(name)
|
||||
local r = hilbish.runner.get(name)
|
||||
function runnerHandler.setCurrent(name)
|
||||
local r = runnerHandler.get(name)
|
||||
currentRunner = name
|
||||
|
||||
hilbish.runner.setMode(r.run)
|
||||
end
|
||||
|
||||
hilbish.runner.add('hybrid', function(input)
|
||||
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)
|
||||
|
||||
hilbish.runner.add('hybridRev', function(input)
|
||||
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)
|
||||
|
||||
hilbish.runner.add('lua', function(input)
|
||||
local cmdStr = hilbish.aliases.resolve(input)
|
||||
return hilbish.runner.lua(cmdStr)
|
||||
end)
|
||||
|
||||
hilbish.runner.add('sh', function(input)
|
||||
return hilbish.runner.sh(input)
|
||||
end)
|
||||
-- add functions to hilbish.runner
|
||||
for k, v in ipairs(runnerHandler) do hilbish.runner[k] = v end
|
||||
|
||||
|
|
Loading…
Reference in New Issue