From c457ae421a084c955558da818332561725f22a3f Mon Sep 17 00:00:00 2001 From: niten94 <127052329+niten94@users.noreply.github.com> Date: Fri, 18 Apr 2025 19:21:27 +0800 Subject: [PATCH] Generalize save prompt on close code into method This slightly changes the open and term command to be similar with the Quit action, where the buffer or pane is replaced after the prompts are completed if "n" wasn't pressed after the 1st prompt. --- internal/action/actions.go | 22 ++++++++++++++-------- internal/action/command.go | 18 +++--------------- 2 files changed, 17 insertions(+), 23 deletions(-) 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)