Add flags and yes no prompt

This commit is contained in:
Zachary Yedidia
2016-04-03 17:48:21 -04:00
parent 43187c0438
commit 0be8e9be44
2 changed files with 44 additions and 2 deletions

View File

@@ -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)
}

View File

@@ -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) {