Compare commits

..

No commits in common. "eca808a85289c0dc8773cb72d4971ee9f5abc02d" and "dce49574e63467c217b0125c63f5b8953943ddd9" have entirely different histories.

3 changed files with 13 additions and 78 deletions

View File

@ -46,8 +46,6 @@ includes git commit, branch, and (new!!) release name.
- Added `fg` and `bg` builtins - Added `fg` and `bg` builtins
- `job.foreground()` and `job.background()`, when `job` is a job object, - `job.foreground()` and `job.background()`, when `job` is a job object,
foreground and backgrounds a job respectively. foreground and backgrounds a job respectively.
- Friendlier functions to the `hilbish.runner` interface, which also allow
having and using multiple runners.
### Changed ### Changed
- **Breaking Change:** Upgraded to Lua 5.4. - **Breaking Change:** Upgraded to Lua 5.4.

View File

@ -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. `nil` to indicate no error.
## Functions ## 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` + setMode(mode) > The same as `hilbish.runnerMode`
+ sh(input) -> input, code, err > Runs `input` in Hilbish's sh interpreter + sh(input) -> input, code, err > Runs `input` in Hilbish's sh interpreter
+ lua(input) -> input, code, err > Evals `input` as Lua code + 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.

View File

@ -1,13 +1,8 @@
local currentRunner = 'hybrid' local currentRunner = 'hybrid'
local runnerHandler = {}
local runners = {} local runners = {}
-- lsp shut up function runnerHandler.get(name)
hilbish = hilbish
--- Get a runner by name.
--- @param name string
--- @return table
function hilbish.runner.get(name)
local r = runners[name] local r = runners[name]
if not r then if not r then
@ -17,34 +12,23 @@ function hilbish.runner.get(name)
return r return r
end end
--- Adds a runner to the table of available runners. If runner is a table, function runnerHandler.add(name, runner)
--- it must have the run function in it.
--- @param name string
--- @param runner function | table
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
if type(runner) == 'function' then
runner = {run = runner} -- this probably looks confusing
end
if type(runner) ~= 'table' then if type(runner) ~= 'table' then
error 'expected runner to be a table or function' error 'expected runner to be a table'
end end
if runners[name] then if runners[name] then
error(string.format('runner %s already exists', name)) error(string.format('runner %s already exists', name))
end end
hilbish.runner.set(name, runner) runnerHandler.set(name, runner)
end end
--- Sets a runner by name. The runner table must have the run function in it. function runnerHandler.set(name, runner)
--- @param name string
--- @param runner table
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
@ -52,55 +36,21 @@ function hilbish.runner.set(name, runner)
runners[name] = runner runners[name] = runner
end end
--- Executes cmd with a runner. If runnerName isn't passed, it uses function runnerHandler.exec(cmd, runnerName)
--- the user's current runner.
--- @param cmd string
--- @param runnerName string?
--- @return string, number, string
function hilbish.runner.exec(cmd, runnerName)
if not runnerName then runnerName = currentRunner end if not runnerName then runnerName = currentRunner end
local r = hilbish.runner.get(runnerName) local r = runnerHandler.get(runnerName)
return r.run(cmd) return r.run(cmd)
end end
--- Sets the current interactive/command line runner mode. function runnerHandler.setCurrent(name)
--- @param name string local r = runnerHandler.get(name)
function hilbish.runner.setCurrent(name)
local r = hilbish.runner.get(name)
currentRunner = name currentRunner = name
hilbish.runner.setMode(r.run) hilbish.runner.setMode(r.run)
end end
hilbish.runner.add('hybrid', function(input) -- add functions to hilbish.runner
local cmdStr = hilbish.aliases.resolve(input) for k, v in ipairs(runnerHandler) do hilbish.runner[k] = v end
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)