Skip to content

Commit da58578

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

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

internal/action/actions.go

Lines changed: 33 additions & 10 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,6 +1034,17 @@ 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)
@@ -1034,7 +1053,7 @@ func (h *BufPane) SaveAs() bool {
10341053
// This function saves the buffer to `filename` and changes the buffer's path and name
10351054
// to `filename` if the save is successful
10361055
// The callback is only called if the save was successful
1037-
func (h *BufPane) saveBufToFile(filename string, action string, callback func()) bool {
1056+
func (h *BufPane) saveBufToFile(filename string, action string, callback func(bool)) bool {
10381057
err := h.Buf.SaveAs(filename)
10391058
if err != nil {
10401059
if errors.Is(err, fs.ErrPermission) {
@@ -1050,7 +1069,7 @@ func (h *BufPane) saveBufToFile(filename string, action string, callback func())
10501069
} else {
10511070
InfoBar.Message("Saved " + filename)
10521071
if callback != nil {
1053-
callback()
1072+
callback(true)
10541073
}
10551074
}
10561075
}
@@ -1060,7 +1079,11 @@ func (h *BufPane) saveBufToFile(filename string, action string, callback func())
10601079
InfoBar.YNPrompt(
10611080
fmt.Sprintf("Permission denied. Do you want to save this file using %s? (y,n)", config.GlobalSettings["sucmd"].(string)),
10621081
func(yes, canceled bool) {
1063-
if yes && !canceled {
1082+
if !yes && !canceled {
1083+
if callback != nil {
1084+
callback(false)
1085+
}
1086+
} else if yes && !canceled {
10641087
saveWithSudo()
10651088
h.completeAction(action)
10661089
}
@@ -1074,7 +1097,7 @@ func (h *BufPane) saveBufToFile(filename string, action string, callback func())
10741097
} else {
10751098
InfoBar.Message("Saved " + filename)
10761099
if callback != nil {
1077-
callback()
1100+
callback(true)
10781101
}
10791102
}
10801103
return true

0 commit comments

Comments
 (0)