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
runner-prompt
TorchedSammy 2022-06-02 22:20:44 -04:00
parent ade570d598
commit 0c3028bb03
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 52 additions and 34 deletions

70
exec.go
View File

@ -116,13 +116,53 @@ func runInput(input string, priv bool) {
} }
} else { } else {
// can only be a string or function so // can only be a string or function so
term := rt.NewTerminationWith(l.MainThread().CurrentCont(), 3, false) input, exitCode, cont, err = runLuaRunner(currentRunner, input)
err = rt.Call(l.MainThread(), currentRunner, []rt.Value{rt.StringValue(input)}, term)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
cmdFinish(124, input, priv) cmdFinish(124, input, priv)
return return
} }
}
if cont {
input, err = reprompt(input)
if err == nil {
goto rerun
} else if err == io.EOF {
return
}
}
if err != nil {
if exErr, ok := isExecError(err); ok {
hooks.Em.Emit("command." + exErr.typ, exErr.cmd)
err = exErr.sprint()
}
fmt.Fprintln(os.Stderr, err)
}
cmdFinish(exitCode, input, priv)
}
func reprompt(input string) (string, error) {
for {
in, err := continuePrompt(strings.TrimSuffix(input, "\\"))
if err != nil {
return input, err
}
if strings.HasSuffix(in, "\\") {
continue
}
return in, nil
}
}
func runLuaRunner(runr rt.Value, userInput string) (input string, exitCode uint8, continued bool, err error) {
term := rt.NewTerminationWith(l.MainThread().CurrentCont(), 3, false)
err = rt.Call(l.MainThread(), runr, []rt.Value{rt.StringValue(userInput)}, term)
if err != nil {
return
}
var runner *rt.Table var runner *rt.Table
var ok bool var ok bool
@ -144,31 +184,9 @@ func runInput(input string, priv bool) {
} }
if c, ok := runner.Get(rt.StringValue("continue")).TryBool(); ok { if c, ok := runner.Get(rt.StringValue("continue")).TryBool(); ok {
cont = c continued = c
} }
} return
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)
err = exErr.sprint()
}
fmt.Fprintln(os.Stderr, err)
}
cmdFinish(exitCode, input, priv)
} }
func handleLua(cmdString string) (string, uint8, error) { func handleLua(cmdString string) (string, uint8, error) {