From 9b60dbfe99002a6203ad9dad7499e432f8a94426 Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Sun, 29 May 2022 21:33:52 -0400 Subject: [PATCH] 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. --- exec.go | 15 +++++++++------ nature/opts/autocd.lua | 18 ++++++++++-------- nature/runner.lua | 12 ++++++------ runnermode.go | 12 ++++++++++-- 4 files changed, 35 insertions(+), 22 deletions(-) diff --git a/exec.go b/exec.go index 2e0e0b1..3bd01f7 100644 --- a/exec.go +++ b/exec.go @@ -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) } } diff --git a/nature/opts/autocd.lua b/nature/opts/autocd.lua index b25732e..ac32f32 100644 --- a/nature/opts/autocd.lua +++ b/nature/opts/autocd.lua @@ -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) diff --git a/nature/runner.lua b/nature/runner.lua index ed1b7dd..e155f63 100644 --- a/nature/runner.lua +++ b/nature/runner.lua @@ -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) diff --git a/runnermode.go b/runnermode.go index 4285142..d5dc722 100644 --- a/runnermode.go +++ b/runnermode.go @@ -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 }