From a2674c121e1cfe73390f47529a0fc8354b4ea7d8 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Fri, 30 Aug 2024 20:18:11 +0200 Subject: [PATCH] feat: add save_as mini mode --- src/tui/mode/mini/save_as.zig | 46 +++++++++++++++++++++++++++++++++++ src/tui/tui.zig | 4 +++ 2 files changed, 50 insertions(+) create mode 100644 src/tui/mode/mini/save_as.zig diff --git a/src/tui/mode/mini/save_as.zig b/src/tui/mode/mini/save_as.zig new file mode 100644 index 0000000..9ac78bc --- /dev/null +++ b/src/tui/mode/mini/save_as.zig @@ -0,0 +1,46 @@ +const std = @import("std"); +const tp = @import("thespian"); +const log = @import("log"); +const root = @import("root"); + +const key = @import("renderer").input.key; +const mod = @import("renderer").input.modifier; +const event_type = @import("renderer").input.event_type; +const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8; +const project_manager = @import("project_manager"); + +const tui = @import("../../tui.zig"); +const mainview = @import("../../mainview.zig"); +const command = @import("../../command.zig"); +const EventHandler = @import("../../EventHandler.zig"); +const MessageFilter = @import("../../MessageFilter.zig"); + +pub const Type = @import("file_browser.zig").Create(@This()); + +pub const create = Type.create; + +pub fn load_entries(self: *Type) !void { + if (tui.current().mainview.dynamic_cast(mainview)) |mv_| if (mv_.get_editor()) |editor| { + if (editor.file_path) |old_path| + if (std.mem.lastIndexOf(u8, old_path, "/")) |pos| + try self.file_path.appendSlice(old_path[0 .. pos + 1]); + if (editor.get_primary().selection) |sel| ret: { + const text = editor.get_selection(sel, self.a) catch break :ret; + defer self.a.free(text); + if (!(text.len > 2 and std.mem.eql(u8, text[0..2], ".."))) + self.file_path.clearRetainingCapacity(); + try self.file_path.appendSlice(text); + } + }; +} + +pub fn name(_: *Type) []const u8 { + return " save as"; +} + +pub fn select(self: *Type) void { + if (root.is_directory(self.file_path.items) catch false) return; + if (self.file_path.items.len > 0) + tp.self_pid().send(.{ "cmd", "save_file_as", .{self.file_path.items} }) catch {}; + command.executeName("exit_mini_mode", .{}) catch {}; +} diff --git a/src/tui/tui.zig b/src/tui/tui.zig index b93666f..d0a6b53 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -698,6 +698,10 @@ const cmds = struct { return enter_mini_mode(self, @import("mode/mini/open_file.zig"), ctx); } + pub fn save_as(self: *Self, ctx: Ctx) Result { + return enter_mini_mode(self, @import("mode/mini/save_as.zig"), ctx); + } + const MiniModeFactory = fn (Allocator, Ctx) error{ NotFound, OutOfMemory }!EventHandler; fn enter_mini_mode(self: *Self, comptime mode: anytype, ctx: Ctx) Result {