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 { func hlrun(L *lua.LState) int {
var exitcode uint8 var exitcode uint8
cmd := L.CheckString(1) cmd := L.CheckString(1)
err := execCommand(cmd, cmd) err := execCommand(cmd, true)
if code, ok := interp.IsExitStatus(err); ok { if code, ok := interp.IsExitStatus(err); ok {
exitcode = code exitcode = code

30
exec.go
View File

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