feat: allow runners to specify continue in return to prompt for more input

runner-prompt
TorchedSammy 2022-05-29 22:13:14 -04:00
parent 9b60dbfe99
commit 7dc438cc97
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 35 additions and 28 deletions

60
exec.go
View File

@ -87,19 +87,23 @@ func runInput(input string, priv bool) {
cmdString := aliases.Resolve(input)
hooks.Em.Emit("command.preexec", input, cmdString)
rerun:
var exitCode uint8
var err error
if runnerMode.Type() == rt.StringType {
switch runnerMode.AsString() {
var cont bool
// save incase it changes while prompting (For some reason)
currentRunner := runnerMode
if currentRunner.Type() == rt.StringType {
switch currentRunner.AsString() {
case "hybrid":
_, _, err = handleLua(cmdString)
if err == nil {
cmdFinish(0, input, priv)
return
}
input, exitCode, err = handleSh(input)
input, exitCode, err, cont = handleSh(input)
case "hybridRev":
_, _, err = handleSh(input)
_, _, err, cont = handleSh(input)
if err == nil {
cmdFinish(0, input, priv)
return
@ -108,12 +112,12 @@ func runInput(input string, priv bool) {
case "lua":
input, exitCode, err = handleLua(cmdString)
case "sh":
input, exitCode, err = handleSh(input)
input, exitCode, err, cont = handleSh(input)
}
} else {
// can only be a string or function so
term := rt.NewTerminationWith(l.MainThread().CurrentCont(), 3, false)
err = rt.Call(l.MainThread(), runnerMode, []rt.Value{rt.StringValue(input)}, term)
err = rt.Call(l.MainThread(), currentRunner, []rt.Value{rt.StringValue(input)}, term)
if err != nil {
fmt.Fprintln(os.Stderr, err)
cmdFinish(124, input, priv)
@ -138,8 +142,25 @@ func runInput(input string, priv bool) {
if errStr, ok := runner.Get(rt.StringValue("err")).TryString(); ok {
err = fmt.Errorf("%s", errStr)
}
if c, ok := runner.Get(rt.StringValue("continue")).TryBool(); ok {
cont = c
}
}
if cont {
for {
input, err = continuePrompt(strings.TrimSuffix(input, "\\"))
if err != nil {
break
}
if strings.HasSuffix(cmdString, "\\") {
continue
}
goto rerun
}
}
if err != nil {
if exErr, ok := isExecError(err); ok {
hooks.Em.Emit("command." + exErr.typ, exErr.cmd)
@ -176,40 +197,25 @@ func handleLua(cmdString string) (string, uint8, error) {
return cmdString, 125, err
}
func handleSh(cmdString string) (string, uint8, error) {
func handleSh(cmdString string) (string, uint8, error, bool) {
_, _, err := execCommand(cmdString, true)
if err != nil {
// If input is incomplete, start multiline prompting
if syntax.IsIncomplete(err) {
if !interactive {
return cmdString, 126, err
}
for {
cmdString, err = continuePrompt(strings.TrimSuffix(cmdString, "\\"))
if err != nil {
break
}
_, _, err = execCommand(cmdString, true)
if syntax.IsIncomplete(err) || strings.HasSuffix(cmdString, "\\") {
continue
} else if code, ok := interp.IsExitStatus(err); ok {
return cmdString, code, nil
} else if err != nil {
return cmdString, 126, err
} else {
return cmdString, 0, nil
}
return cmdString, 126, err, false
}
return cmdString, 126, err, true
} else {
if code, ok := interp.IsExitStatus(err); ok {
return cmdString, code, nil
return cmdString, code, nil, false
} else {
return cmdString, 126, err
return cmdString, 126, err, false
}
}
}
return cmdString, 0, nil
return cmdString, 0, nil, false
}
// Run command in sh interpreter

View File

@ -28,7 +28,7 @@ func shRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return nil, err
}
input, exitCode, err := handleSh(cmd)
input, exitCode, err, cont := handleSh(cmd)
var luaErr rt.Value = rt.NilValue
if err != nil {
luaErr = rt.StringValue(err.Error())
@ -36,6 +36,7 @@ func shRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, 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