Have CanClose use single letters for responses

Closes #184
This commit is contained in:
Zachary Yedidia
2016-08-29 10:10:16 -04:00
parent eda08a994a
commit fccec47ae5
3 changed files with 36 additions and 7 deletions

View File

@@ -989,7 +989,7 @@ func (v *View) OpenFile(usePlugin bool) bool {
return false
}
if v.CanClose("Continue? (yes, no, save) ") {
if v.CanClose("Continue? (y,n,s) ", 'y', 'n', 's') {
filename, canceled := messenger.Prompt("File to open: ", "Open", FileCompletion)
if canceled {
return false
@@ -1285,7 +1285,7 @@ func (v *View) Quit(usePlugin bool) bool {
}
// Make sure not to quit if there are unsaved changes
if v.CanClose("Quit anyway? (yes, no, save) ") {
if v.CanClose("Quit anyway? (y,n,s) ", 'y', 'n', 's') {
v.CloseBuffer()
if len(tabs[curTab].views) > 1 {
v.splitNode.Delete()

View File

@@ -124,6 +124,35 @@ func (m *Messenger) YesNoPrompt(prompt string) (bool, bool) {
}
}
// LetterPrompt gives the user a prompt and waits for a one letter response
func (m *Messenger) LetterPrompt(prompt string, responses ...rune) (rune, bool) {
m.Message(prompt)
_, h := screen.Size()
for {
m.Clear()
m.Display()
screen.ShowCursor(Count(m.message), h-1)
screen.Show()
event := <-events
switch e := event.(type) {
case *tcell.EventKey:
switch e.Key() {
case tcell.KeyRune:
for _, r := range responses {
if e.Rune() == r {
m.Reset()
return r, false
}
}
case tcell.KeyCtrlC, tcell.KeyCtrlQ, tcell.KeyEscape:
return ' ', true
}
}
}
}
type Completion int
const (

View File

@@ -169,13 +169,13 @@ func (v *View) ScrollDown(n int) {
// If there are unsaved changes, the user will be asked if the view can be closed
// causing them to lose the unsaved changes
// The message is what to print after saying "You have unsaved changes. "
func (v *View) CanClose(msg string) bool {
func (v *View) CanClose(msg string, responses ...rune) bool {
if v.Buf.IsModified {
quit, canceled := messenger.Prompt("You have unsaved changes. "+msg, "Unsaved", NoCompletion)
char, canceled := messenger.LetterPrompt("You have unsaved changes. "+msg, responses...)
if !canceled {
if strings.ToLower(quit) == "yes" || strings.ToLower(quit) == "y" {
if char == 'y' {
return true
} else if strings.ToLower(quit) == "save" || strings.ToLower(quit) == "s" {
} else if char == 's' {
v.Save(true)
return true
}
@@ -217,7 +217,7 @@ func (v *View) CloseBuffer() {
// ReOpen reloads the current buffer
func (v *View) ReOpen() {
if v.CanClose("Continue? (yes, no, save) ") {
if v.CanClose("Continue? (y,n,s) ", 'y', 'n', 's') {
screen.Clear()
v.Buf.ReOpen()
v.Relocate()