Skip to content

Commit cbf1fc3

Browse files
authored
Merge pull request #526 from GRFreire/rename-warn-before-override
Warn overwrite when renaming
2 parents 6a644c1 + 76eb309 commit cbf1fc3

File tree

3 files changed

+69
-23
lines changed

3 files changed

+69
-23
lines changed

src/internal/handle_file_operations.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,39 @@ func (m *model) panelCreateNewFile() {
3939

4040
}
4141

42+
func (m *model) IsRenamingConflicting() bool {
43+
panel := m.fileModel.filePanels[m.filePanelFocusIndex]
44+
oldPath := panel.element[panel.cursor].location
45+
newPath := panel.location + "/" + panel.rename.Value()
46+
47+
if oldPath == newPath {
48+
return false
49+
}
50+
51+
_, err := os.Stat(newPath)
52+
if err == nil {
53+
return true
54+
}
55+
56+
return false
57+
}
58+
59+
func (m *model) warnModalForRenaming() {
60+
id := shortuuid.New()
61+
message := channelMessage{
62+
messageId: id,
63+
messageType: sendWarnModal,
64+
}
65+
66+
message.warnModal = warnModal{
67+
open: true,
68+
title: "There is already a file or directory with that name",
69+
content: "This operation will override the existing file",
70+
warnType: confirmRenameItem,
71+
}
72+
channel <- message
73+
}
74+
4275
// Rename file where the cusror is located
4376
func (m *model) panelItemRename() {
4477
panel := m.fileModel.filePanels[m.filePanelFocusIndex]

src/internal/key_function.go

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -205,32 +205,40 @@ func (m *model) warnModalOpenKey(msg string) {
205205
switch msg {
206206
case containsKey(msg, hotkeys.Quit), containsKey(msg, hotkeys.CancelTyping):
207207
m.cancelWarnModal()
208+
if m.warnModal.warnType == confirmRenameItem {
209+
m.cancelRename()
210+
}
208211
case containsKey(msg, hotkeys.Confirm):
209212
m.warnModal.open = false
210-
panel := m.fileModel.filePanels[m.filePanelFocusIndex]
211-
if m.fileModel.filePanels[m.filePanelFocusIndex].panelMode == selectMode {
212-
if isExternalDiskPath(panel.location) {
213-
go func() {
214-
m.completelyDeleteMultipleItems()
215-
m.fileModel.filePanels[m.filePanelFocusIndex].selected = m.fileModel.filePanels[m.filePanelFocusIndex].selected[:0]
216-
}()
217-
} else {
218-
go func() {
219-
m.deleteMultipleItems()
220-
m.fileModel.filePanels[m.filePanelFocusIndex].selected = m.fileModel.filePanels[m.filePanelFocusIndex].selected[:0]
221-
}()
222-
}
223-
} else {
224-
if isExternalDiskPath(panel.location) {
225-
go func() {
226-
m.completelyDeleteSingleItem()
227-
}()
213+
switch m.warnModal.warnType {
214+
case confirmDeleteItem:
215+
panel := m.fileModel.filePanels[m.filePanelFocusIndex]
216+
if m.fileModel.filePanels[m.filePanelFocusIndex].panelMode == selectMode {
217+
if isExternalDiskPath(panel.location) {
218+
go func() {
219+
m.completelyDeleteMultipleItems()
220+
m.fileModel.filePanels[m.filePanelFocusIndex].selected = m.fileModel.filePanels[m.filePanelFocusIndex].selected[:0]
221+
}()
222+
} else {
223+
go func() {
224+
m.deleteMultipleItems()
225+
m.fileModel.filePanels[m.filePanelFocusIndex].selected = m.fileModel.filePanels[m.filePanelFocusIndex].selected[:0]
226+
}()
227+
}
228228
} else {
229-
go func() {
230-
m.deleteSingleItem()
231-
}()
232-
}
229+
if isExternalDiskPath(panel.location) {
230+
go func() {
231+
m.completelyDeleteSingleItem()
232+
}()
233+
} else {
234+
go func() {
235+
m.deleteSingleItem()
236+
}()
237+
}
233238

239+
}
240+
case confirmRenameItem:
241+
m.confirmRename()
234242
}
235243
}
236244
}
@@ -272,7 +280,11 @@ func (m *model) renamingKey(msg string) {
272280
case containsKey(msg, hotkeys.CancelTyping):
273281
m.cancelRename()
274282
case containsKey(msg, hotkeys.ConfirmTyping):
275-
m.confirmRename()
283+
if m.IsRenamingConflicting() {
284+
m.warnModalForRenaming()
285+
} else {
286+
m.confirmRename()
287+
}
276288
}
277289
}
278290

src/internal/type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const (
3333

3434
const (
3535
confirmDeleteItem warnType = iota
36+
confirmRenameItem
3637
)
3738

3839
// Constants for panel with no focus

0 commit comments

Comments
 (0)