refactor!: make runners require returning a table

allows for more options for runners in the future,
and makes it so that you can avoid passing
certain args more easily.
runner-prompt
TorchedSammy 2022-05-29 21:33:52 -04:00
parent f9a2a981b4
commit 9b60dbfe99
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
4 changed files with 35 additions and 22 deletions

15
exec.go
View File

@ -120,19 +120,22 @@ func runInput(input string, priv bool) {
return
}
luaInput := term.Get(0)
luaexitcode := term.Get(1)
runErr := term.Get(2)
var runner *rt.Table
var ok bool
runnerRet := term.Get(0)
if runner, ok = runnerRet.TryTable(); !ok {
fmt.Fprintln(os.Stderr, "runner did not return a table")
}
if code, ok := luaexitcode.TryInt(); ok {
if code, ok := runner.Get(rt.StringValue("exitCode")).TryInt(); ok {
exitCode = uint8(code)
}
if inp, ok := luaInput.TryString(); ok {
if inp, ok := runner.Get(rt.StringValue("input")).TryString(); ok {
input = inp
}
if errStr, ok := runErr.TryString(); ok {
if errStr, ok := runner.Get(rt.StringValue("err")).TryString(); ok {
err = fmt.Errorf("%s", errStr)
}
}

View File

@ -1,23 +1,25 @@
local fs = require 'fs'
function cdHandle(inp)
local input, exit, err = hilbish.runner.lua(inp)
local res = hilbish.runner.lua(inp)
if not err then
return input, exit, err
if not res.err then
return res
end
input, exit, err = hilbish.runner.sh(inp)
res = hilbish.runner.sh(inp)
if exit ~= 0 and hilbish.opts.autocd then
local ok, stat = pcall(fs.stat, input)
if res.exit ~= 0 and hilbish.opts.autocd then
local ok, stat = pcall(fs.stat, res.input)
if ok and stat.isDir then
-- discard here to not append the cd, which will be in history
_, exit, err = hilbish.runner.sh('cd ' .. input)
local _, exitCode, err = hilbish.runner.sh('cd ' .. res.input)
res.exitCode = exitCode
res.err = err
end
end
return input, exit, err
return res
end
hilbish.runner.setMode(cdHandle)

View File

@ -77,18 +77,18 @@ 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
local res = hilbish.runner.lua(cmdStr)
if not res.err then
return res
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
local res = hilbish.runner.sh(input)
if not res.err then
return res
end
local cmdStr = hilbish.aliases.resolve(input)

View File

@ -33,8 +33,12 @@ func shRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err != nil {
luaErr = rt.StringValue(err.Error())
}
runnerRet := rt.NewTable()
runnerRet.Set(rt.StringValue("input"), rt.StringValue(input))
runnerRet.Set(rt.StringValue("exitCode"), rt.IntValue(int64(exitCode)))
runnerRet.Set(rt.StringValue("err"), luaErr)
return c.PushingNext(t.Runtime, rt.StringValue(input), rt.IntValue(int64(exitCode)), luaErr), nil
return c.PushingNext(t.Runtime, rt.TableValue(runnerRet)), nil
}
func luaRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
@ -51,6 +55,10 @@ func luaRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err != nil {
luaErr = rt.StringValue(err.Error())
}
runnerRet := rt.NewTable()
runnerRet.Set(rt.StringValue("input"), rt.StringValue(input))
runnerRet.Set(rt.StringValue("exitCode"), rt.IntValue(int64(exitCode)))
runnerRet.Set(rt.StringValue("err"), luaErr)
return c.PushingNext(t.Runtime, rt.StringValue(input), rt.IntValue(int64(exitCode)), luaErr), nil
return c.PushingNext(t.Runtime, rt.TableValue(runnerRet)), nil
}