diff --git a/Terminal.Gui/Windows/DefaultFileOperations.cs b/Terminal.Gui/Windows/DefaultFileOperations.cs index fab98cec9..3d3505f8c 100644 --- a/Terminal.Gui/Windows/DefaultFileOperations.cs +++ b/Terminal.Gui/Windows/DefaultFileOperations.cs @@ -85,12 +85,12 @@ namespace Terminal.Gui { } /// - public bool Rename (FileSystemInfo toRename) + public FileSystemInfo Rename (FileSystemInfo toRename) { // Dont allow renaming C: or D: or / (on linux) etc if(toRename is DirectoryInfo dir && dir.Parent == null) { - return false; + return null; } if(Prompt("Rename",toRename.Name,out var newName)) @@ -101,39 +101,43 @@ namespace Terminal.Gui { if (toRename is FileInfo f) { - f.MoveTo(Path.Combine(f.Directory.FullName,newName)); + var newLocation = new FileInfo(Path.Combine(f.Directory.FullName,newName)); + f.MoveTo(newLocation.FullName); + return newLocation; } else { var d = ((DirectoryInfo)toRename); - d.MoveTo(Path.Combine(d.Parent.FullName,newName)); - } - return true; + var newLocation = new DirectoryInfo(Path.Combine(d.Parent.FullName,newName)); + d.MoveTo(newLocation.FullName); + return newLocation; + } } catch (Exception ex) { MessageBox.ErrorQuery ("Rename Failed", ex.Message, "Ok"); } } } - return false; + return null; } /// - public bool New (DirectoryInfo inDirectory) + public FileSystemInfo New (DirectoryInfo inDirectory) { if(Prompt("New Folder","",out var named)) { if(!string.IsNullOrWhiteSpace(named)) { try { - Directory.CreateDirectory(Path.Combine(inDirectory.FullName,named)); - return true; + var newDir = new DirectoryInfo(Path.Combine(inDirectory.FullName,named)); + newDir.Create(); + return newDir; } catch (Exception ex) { MessageBox.ErrorQuery ("Rename Failed", ex.Message, "Ok"); } } } - return false; + return null; } } } \ No newline at end of file diff --git a/Terminal.Gui/Windows/FileDialog.cs b/Terminal.Gui/Windows/FileDialog.cs index e2740ae9c..a42527129 100644 --- a/Terminal.Gui/Windows/FileDialog.cs +++ b/Terminal.Gui/Windows/FileDialog.cs @@ -489,17 +489,25 @@ namespace Terminal.Gui { if(toRename?.Length == 1) { - if(FileOperationsHandler.Rename (toRename.Single())) + var newNamed = FileOperationsHandler.Rename (toRename.Single()); + + if(newNamed != null) { RefreshState(); + RestoreSelection(newNamed); } } } private void New() { - if(state != null && FileOperationsHandler.New (state.Directory)) + if(state != null) { - RefreshState(); + var created = FileOperationsHandler.New (state.Directory); + if(created != null) + { + RefreshState(); + RestoreSelection(created); + } } } private FileSystemInfo[] GetFocusedFiles() @@ -1389,10 +1397,10 @@ namespace Terminal.Gui { { return this.state?.Children [(int)this.tableView.Table.Rows [rowIndex] [0]]; } - private int? StatsToRow (FileSystemInfoStats stats) + private int? StatsToRow (FileSystemInfo fileSystemInfo) { // find array index of the current state for the stats - var idx = state?.Children.IndexOf ((f) => f.FileSystemInfo.FullName == stats.FileSystemInfo.FullName); + var idx = state?.Children.IndexOf ((f) => f.FileSystemInfo.FullName == fileSystemInfo.FullName); if (idx != -1 && idx != null) { @@ -2056,7 +2064,7 @@ namespace Terminal.Gui { this.dlg.PushState (goTo, false, true, false); if (restoreSelection != null) { - this.dlg.RestoreSelection (restoreSelection); + this.dlg.RestoreSelection (restoreSelection.FileSystemInfo); } return true; @@ -2124,7 +2132,7 @@ namespace Terminal.Gui { } } - private void RestoreSelection (FileSystemInfoStats toRestore) + private void RestoreSelection (FileSystemInfo toRestore) { var toReselect = StatsToRow (toRestore); diff --git a/Terminal.Gui/Windows/IFileOperations.cs b/Terminal.Gui/Windows/IFileOperations.cs index b625d940b..5e102ad32 100644 --- a/Terminal.Gui/Windows/IFileOperations.cs +++ b/Terminal.Gui/Windows/IFileOperations.cs @@ -25,11 +25,10 @@ namespace Terminal.Gui { /// in . /// /// - /// if operation was completed or - /// if cancelled + /// The new name for the file or null if cancelled /// Ensure you use a try/catch block with appropriate /// error handling (e.g. showing a - bool Rename(FileSystemInfo toRename); + FileSystemInfo Rename(FileSystemInfo toRename); /// @@ -38,10 +37,9 @@ namespace Terminal.Gui { /// /// The parent directory in which the new /// directory should be created - /// if operation was completed or - /// if cancelled + /// The newly created directory or null if cancelled. /// Ensure you use a try/catch block with appropriate /// error handling (e.g. showing a - bool New(DirectoryInfo inDirectory); + FileSystemInfo New(DirectoryInfo inDirectory); } } \ No newline at end of file