From 30b0a8fe97d223f4aa91a67016b2aa81d33a8dbd Mon Sep 17 00:00:00 2001 From: BrookJeynes Date: Tue, 25 Mar 2025 13:29:44 +1000 Subject: [PATCH] fix: Undoing a delete/rename wont overwrite an item with the same name now --- src/event_handlers.zig | 50 +++++++++++++++++++++++++++++++++++++----- src/notification.zig | 3 ++- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/event_handlers.zig b/src/event_handlers.zig index dd6c255..51cd487 100644 --- a/src/event_handlers.zig +++ b/src/event_handlers.zig @@ -275,8 +275,23 @@ pub fn handleNormalEvent( defer app.alloc.free(a.new); defer app.alloc.free(a.old); - // TODO: Will overwrite an item if it has the same name. - if (app.directories.dir.rename(a.new, a.old)) { + var had_duplicate = false; + + // Handle if item with same name already exists. + var new_path_buf: [std.fs.max_path_bytes]u8 = undefined; + const new_path = if (environment.fileExists(app.directories.dir, a.old)) lbl: { + const extension = std.fs.path.extension(a.old); + had_duplicate = true; + break :lbl try std.fmt.bufPrint( + &new_path_buf, + "{s}-{s}{s}", + .{ a.old[0 .. a.old.len - extension.len], zuid.new.v4(), extension }, + ); + } else lbl: { + break :lbl a.old; + }; + + if (app.directories.dir.rename(a.new, new_path)) { app.directories.clearEntries(); const fuzzy = inputToSlice(app); app.directories.populateEntries(fuzzy) catch |err| { @@ -285,7 +300,11 @@ pub fn handleNormalEvent( else => try app.notification.writeErr(.UnknownError), } }; - try app.notification.writeInfo(.RestoredDelete); + if (had_duplicate) { + try app.notification.writeWarn(.DuplicateFileOnUndo); + } else { + try app.notification.writeInfo(.RestoredDelete); + } } else |_| { try app.notification.writeErr(.UnableToUndo); } @@ -294,8 +313,23 @@ pub fn handleNormalEvent( defer app.alloc.free(a.new); defer app.alloc.free(a.old); - // TODO: Will overwrite an item if it has the same name. - if (app.directories.dir.rename(a.new, a.old)) { + var had_duplicate = false; + + // Handle if item with same name already exists. + var new_path_buf: [std.fs.max_path_bytes]u8 = undefined; + const new_path = if (environment.fileExists(app.directories.dir, a.old)) lbl: { + const extension = std.fs.path.extension(a.old); + had_duplicate = true; + break :lbl try std.fmt.bufPrint( + &new_path_buf, + "{s}-{s}{s}", + .{ a.old[0 .. a.old.len - extension.len], zuid.new.v4(), extension }, + ); + } else lbl: { + break :lbl a.old; + }; + + if (app.directories.dir.rename(a.new, new_path)) { app.directories.clearEntries(); const fuzzy = inputToSlice(app); app.directories.populateEntries(fuzzy) catch |err| { @@ -304,7 +338,11 @@ pub fn handleNormalEvent( else => try app.notification.writeErr(.UnknownError), } }; - try app.notification.writeInfo(.RestoredRename); + if (had_duplicate) { + try app.notification.writeWarn(.DuplicateFileOnUndo); + } else { + try app.notification.writeInfo(.RestoredRename); + } } else |_| { try app.notification.writeErr(.UnableToUndo); } diff --git a/src/notification.zig b/src/notification.zig index 2ede17c..a05dba2 100644 --- a/src/notification.zig +++ b/src/notification.zig @@ -45,7 +45,7 @@ const Info = enum { ConfigReloaded, }; -const Warn = enum { DeprecatedConfigPath }; +const Warn = enum { DeprecatedConfigPath, DuplicateFileOnUndo }; buf: [1024]u8 = undefined, style: Style = Style.info, @@ -106,6 +106,7 @@ pub fn writeInfo(self: *Self, info: Info) !void { pub fn writeWarn(self: *Self, warning: Warn) !void { try switch (warning) { .DeprecatedConfigPath => self.write("You are using a deprecated config path. Please move your config to either `$XDG_CONFIG_HOME/jido` or `$HOME/.jido`", .warn), + .DuplicateFileOnUndo => self.write("A file with the same name already exists. A unique identifier has been appending to the duplicated item.", .warn), }; } -- 2.51.2