feat: add save_as mini mode

This commit is contained in:
CJ van den Berg 2024-08-30 20:18:11 +02:00
parent 34540bbff0
commit a2674c121e
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 50 additions and 0 deletions

View file

@ -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 {};
}

View file

@ -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 {