mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-29 22:27:13 +09:00
Add flags and yes no prompt
This commit is contained in:
@@ -39,11 +39,17 @@ func HandleCommand(input string, view *View) {
|
|||||||
case "replace":
|
case "replace":
|
||||||
r := regexp.MustCompile(`"[^"\\]*(?:\\.[^"\\]*)*"|[^\s]*`)
|
r := regexp.MustCompile(`"[^"\\]*(?:\\.[^"\\]*)*"|[^\s]*`)
|
||||||
replaceCmd := r.FindAllString(strings.Join(args, " "), -1)
|
replaceCmd := r.FindAllString(strings.Join(args, " "), -1)
|
||||||
if len(replaceCmd) != 2 {
|
if len(replaceCmd) < 2 {
|
||||||
messenger.Error("Invalid replace statement: " + strings.Join(args, " "))
|
messenger.Error("Invalid replace statement: " + strings.Join(args, " "))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var flags string
|
||||||
|
if len(replaceCmd) == 3 {
|
||||||
|
// The user included some flags
|
||||||
|
flags = replaceCmd[2]
|
||||||
|
}
|
||||||
|
|
||||||
search := string(replaceCmd[0])
|
search := string(replaceCmd[0])
|
||||||
replace := string(replaceCmd[1])
|
replace := string(replaceCmd[1])
|
||||||
|
|
||||||
@@ -57,7 +63,7 @@ func HandleCommand(input string, view *View) {
|
|||||||
search = strings.Replace(search, `\"`, `"`, -1)
|
search = strings.Replace(search, `\"`, `"`, -1)
|
||||||
replace = strings.Replace(replace, `\"`, `"`, -1)
|
replace = strings.Replace(replace, `\"`, `"`, -1)
|
||||||
|
|
||||||
messenger.Error(search + " -> " + replace)
|
// messenger.Error(search + " -> " + replace)
|
||||||
|
|
||||||
regex, err := regexp.Compile(search)
|
regex, err := regexp.Compile(search)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -65,13 +71,26 @@ func HandleCommand(input string, view *View) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
found := false
|
||||||
for {
|
for {
|
||||||
match := regex.FindStringIndex(view.buf.text)
|
match := regex.FindStringIndex(view.buf.text)
|
||||||
if match == nil {
|
if match == nil {
|
||||||
break
|
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)
|
view.eh.Replace(match[0], match[1], replace)
|
||||||
}
|
}
|
||||||
|
if !found {
|
||||||
|
messenger.Message("Nothing matched " + search)
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
messenger.Error("Unknown command: " + cmd)
|
messenger.Error("Unknown command: " + cmd)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,6 +71,29 @@ func (m *Messenger) Error(msg string) {
|
|||||||
m.hasMessage = true
|
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
|
// 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
|
// This function blocks the main loop while waiting for input
|
||||||
func (m *Messenger) Prompt(prompt string) (string, bool) {
|
func (m *Messenger) Prompt(prompt string) (string, bool) {
|
||||||
|
|||||||
Reference in New Issue
Block a user