fix: add full command after complete prompt to history

windows-fixes
TorchedSammy 2022-02-27 19:12:58 -04:00
parent b221921ab7
commit d9d2152e04
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
4 changed files with 22 additions and 20 deletions

View File

@ -19,6 +19,7 @@ with Lua
- Home dir is now added to recent dirs (the case of cd with no arg)
- `index` subdoc will no longer appear
- Alias expansion with quotes
- Add full command to history in the case of incomplete input
### Changed
- The minimal config is truly minimal now

2
api.go
View File

@ -88,7 +88,7 @@ The nice lil shell for {blue}Lua{reset} fanatics!
func hlrun(L *lua.LState) int {
var exitcode uint8
cmd := L.CheckString(1)
err := execCommand(cmd)
err := execCommand(cmd, cmd)
if code, ok := interp.IsExitStatus(err); ok {
exitcode = code

26
exec.go
View File

@ -15,7 +15,7 @@ import (
"mvdan.cc/sh/v3/syntax"
)
func runInput(input string) {
func runInput(input, origInput string) {
running = true
cmdString := aliases.Resolve(input)
@ -39,12 +39,12 @@ func runInput(input string) {
err = l.PCall(0, lua.MultRet, nil)
}
if err == nil {
cmdFinish(0, cmdString)
cmdFinish(0, cmdString, origInput)
return
}
// Last option: use sh interpreter
err = execCommand(cmdString)
err = execCommand(cmdString, origInput)
if err != nil {
// If input is incomplete, start multiline prompting
if syntax.IsIncomplete(err) {
@ -53,31 +53,31 @@ func runInput(input string) {
if err != nil {
break
}
err = execCommand(cmdString)
err = execCommand(cmdString, origInput)
if syntax.IsIncomplete(err) || strings.HasSuffix(input, "\\") {
continue
} else if code, ok := interp.IsExitStatus(err); ok {
cmdFinish(code, cmdString)
cmdFinish(code, cmdString, origInput)
} else if err != nil {
fmt.Fprintln(os.Stderr, err)
cmdFinish(1, cmdString)
cmdFinish(1, cmdString, origInput)
}
break
}
} else {
if code, ok := interp.IsExitStatus(err); ok {
cmdFinish(code, cmdString)
cmdFinish(code, cmdString, origInput)
} else {
fmt.Fprintln(os.Stderr, err)
}
}
} else {
cmdFinish(0, cmdString)
cmdFinish(0, cmdString, origInput)
}
}
// Run command in sh interpreter
func execCommand(cmd string) error {
func execCommand(cmd, old string) error {
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
if err != nil {
return err
@ -127,7 +127,7 @@ func execCommand(cmd string) error {
exitcode = uint8(code)
}
cmdFinish(exitcode, argstring)
cmdFinish(exitcode, argstring, old)
return interp.NewExitStatus(exitcode)
}
@ -237,6 +237,10 @@ func splitInput(input string) ([]string, string) {
return cmdArgs, cmdstr.String()
}
func cmdFinish(code uint8, cmdstr string) {
func cmdFinish(code uint8, cmdstr, oldInput string) {
// if input has space at the beginning, dont put in history
if !strings.HasPrefix(oldInput, " ") || interactive {
handleHistory(cmdstr)
}
hooks.Em.Emit("command.exit", code, cmdstr)
}

13
main.go
View File

@ -139,12 +139,13 @@ func main() {
if fileInfo, _ := os.Stdin.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 {
scanner := bufio.NewScanner(bufio.NewReader(os.Stdin))
for scanner.Scan() {
runInput(scanner.Text())
text := scanner.Text()
runInput(text, text)
}
}
if *cmdflag != "" {
runInput(*cmdflag)
runInput(*cmdflag, *cmdflag)
}
if getopt.NArgs() > 0 {
@ -188,7 +189,7 @@ input:
if strings.HasSuffix(input, "\\") {
for {
input, err = continuePrompt(strings.TrimSuffix(input, "\\"))
input, err = continuePrompt(input)
if err != nil {
goto input // continue inside nested loop
}
@ -198,11 +199,7 @@ input:
}
}
// if input has space at the beginning, dont put in history
if !strings.HasPrefix(oldInput, " ") {
handleHistory(input)
}
runInput(input)
runInput(input, oldInput)
termwidth, _, err := term.GetSize(0)
if err != nil {