Skip to content

Commit 5de12c4

Browse files
committed
actions: Execute possible callbacks on Y/N prompts
...and keep backward compatibility with `SaveAsCB()` where the callback shall be performed only in case the save was successful.
1 parent 115e560 commit 5de12c4

1 file changed

Lines changed: 39 additions & 13 deletions

File tree

internal/action/actions.go

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -961,13 +961,19 @@ func (h *BufPane) SaveAll() bool {
961961
return true
962962
}
963963

964-
// SaveCB performs a save and does a callback at the very end (after all prompts have been resolved)
964+
// SaveCB performs a save and does a callback at the very end
965+
// after all prompts have been resolved with yes or no and no error occured.
965966
func (h *BufPane) SaveCB(action string, callback func()) bool {
967+
cb := func(bool) {
968+
if callback != nil {
969+
callback()
970+
}
971+
}
966972
// If this is an empty buffer, ask for a filename
967973
if h.Buf.Path == "" {
968-
h.SaveAsCB(action, callback)
974+
h.saveAsCB(action, cb)
969975
} else {
970-
noPrompt := h.saveBufToFile(h.Buf.Path, action, callback)
976+
noPrompt := h.saveBufToFile(h.Buf.Path, action, cb)
971977
if noPrompt {
972978
return true
973979
}
@@ -980,9 +986,9 @@ func (h *BufPane) Save() bool {
980986
return h.SaveCB("Save", nil)
981987
}
982988

983-
// SaveAsCB performs a save as and does a callback at the very end (after all prompts have been resolved)
984-
// The callback is only called if the save was successful
985-
func (h *BufPane) SaveAsCB(action string, callback func()) bool {
989+
// saveAsCB performs a save as and does a callback at the very end
990+
// after all prompts have been resolved with yes or no and no error occured.
991+
func (h *BufPane) saveAsCB(action string, callback func(bool)) bool {
986992
InfoBar.Prompt("Filename: ", "", "Save", nil, func(resp string, canceled bool) {
987993
if !canceled {
988994
// the filename might or might not be quoted, so unquote first then join the strings.
@@ -1012,7 +1018,11 @@ func (h *BufPane) SaveAsCB(action string, callback func()) bool {
10121018
InfoBar.YNPrompt(
10131019
fmt.Sprintf("The file %s already exists in the directory, would you like to overwrite? Y/n", fileinfo.Name()),
10141020
func(yes, canceled bool) {
1015-
if yes && !canceled {
1021+
if !yes && !canceled {
1022+
if callback != nil {
1023+
callback(false)
1024+
}
1025+
} else if yes && !canceled {
10161026
noPrompt := h.saveBufToFile(filename, action, callback)
10171027
if noPrompt {
10181028
h.completeAction(action)
@@ -1026,15 +1036,27 @@ func (h *BufPane) SaveAsCB(action string, callback func()) bool {
10261036
return false
10271037
}
10281038

1039+
// SaveAsCB is the same as saveAsCB,
1040+
// but the callback is only called if the save was successful
1041+
func (h *BufPane) SaveAsCB(action string, callback func()) bool {
1042+
cb := func(success bool) {
1043+
if callback != nil && success {
1044+
callback()
1045+
}
1046+
}
1047+
return h.saveAsCB(action, cb)
1048+
}
1049+
10291050
// SaveAs saves the buffer to disk with the given name
10301051
func (h *BufPane) SaveAs() bool {
10311052
return h.SaveAsCB("SaveAs", nil)
10321053
}
10331054

10341055
// This function saves the buffer to `filename` and changes the buffer's path and name
1035-
// to `filename` if the save is successful
1036-
// The callback is only called if the save was successful
1037-
func (h *BufPane) saveBufToFile(filename string, action string, callback func()) bool {
1056+
// to `filename` if the save is successful.
1057+
// The callback is called after all prompts have been resolved with yes or no
1058+
// and no error occured.
1059+
func (h *BufPane) saveBufToFile(filename string, action string, callback func(bool)) bool {
10381060
err := h.Buf.SaveAs(filename)
10391061
if err != nil {
10401062
if errors.Is(err, fs.ErrPermission) {
@@ -1050,7 +1072,7 @@ func (h *BufPane) saveBufToFile(filename string, action string, callback func())
10501072
} else {
10511073
InfoBar.Message("Saved " + filename)
10521074
if callback != nil {
1053-
callback()
1075+
callback(true)
10541076
}
10551077
}
10561078
}
@@ -1060,7 +1082,11 @@ func (h *BufPane) saveBufToFile(filename string, action string, callback func())
10601082
InfoBar.YNPrompt(
10611083
fmt.Sprintf("Permission denied. Do you want to save this file using %s? (y,n)", config.GlobalSettings["sucmd"].(string)),
10621084
func(yes, canceled bool) {
1063-
if yes && !canceled {
1085+
if !yes && !canceled {
1086+
if callback != nil {
1087+
callback(false)
1088+
}
1089+
} else if yes && !canceled {
10641090
saveWithSudo()
10651091
h.completeAction(action)
10661092
}
@@ -1074,7 +1100,7 @@ func (h *BufPane) saveBufToFile(filename string, action string, callback func())
10741100
} else {
10751101
InfoBar.Message("Saved " + filename)
10761102
if callback != nil {
1077-
callback()
1103+
callback(true)
10781104
}
10791105
}
10801106
return true

0 commit comments

Comments
 (0)