diff --git a/src/command.go b/src/command.go index 1994e207..7acfcb57 100644 --- a/src/command.go +++ b/src/command.go @@ -39,11 +39,17 @@ func HandleCommand(input string, view *View) { case "replace": r := regexp.MustCompile(`"[^"\\]*(?:\\.[^"\\]*)*"|[^\s]*`) replaceCmd := r.FindAllString(strings.Join(args, " "), -1) - if len(replaceCmd) != 2 { + if len(replaceCmd) < 2 { messenger.Error("Invalid replace statement: " + strings.Join(args, " ")) return } + var flags string + if len(replaceCmd) == 3 { + // The user included some flags + flags = replaceCmd[2] + } + search := string(replaceCmd[0]) replace := string(replaceCmd[1]) @@ -57,7 +63,7 @@ func HandleCommand(input string, view *View) { search = strings.Replace(search, `\"`, `"`, -1) replace = strings.Replace(replace, `\"`, `"`, -1) - messenger.Error(search + " -> " + replace) + // messenger.Error(search + " -> " + replace) regex, err := regexp.Compile(search) if err != nil { @@ -65,13 +71,26 @@ func HandleCommand(input string, view *View) { return } + found := false for { match := regex.FindStringIndex(view.buf.text) if match == nil { break } + found = true + if strings.Contains(flags, "c") { + // // The 'check' flag was used + // if messenger.YesNoPrompt("Perform replacement?") { + // view.eh.Replace(match[0], match[1], replace) + // } else { + // continue + // } + } view.eh.Replace(match[0], match[1], replace) } + if !found { + messenger.Message("Nothing matched " + search) + } default: messenger.Error("Unknown command: " + cmd) } diff --git a/src/messenger.go b/src/messenger.go index ca80b1b4..e7984bf3 100644 --- a/src/messenger.go +++ b/src/messenger.go @@ -71,6 +71,29 @@ func (m *Messenger) Error(msg string) { m.hasMessage = true } +// YesNoPrompt asks the user a yes or no question (waits for y or n) and returns the result +func (m *Messenger) YesNoPrompt(prompt string) bool { + m.Message(prompt) + + for { + m.Clear() + m.Display() + screen.Show() + event := screen.PollEvent() + + switch e := event.(type) { + case *tcell.EventKey: + if e.Key() == tcell.KeyRune { + if e.Rune() == 'y' { + return true + } else if e.Rune() == 'n' { + return false + } + } + } + } +} + // Prompt sends the user a message and waits for a response to be typed in // This function blocks the main loop while waiting for input func (m *Messenger) Prompt(prompt string) (string, bool) {