Skip to content

Commit

Permalink
Merge pull request #526 from GRFreire/rename-warn-before-override
Browse files Browse the repository at this point in the history
Warn overwrite when renaming
  • Loading branch information
yorukot authored Dec 29, 2024
2 parents 6a644c1 + 76eb309 commit cbf1fc3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 23 deletions.
33 changes: 33 additions & 0 deletions src/internal/handle_file_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,39 @@ func (m *model) panelCreateNewFile() {

}

func (m *model) IsRenamingConflicting() bool {
panel := m.fileModel.filePanels[m.filePanelFocusIndex]
oldPath := panel.element[panel.cursor].location
newPath := panel.location + "/" + panel.rename.Value()

if oldPath == newPath {
return false
}

_, err := os.Stat(newPath)
if err == nil {
return true
}

return false
}

func (m *model) warnModalForRenaming() {
id := shortuuid.New()
message := channelMessage{
messageId: id,
messageType: sendWarnModal,
}

message.warnModal = warnModal{
open: true,
title: "There is already a file or directory with that name",
content: "This operation will override the existing file",
warnType: confirmRenameItem,
}
channel <- message
}

// Rename file where the cusror is located
func (m *model) panelItemRename() {
panel := m.fileModel.filePanels[m.filePanelFocusIndex]
Expand Down
58 changes: 35 additions & 23 deletions src/internal/key_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,32 +205,40 @@ func (m *model) warnModalOpenKey(msg string) {
switch msg {
case containsKey(msg, hotkeys.Quit), containsKey(msg, hotkeys.CancelTyping):
m.cancelWarnModal()
if m.warnModal.warnType == confirmRenameItem {
m.cancelRename()
}
case containsKey(msg, hotkeys.Confirm):
m.warnModal.open = false
panel := m.fileModel.filePanels[m.filePanelFocusIndex]
if m.fileModel.filePanels[m.filePanelFocusIndex].panelMode == selectMode {
if isExternalDiskPath(panel.location) {
go func() {
m.completelyDeleteMultipleItems()
m.fileModel.filePanels[m.filePanelFocusIndex].selected = m.fileModel.filePanels[m.filePanelFocusIndex].selected[:0]
}()
} else {
go func() {
m.deleteMultipleItems()
m.fileModel.filePanels[m.filePanelFocusIndex].selected = m.fileModel.filePanels[m.filePanelFocusIndex].selected[:0]
}()
}
} else {
if isExternalDiskPath(panel.location) {
go func() {
m.completelyDeleteSingleItem()
}()
switch m.warnModal.warnType {
case confirmDeleteItem:
panel := m.fileModel.filePanels[m.filePanelFocusIndex]
if m.fileModel.filePanels[m.filePanelFocusIndex].panelMode == selectMode {
if isExternalDiskPath(panel.location) {
go func() {
m.completelyDeleteMultipleItems()
m.fileModel.filePanels[m.filePanelFocusIndex].selected = m.fileModel.filePanels[m.filePanelFocusIndex].selected[:0]
}()
} else {
go func() {
m.deleteMultipleItems()
m.fileModel.filePanels[m.filePanelFocusIndex].selected = m.fileModel.filePanels[m.filePanelFocusIndex].selected[:0]
}()
}
} else {
go func() {
m.deleteSingleItem()
}()
}
if isExternalDiskPath(panel.location) {
go func() {
m.completelyDeleteSingleItem()
}()
} else {
go func() {
m.deleteSingleItem()
}()
}

}
case confirmRenameItem:
m.confirmRename()
}
}
}
Expand Down Expand Up @@ -272,7 +280,11 @@ func (m *model) renamingKey(msg string) {
case containsKey(msg, hotkeys.CancelTyping):
m.cancelRename()
case containsKey(msg, hotkeys.ConfirmTyping):
m.confirmRename()
if m.IsRenamingConflicting() {
m.warnModalForRenaming()
} else {
m.confirmRename()
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/internal/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (

const (
confirmDeleteItem warnType = iota
confirmRenameItem
)

// Constants for panel with no focus
Expand Down

0 comments on commit cbf1fc3

Please sign in to comment.