地圖 (Jido) is a lightweight Unix TUI file explorer designed for speed and simplicity.

refactor: Changed action struct field names to be more clear.

+32 -50
+9 -12
src/app.zig
··· 66 66 help_menu, 67 67 }; 68 68 69 - const ActionPaths = struct { 70 - /// Allocated. 71 - old: []const u8, 72 - /// Allocated. 73 - new: []const u8, 74 - }; 75 - 76 69 pub const Action = union(enum) { 77 - delete: ActionPaths, 78 - rename: ActionPaths, 70 + delete: struct { prev_path: []const u8, new_path: []const u8 }, 71 + rename: struct { prev_path: []const u8, new_path: []const u8 }, 79 72 paste: []const u8, 80 73 }; 81 74 ··· 140 133 pub fn deinit(self: *App) void { 141 134 while (self.actions.pop()) |action| { 142 135 switch (action) { 143 - .delete, .rename => |a| { 144 - self.alloc.free(a.new); 145 - self.alloc.free(a.old); 136 + .delete => |a| { 137 + self.alloc.free(a.new_path); 138 + self.alloc.free(a.prev_path); 139 + }, 140 + .rename => |a| { 141 + self.alloc.free(a.new_path); 142 + self.alloc.free(a.prev_path); 146 143 }, 147 144 .paste => |a| self.alloc.free(a), 148 145 }
+23 -38
src/event_handlers.zig
··· 74 74 if (entry) |e| break :lbl e else return; 75 75 }; 76 76 77 - var old_path_buf: [std.fs.max_path_bytes]u8 = undefined; 78 - const old_path = try app.alloc.dupe(u8, try app.directories.dir.realpath(entry.name, &old_path_buf)); 77 + var prev_path_buf: [std.fs.max_path_bytes]u8 = undefined; 78 + const prev_path = try app.alloc.dupe(u8, try app.directories.dir.realpath(entry.name, &prev_path_buf)); 79 79 80 80 var trash_dir = dir: { 81 81 notfound: { 82 82 break :dir (config.trashDir() catch break :notfound) orelse break :notfound; 83 83 } 84 - app.alloc.free(old_path); 84 + app.alloc.free(prev_path); 85 85 try app.notification.writeErr(.ConfigPathNotFound); 86 86 return; 87 87 }; ··· 89 89 var trash_dir_path_buf: [std.fs.max_path_bytes]u8 = undefined; 90 90 const trash_dir_path = try trash_dir.realpath(".", &trash_dir_path_buf); 91 91 92 - if (std.mem.eql(u8, old_path, trash_dir_path)) { 92 + if (std.mem.eql(u8, prev_path, trash_dir_path)) { 93 93 try app.notification.writeErr(.CannotDeleteTrashDir); 94 - app.alloc.free(old_path); 94 + app.alloc.free(prev_path); 95 95 return; 96 96 } 97 97 ··· 100 100 101 101 if (app.directories.dir.rename(entry.name, tmp_path)) { 102 102 if (app.actions.push(.{ 103 - .delete = .{ .old = old_path, .new = tmp_path }, 103 + .delete = .{ .prev_path = prev_path, .new_path = tmp_path }, 104 104 })) |prev_elem| { 105 - app.alloc.free(prev_elem.delete.old); 106 - app.alloc.free(prev_elem.delete.new); 105 + app.alloc.free(prev_elem.delete.prev_path); 106 + app.alloc.free(prev_elem.delete.new_path); 107 107 } 108 108 109 109 try app.notification.writeInfo(.Deleted); ··· 113 113 error.RenameAcrossMountPoints => try app.notification.writeErr(.UnableToDeleteAcrossMountPoints), 114 114 else => try app.notification.writeErr(.UnableToDelete), 115 115 } 116 - app.alloc.free(old_path); 116 + app.alloc.free(prev_path); 117 117 app.alloc.free(tmp_path); 118 118 } 119 119 }, ··· 446 446 } 447 447 }, 448 448 .rename => |a| { 449 - defer app.alloc.free(a.new); 450 - defer app.alloc.free(a.old); 449 + defer app.alloc.free(a.new_path); 450 + defer app.alloc.free(a.prev_path); 451 451 452 - var had_duplicate = false; 453 - 454 - // Handle if item with same name already exists. 455 452 var new_path_buf: [std.fs.max_path_bytes]u8 = undefined; 456 - const new_path = if (environment.fileExists(app.directories.dir, a.old)) lbl: { 457 - const extension = std.fs.path.extension(a.old); 458 - had_duplicate = true; 459 - break :lbl try std.fmt.bufPrint( 460 - &new_path_buf, 461 - "{s}-{s}{s}", 462 - .{ a.old[0 .. a.old.len - extension.len], zuid.new.v4(), extension }, 463 - ); 464 - } else lbl: { 465 - break :lbl a.old; 466 - }; 453 + const new_path_res = try environment.checkDuplicatePath(&new_path_buf, app.directories.dir, a.prev_path); 467 454 468 - if (app.directories.dir.rename(a.new, new_path)) { 455 + if (app.directories.dir.rename(a.new_path, new_path_res.path)) { 469 456 app.directories.clearEntries(); 470 - const fuzzy = inputToSlice(app); 471 - app.directories.populateEntries(fuzzy) catch |err| { 457 + app.directories.populateEntries("") catch |err| { 472 458 switch (err) { 473 459 error.AccessDenied => try app.notification.writeErr(.PermissionDenied), 474 460 else => try app.notification.writeErr(.UnknownError), 475 461 } 476 462 }; 477 - if (had_duplicate) { 463 + if (new_path_res.had_duplicate) { 478 464 try app.notification.writeWarn(.DuplicateFileOnUndo); 479 465 } else { 480 466 try app.notification.writeInfo(.RestoredRename); ··· 590 576 var dir_prefix_buf: [std.fs.max_path_bytes]u8 = undefined; 591 577 const dir_prefix = try app.directories.dir.realpath(".", &dir_prefix_buf); 592 578 593 - const old = lbl: { 579 + const entry = lbl: { 594 580 const entry = app.directories.getSelected() catch { 595 581 try app.notification.writeErr(.UnableToRename); 596 582 return; 597 583 }; 598 584 if (entry) |e| break :lbl e else return; 599 585 }; 600 - const new = inputToSlice(app); 586 + const new_path = inputToSlice(app); 601 587 602 - if (environment.fileExists(app.directories.dir, new)) { 588 + if (environment.fileExists(app.directories.dir, new_path)) { 603 589 try app.notification.writeErr(.ItemAlreadyExists); 604 590 } else { 605 - app.directories.dir.rename(old.name, new) catch |err| switch (err) { 591 + app.directories.dir.rename(entry.name, new_path) catch |err| switch (err) { 606 592 error.AccessDenied => try app.notification.writeErr(.PermissionDenied), 607 593 error.PathAlreadyExists => try app.notification.writeErr(.ItemAlreadyExists), 608 594 else => try app.notification.writeErr(.UnknownError), 609 595 }; 610 596 if (app.actions.push(.{ 611 597 .rename = .{ 612 - .old = try std.fs.path.join(app.alloc, &.{ dir_prefix, old.name }), 613 - .new = try std.fs.path.join(app.alloc, &.{ dir_prefix, new }), 598 + .prev_path = try std.fs.path.join(app.alloc, &.{ dir_prefix, entry.name }), 599 + .new_path = try std.fs.path.join(app.alloc, &.{ dir_prefix, new_path }), 614 600 }, 615 601 })) |prev_elem| { 616 - app.alloc.free(prev_elem.rename.old); 617 - app.alloc.free(prev_elem.rename.new); 602 + app.alloc.free(prev_elem.rename.prev_path); 603 + app.alloc.free(prev_elem.rename.new_path); 618 604 } 619 605 620 606 try app.notification.writeInfo(.Renamed); ··· 664 650 break :supported; 665 651 } 666 652 667 - // TODO(06-01-25): Add a confirmation for this. 668 653 if (std.mem.eql(u8, command, ":empty_trash")) { 669 654 try commands.emptyTrash(app); 670 655 break :supported;