fix: make sure complete input is added to history

pull/128/head
TorchedSammy 2022-03-19 18:48:03 -04:00
parent 1e899bf18e
commit 96c1487bfa
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
3 changed files with 23 additions and 20 deletions

2
api.go
View File

@ -172,7 +172,7 @@ func unsetVimMode() {
func hlrun(L *lua.LState) int {
var exitcode uint8
cmd := L.CheckString(1)
err := execCommand(cmd, cmd)
err := execCommand(cmd, true)
if code, ok := interp.IsExitStatus(err); ok {
exitcode = code

30
exec.go
View File

@ -25,7 +25,7 @@ import (
var errNotExec = errors.New("not executable")
func runInput(input, origInput string) {
func runInput(input string, priv bool) {
running = true
cmdString := aliases.Resolve(input)
@ -49,12 +49,12 @@ func runInput(input, origInput string) {
err = l.PCall(0, lua.MultRet, nil)
}
if err == nil {
cmdFinish(0, cmdString, origInput)
cmdFinish(0, cmdString, priv)
return
}
// Last option: use sh interpreter
err = execCommand(cmdString, origInput)
err = execCommand(cmdString, priv)
if err != nil {
// If input is incomplete, start multiline prompting
if syntax.IsIncomplete(err) {
@ -63,34 +63,34 @@ func runInput(input, origInput string) {
if err != nil {
break
}
err = execCommand(cmdString, origInput)
err = execCommand(cmdString, priv)
if syntax.IsIncomplete(err) || strings.HasSuffix(input, "\\") {
continue
} else if code, ok := interp.IsExitStatus(err); ok {
cmdFinish(code, cmdString, origInput)
cmdFinish(code, cmdString, priv)
} else if err != nil {
cmdFinish(1, cmdString, priv)
fmt.Fprintln(os.Stderr, err)
cmdFinish(1, cmdString, origInput)
} else {
cmdFinish(0, cmdString, origInput)
cmdFinish(0, cmdString, priv)
}
break
}
} else {
if code, ok := interp.IsExitStatus(err); ok {
cmdFinish(code, cmdString, origInput)
cmdFinish(code, cmdString, priv)
} else {
cmdFinish(126, cmdString, origInput)
cmdFinish(126, cmdString, priv)
fmt.Fprintln(os.Stderr, err)
}
}
} else {
cmdFinish(0, cmdString, origInput)
cmdFinish(0, cmdString, priv)
}
}
// Run command in sh interpreter
func execCommand(cmd, old string) error {
func execCommand(cmd string, priv bool) error {
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
if err != nil {
return err
@ -141,7 +141,7 @@ func execCommand(cmd, old string) error {
exitcode = uint8(code)
}
cmdFinish(exitcode, argstring, old)
cmdFinish(exitcode, argstring, priv)
return interp.NewExitStatus(exitcode)
}
@ -367,10 +367,10 @@ func splitInput(input string) ([]string, string) {
return cmdArgs, cmdstr.String()
}
func cmdFinish(code uint8, cmdstr, oldInput string) {
func cmdFinish(code uint8, cmdstr string, private bool) {
// if input has space at the beginning, dont put in history
if interactive && !strings.HasPrefix(oldInput, " ") {
handleHistory(strings.TrimSpace(oldInput))
if interactive && !private {
handleHistory(cmdstr)
}
util.SetField(l, hshMod, "exitCode", lua.LNumber(code), "Exit code of last exected command")
hooks.Em.Emit("command.exit", code, cmdstr)

11
main.go
View File

@ -142,12 +142,12 @@ func main() {
scanner := bufio.NewScanner(bufio.NewReader(os.Stdin))
for scanner.Scan() {
text := scanner.Text()
runInput(text, text)
runInput(text, true)
}
}
if *cmdflag != "" {
runInput(*cmdflag, *cmdflag)
runInput(*cmdflag, true)
}
if getopt.NArgs() > 0 {
@ -185,7 +185,10 @@ input:
fmt.Println("^C")
continue
}
oldInput := input
var priv bool
if strings.HasPrefix(input, " ") {
priv = true
}
input = strings.TrimSpace(input)
if len(input) == 0 {
@ -208,7 +211,7 @@ input:
}
}
runInput(input, oldInput)
runInput(input, priv)
termwidth, _, err := term.GetSize(0)
if err != nil {