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) - Home dir is now added to recent dirs (the case of cd with no arg)
- `index` subdoc will no longer appear - `index` subdoc will no longer appear
- Alias expansion with quotes - Alias expansion with quotes
- Add full command to history in the case of incomplete input
### Changed ### Changed
- The minimal config is truly minimal now - 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 { func hlrun(L *lua.LState) int {
var exitcode uint8 var exitcode uint8
cmd := L.CheckString(1) cmd := L.CheckString(1)
err := execCommand(cmd) err := execCommand(cmd, cmd)
if code, ok := interp.IsExitStatus(err); ok { if code, ok := interp.IsExitStatus(err); ok {
exitcode = code exitcode = code

26
exec.go
View File

@ -15,7 +15,7 @@ import (
"mvdan.cc/sh/v3/syntax" "mvdan.cc/sh/v3/syntax"
) )
func runInput(input string) { func runInput(input, origInput string) {
running = true running = true
cmdString := aliases.Resolve(input) cmdString := aliases.Resolve(input)
@ -39,12 +39,12 @@ func runInput(input string) {
err = l.PCall(0, lua.MultRet, nil) err = l.PCall(0, lua.MultRet, nil)
} }
if err == nil { if err == nil {
cmdFinish(0, cmdString) cmdFinish(0, cmdString, origInput)
return return
} }
// Last option: use sh interpreter // Last option: use sh interpreter
err = execCommand(cmdString) err = execCommand(cmdString, origInput)
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) {
@ -53,31 +53,31 @@ func runInput(input string) {
if err != nil { if err != nil {
break break
} }
err = execCommand(cmdString) err = execCommand(cmdString, origInput)
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) cmdFinish(code, cmdString, origInput)
} else if err != nil { } else if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
cmdFinish(1, cmdString) cmdFinish(1, cmdString, origInput)
} }
break break
} }
} else { } else {
if code, ok := interp.IsExitStatus(err); ok { if code, ok := interp.IsExitStatus(err); ok {
cmdFinish(code, cmdString) cmdFinish(code, cmdString, origInput)
} else { } else {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
} }
} }
} else { } else {
cmdFinish(0, cmdString) cmdFinish(0, cmdString, origInput)
} }
} }
// Run command in sh interpreter // Run command in sh interpreter
func execCommand(cmd string) error { func execCommand(cmd, old string) 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
@ -127,7 +127,7 @@ func execCommand(cmd string) error {
exitcode = uint8(code) exitcode = uint8(code)
} }
cmdFinish(exitcode, argstring) cmdFinish(exitcode, argstring, old)
return interp.NewExitStatus(exitcode) return interp.NewExitStatus(exitcode)
} }
@ -237,6 +237,10 @@ func splitInput(input string) ([]string, string) {
return cmdArgs, cmdstr.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) 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 { if fileInfo, _ := os.Stdin.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 {
scanner := bufio.NewScanner(bufio.NewReader(os.Stdin)) scanner := bufio.NewScanner(bufio.NewReader(os.Stdin))
for scanner.Scan() { for scanner.Scan() {
runInput(scanner.Text()) text := scanner.Text()
runInput(text, text)
} }
} }
if *cmdflag != "" { if *cmdflag != "" {
runInput(*cmdflag) runInput(*cmdflag, *cmdflag)
} }
if getopt.NArgs() > 0 { if getopt.NArgs() > 0 {
@ -188,7 +189,7 @@ input:
if strings.HasSuffix(input, "\\") { if strings.HasSuffix(input, "\\") {
for { for {
input, err = continuePrompt(strings.TrimSuffix(input, "\\")) input, err = continuePrompt(input)
if err != nil { if err != nil {
goto input // continue inside nested loop goto input // continue inside nested loop
} }
@ -198,11 +199,7 @@ input:
} }
} }
// if input has space at the beginning, dont put in history runInput(input, oldInput)
if !strings.HasPrefix(oldInput, " ") {
handleHistory(input)
}
runInput(input)
termwidth, _, err := term.GetSize(0) termwidth, _, err := term.GetSize(0)
if err != nil { if err != nil {