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.
This commit is contained in:
niten94 2025-04-18 19:21:27 +08:00
parent 0d5b2b73e3
commit c457ae421a
2 changed files with 17 additions and 23 deletions

View File

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

View File

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