diff --git a/internal/action/actions.go b/internal/action/actions.go index e1f7a1a6..0ed94558 100644 --- a/internal/action/actions.go +++ b/internal/action/actions.go @@ -1907,6 +1907,18 @@ func (h *BufPane) ForceQuit() bool { return true } +// closePrompt displays a prompt to save the buffer before closing it to proceed +// with a different action or command +func (h *BufPane) closePrompt(action string, callback func()) { + InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) { + if !canceled && !yes { + callback() + } else if !canceled && yes { + h.SaveCB(action, callback) + } + }) +} + // Quit this will close the current tab or view that is open func (h *BufPane) Quit() bool { if h.Buf.Modified() && !h.Buf.Shared() { @@ -1916,14 +1928,8 @@ func (h *BufPane) Quit() bool { h.ForceQuit() }) } else { - InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) { - if !canceled && !yes { - h.ForceQuit() - } else if !canceled && yes { - h.SaveCB("Quit", func() { - h.ForceQuit() - }) - } + h.closePrompt("Quit", func() { + h.ForceQuit() }) } } else { diff --git a/internal/action/command.go b/internal/action/command.go index 75516fd5..3b590559 100644 --- a/internal/action/command.go +++ b/internal/action/command.go @@ -309,14 +309,7 @@ func (h *BufPane) OpenCmd(args []string) { h.OpenBuffer(b) } if h.Buf.Modified() && !h.Buf.Shared() { - InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) { - if !canceled && !yes { - open() - } else if !canceled && yes { - h.Save() - open() - } - }) + h.closePrompt("Save", open) } else { open() } @@ -1122,13 +1115,8 @@ func (h *BufPane) TermCmd(args []string) { for i, p := range ps { if p.ID() == h.ID() { if h.Buf.Modified() && !h.Buf.Shared() { - InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) { - if !canceled && !yes { - term(i, false) - } else if !canceled && yes { - h.Save() - term(i, false) - } + h.closePrompt("Save", func() { + term(i, false) }) } else { term(i, false)