fix: dont replace ^^ in commands before, or in quotes

pull/21/head
TorchedSammy 2021-03-28 18:15:51 -04:00
parent b735421af8
commit 479968f79e
1 changed files with 19 additions and 11 deletions

30
main.go
View File

@ -101,15 +101,7 @@ func main() {
continue continue
} }
// Set a variable to the command string before ^^ is cmdArgs, cmdString := splitInput(cmdString)
// replaced with the command before. This will be added
// to history, preventing ^^ from before being replaced.
pcmdString := cmdString
lastcmd := readline.GetHistory(readline.HistorySize() - 1)
cmdString = strings.Replace(cmdString, "^^", lastcmd, 1)
cmdArgs := splitInput(pcmdString)
if len(cmdArgs) == 0 { continue } if len(cmdArgs) == 0 { continue }
if aliases[cmdArgs[0]] != "" { if aliases[cmdArgs[0]] != "" {
@ -204,10 +196,16 @@ func StartMultiline(prev string, sb *strings.Builder) bool {
return true return true
} }
func splitInput(input string) []string { func splitInput(input string) ([]string, string) {
// end my suffering
// TODO: refactor this garbage
quoted := false quoted := false
startlastcmd := false
lastcmddone := false
cmdArgs := []string{} cmdArgs := []string{}
sb := &strings.Builder{} sb := &strings.Builder{}
cmdstr := &strings.Builder{}
lastcmd := readline.GetHistory(readline.HistorySize() - 1)
for _, r := range input { for _, r := range input {
if r == '"' { if r == '"' {
@ -219,16 +217,26 @@ func splitInput(input string) []string {
} else if !quoted && r == ' ' { } else if !quoted && r == ' ' {
cmdArgs = append(cmdArgs, sb.String()) cmdArgs = append(cmdArgs, sb.String())
sb.Reset() sb.Reset()
} else if !quoted && r == '^' && startlastcmd && !lastcmddone {
cmdstr.WriteString(lastcmd)
sb.WriteString(lastcmd)
startlastcmd = !startlastcmd
lastcmddone = !lastcmddone
continue
} else if !quoted && r == '^' && !lastcmddone {
startlastcmd = !startlastcmd
continue
} else { } else {
sb.WriteRune(r) sb.WriteRune(r)
} }
cmdstr.WriteRune(r)
} }
if sb.Len() > 0 { if sb.Len() > 0 {
cmdArgs = append(cmdArgs, sb.String()) cmdArgs = append(cmdArgs, sb.String())
} }
readline.AddHistory(input) readline.AddHistory(input)
return cmdArgs return cmdArgs, cmdstr.String()
} }
func execCommand(cmd string) error { func execCommand(cmd string) error {