Add F2 rename flow and improve modal key hints
This commit is contained in:
parent
124e7ef972
commit
e7b31a8d5c
4 changed files with 192 additions and 12 deletions
|
|
@ -270,6 +270,31 @@ func MakeDir(parent string, name string) (string, error) {
|
|||
return target, nil
|
||||
}
|
||||
|
||||
func RenamePath(sourcePath string, newName string) (string, error) {
|
||||
newName = filepath.Base(filepath.Clean(newName))
|
||||
if newName == "." || newName == "" {
|
||||
return "", fmt.Errorf("invalid target name")
|
||||
}
|
||||
|
||||
targetPath := filepath.Join(filepath.Dir(sourcePath), newName)
|
||||
if same, err := samePath(sourcePath, targetPath); err != nil {
|
||||
return "", err
|
||||
} else if same {
|
||||
return "", fmt.Errorf("source and target are the same: %s", targetPath)
|
||||
}
|
||||
|
||||
if exists, err := PathExists(targetPath); err != nil {
|
||||
return "", err
|
||||
} else if exists {
|
||||
return "", ErrOverwrite(targetPath)
|
||||
}
|
||||
|
||||
if err := os.Rename(sourcePath, targetPath); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return targetPath, nil
|
||||
}
|
||||
|
||||
func copyDir(srcDir string, dstDir string, tracker *copyProgressState) error {
|
||||
if tracker != nil && tracker.ctx != nil {
|
||||
if err := tracker.ctx.Err(); err != nil {
|
||||
|
|
|
|||
|
|
@ -86,3 +86,50 @@ func TestMovePathWithProgressContextCancelledBeforeStartKeepsSource(t *testing.T
|
|||
t.Fatalf("expected destination file to be absent, stat err=%v", statErr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenamePath(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
root := t.TempDir()
|
||||
source := filepath.Join(root, "old.txt")
|
||||
if err := os.WriteFile(source, []byte("payload"), 0o644); err != nil {
|
||||
t.Fatalf("write source: %v", err)
|
||||
}
|
||||
|
||||
target, err := RenamePath(source, "new.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("rename: %v", err)
|
||||
}
|
||||
|
||||
if filepath.Base(target) != "new.txt" {
|
||||
t.Fatalf("unexpected target path: %s", target)
|
||||
}
|
||||
if _, statErr := os.Stat(target); statErr != nil {
|
||||
t.Fatalf("expected renamed file to exist, stat err=%v", statErr)
|
||||
}
|
||||
if _, statErr := os.Stat(source); !errors.Is(statErr, os.ErrNotExist) {
|
||||
t.Fatalf("expected source to be absent, stat err=%v", statErr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenamePathRejectsExistingTarget(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
root := t.TempDir()
|
||||
source := filepath.Join(root, "old.txt")
|
||||
target := filepath.Join(root, "new.txt")
|
||||
if err := os.WriteFile(source, []byte("payload"), 0o644); err != nil {
|
||||
t.Fatalf("write source: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(target, []byte("payload"), 0o644); err != nil {
|
||||
t.Fatalf("write target: %v", err)
|
||||
}
|
||||
|
||||
_, err := RenamePath(source, "new.txt")
|
||||
if err == nil {
|
||||
t.Fatalf("expected overwrite error, got nil")
|
||||
}
|
||||
if got, want := err.Error(), ErrOverwrite(target).Error(); got != want {
|
||||
t.Fatalf("expected overwrite error %q, got %q", want, got)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue