Skip to content

Commit c457ae4

Browse files
committed
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.
1 parent 0d5b2b7 commit c457ae4

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

internal/action/actions.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,6 +1907,18 @@ func (h *BufPane) ForceQuit() bool {
19071907
return true
19081908
}
19091909

1910+
// closePrompt displays a prompt to save the buffer before closing it to proceed
1911+
// with a different action or command
1912+
func (h *BufPane) closePrompt(action string, callback func()) {
1913+
InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) {
1914+
if !canceled && !yes {
1915+
callback()
1916+
} else if !canceled && yes {
1917+
h.SaveCB(action, callback)
1918+
}
1919+
})
1920+
}
1921+
19101922
// Quit this will close the current tab or view that is open
19111923
func (h *BufPane) Quit() bool {
19121924
if h.Buf.Modified() && !h.Buf.Shared() {
@@ -1916,14 +1928,8 @@ func (h *BufPane) Quit() bool {
19161928
h.ForceQuit()
19171929
})
19181930
} else {
1919-
InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) {
1920-
if !canceled && !yes {
1921-
h.ForceQuit()
1922-
} else if !canceled && yes {
1923-
h.SaveCB("Quit", func() {
1924-
h.ForceQuit()
1925-
})
1926-
}
1931+
h.closePrompt("Quit", func() {
1932+
h.ForceQuit()
19271933
})
19281934
}
19291935
} else {

internal/action/command.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,7 @@ func (h *BufPane) OpenCmd(args []string) {
309309
h.OpenBuffer(b)
310310
}
311311
if h.Buf.Modified() && !h.Buf.Shared() {
312-
InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) {
313-
if !canceled && !yes {
314-
open()
315-
} else if !canceled && yes {
316-
h.Save()
317-
open()
318-
}
319-
})
312+
h.closePrompt("Save", open)
320313
} else {
321314
open()
322315
}
@@ -1122,13 +1115,8 @@ func (h *BufPane) TermCmd(args []string) {
11221115
for i, p := range ps {
11231116
if p.ID() == h.ID() {
11241117
if h.Buf.Modified() && !h.Buf.Shared() {
1125-
InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) {
1126-
if !canceled && !yes {
1127-
term(i, false)
1128-
} else if !canceled && yes {
1129-
h.Save()
1130-
term(i, false)
1131-
}
1118+
h.closePrompt("Save", func() {
1119+
term(i, false)
11321120
})
11331121
} else {
11341122
term(i, false)

0 commit comments

Comments
 (0)