Skip to content

Commit a90b1c9

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 a90b1c9

1 file changed

Lines changed: 33 additions & 11 deletions

File tree

internal/action/actions.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -963,11 +963,16 @@ func (h *BufPane) SaveAll() bool {
963963

964964
// SaveCB performs a save and does a callback at the very end (after all prompts have been resolved)
965965
func (h *BufPane) SaveCB(action string, callback func()) bool {
966+
cb := func(bool) {
967+
if callback != nil {
968+
callback()
969+
}
970+
}
966971
// If this is an empty buffer, ask for a filename
967972
if h.Buf.Path == "" {
968-
h.SaveAsCB(action, callback)
973+
h.saveAsCB(action, cb)
969974
} else {
970-
noPrompt := h.saveBufToFile(h.Buf.Path, action, callback)
975+
noPrompt := h.saveBufToFile(h.Buf.Path, action, cb)
971976
if noPrompt {
972977
return true
973978
}
@@ -980,9 +985,8 @@ func (h *BufPane) Save() bool {
980985
return h.SaveCB("Save", nil)
981986
}
982987

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 {
988+
// saveAsCB performs a save as and does a callback at the very end (after all prompts have been resolved)
989+
func (h *BufPane) saveAsCB(action string, callback func(bool)) bool {
986990
InfoBar.Prompt("Filename: ", "", "Save", nil, func(resp string, canceled bool) {
987991
if !canceled {
988992
// the filename might or might not be quoted, so unquote first then join the strings.
@@ -1012,7 +1016,11 @@ func (h *BufPane) SaveAsCB(action string, callback func()) bool {
10121016
InfoBar.YNPrompt(
10131017
fmt.Sprintf("The file %s already exists in the directory, would you like to overwrite? Y/n", fileinfo.Name()),
10141018
func(yes, canceled bool) {
1015-
if yes && !canceled {
1019+
if !yes && !canceled {
1020+
if callback != nil {
1021+
callback(false)
1022+
}
1023+
} else if yes && !canceled {
10161024
noPrompt := h.saveBufToFile(filename, action, callback)
10171025
if noPrompt {
10181026
h.completeAction(action)
@@ -1026,15 +1034,25 @@ func (h *BufPane) SaveAsCB(action string, callback func()) bool {
10261034
return false
10271035
}
10281036

1037+
// SaveAsCB is the same as saveAsCB,
1038+
// but the callback is only called if the save was successful
1039+
func (h *BufPane) SaveAsCB(action string, callback func()) bool {
1040+
cb := func(success bool) {
1041+
if callback != nil && success {
1042+
callback()
1043+
}
1044+
}
1045+
return h.saveAsCB(action, cb)
1046+
}
1047+
10291048
// SaveAs saves the buffer to disk with the given name
10301049
func (h *BufPane) SaveAs() bool {
10311050
return h.SaveAsCB("SaveAs", nil)
10321051
}
10331052

10341053
// This function saves the buffer to `filename` and changes the buffer's path and name
10351054
// 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 {
1055+
func (h *BufPane) saveBufToFile(filename string, action string, callback func(bool)) bool {
10381056
err := h.Buf.SaveAs(filename)
10391057
if err != nil {
10401058
if errors.Is(err, fs.ErrPermission) {
@@ -1050,7 +1068,7 @@ func (h *BufPane) saveBufToFile(filename string, action string, callback func())
10501068
} else {
10511069
InfoBar.Message("Saved " + filename)
10521070
if callback != nil {
1053-
callback()
1071+
callback(true)
10541072
}
10551073
}
10561074
}
@@ -1060,7 +1078,11 @@ func (h *BufPane) saveBufToFile(filename string, action string, callback func())
10601078
InfoBar.YNPrompt(
10611079
fmt.Sprintf("Permission denied. Do you want to save this file using %s? (y,n)", config.GlobalSettings["sucmd"].(string)),
10621080
func(yes, canceled bool) {
1063-
if yes && !canceled {
1081+
if !yes && !canceled {
1082+
if callback != nil {
1083+
callback(false)
1084+
}
1085+
} else if yes && !canceled {
10641086
saveWithSudo()
10651087
h.completeAction(action)
10661088
}
@@ -1074,7 +1096,7 @@ func (h *BufPane) saveBufToFile(filename string, action string, callback func())
10741096
} else {
10751097
InfoBar.Message("Saved " + filename)
10761098
if callback != nil {
1077-
callback()
1099+
callback(true)
10781100
}
10791101
}
10801102
return true

0 commit comments

Comments
 (0)