From 34540bbff0a00e2c51b1d3680fa261c184fb16df Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Fri, 30 Aug 2024 20:17:48 +0200 Subject: [PATCH] feat: add editor.save_file_as command --- src/tui/editor.zig | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index dd3e65a..ae8a350 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -459,6 +459,14 @@ pub const Editor = struct { self.last.dirty = false; } + fn save_as(self: *Self, file_path: []const u8) !void { + if (self.buffer) |b_mut| try b_mut.store_to_file_and_clean(file_path); + if (self.file_path) |old_file_path| self.a.free(old_file_path); + self.file_path = try self.a.dupe(u8, file_path); + try self.send_editor_save(self.file_path.?); + self.last.dirty = false; + } + pub fn push_cursor(self: *Self) !void { const primary = self.cursels.getLastOrNull() orelse CurSel{} orelse CurSel{}; (try self.cursels.addOne()).* = primary; @@ -3014,6 +3022,13 @@ pub const Editor = struct { try self.save(); } + pub fn save_file_as(self: *Self, ctx: Context) Result { + var file_path: []const u8 = undefined; + if (ctx.args.match(.{tp.extract(&file_path)}) catch false) { + try self.save_as(file_path); + } else return error.InvalidArgument; + } + pub fn close_file(self: *Self, _: Context) Result { self.cancel_all_selections(); try self.close();