mirror of
https://github.com/Hilbis/Hilbish
synced 2025-06-23 04:42:03 +00:00
* 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. * feat: allow runners to specify continue in return to prompt for more input * docs: update changelog * refactor: reorder returns of handleSh function * refactor: move out reprompting and runner handling to functions makes codefactor happy hopefully. this commit includes a fix to check if after reprompt the user hits ctrl d and just exits cleanly
66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"hilbish/util"
|
|
|
|
rt "github.com/arnodel/golua/runtime"
|
|
)
|
|
|
|
func runnerModeLoader(rtm *rt.Runtime) *rt.Table {
|
|
exports := map[string]util.LuaExport{
|
|
"sh": {shRunner, 1, false},
|
|
"lua": {luaRunner, 1, false},
|
|
"setMode": {hlrunnerMode, 1, false},
|
|
}
|
|
|
|
mod := rt.NewTable()
|
|
util.SetExports(rtm, mod, exports)
|
|
|
|
return mod
|
|
}
|
|
|
|
func shRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
if err := c.Check1Arg(); err != nil {
|
|
return nil, err
|
|
}
|
|
cmd, err := c.StringArg(0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
input, exitCode, cont, err := handleSh(cmd)
|
|
var luaErr rt.Value = rt.NilValue
|
|
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("continue"), rt.BoolValue(cont))
|
|
runnerRet.Set(rt.StringValue("err"), luaErr)
|
|
|
|
return c.PushingNext(t.Runtime, rt.TableValue(runnerRet)), nil
|
|
}
|
|
|
|
func luaRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
if err := c.Check1Arg(); err != nil {
|
|
return nil, err
|
|
}
|
|
cmd, err := c.StringArg(0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
input, exitCode, err := handleLua(cmd)
|
|
var luaErr rt.Value = rt.NilValue
|
|
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.TableValue(runnerRet)), nil
|
|
}
|