Compare commits

..

3 commits

4 changed files with 45 additions and 14 deletions

View file

@ -98,9 +98,9 @@ pub fn request_recent_files(max: usize) (ProjectManagerError || ProjectError)!vo
return send(.{ "request_recent_files", project, max }); return send(.{ "request_recent_files", project, max });
} }
pub fn request_recent_projects(allocator: std.mem.Allocator) (ProjectError || CallError)!tp.message { pub fn request_recent_projects() (ProjectManagerError || ProjectError)!void {
const project = tp.env.get().str("project"); const project = tp.env.get().str("project");
return (try get()).pid.call(allocator, request_timeout, .{ "request_recent_projects", project }); return send(.{ "request_recent_projects", project });
} }
pub fn query_recent_files(max: usize, query: []const u8) (ProjectManagerError || ProjectError)!void { pub fn query_recent_files(max: usize, query: []const u8) (ProjectManagerError || ProjectError)!void {
@ -462,6 +462,9 @@ const Process = struct {
self.sort_projects_by_last_used(&recent_projects); self.sort_projects_by_last_used(&recent_projects);
var message = std.ArrayList(u8).init(self.allocator); var message = std.ArrayList(u8).init(self.allocator);
const writer = message.writer(); const writer = message.writer();
try cbor.writeArrayHeader(writer, 3);
try cbor.writeValue(writer, "PRJ");
try cbor.writeValue(writer, "recent_projects");
try cbor.writeArrayHeader(writer, recent_projects.items.len); try cbor.writeArrayHeader(writer, recent_projects.items.len);
for (recent_projects.items) |project| { for (recent_projects.items) |project| {
try cbor.writeArrayHeader(writer, 2); try cbor.writeArrayHeader(writer, 2);
@ -469,6 +472,7 @@ const Process = struct {
try cbor.writeValue(writer, if (self.projects.get(project.name)) |_| true else false); try cbor.writeValue(writer, if (self.projects.get(project.name)) |_| true else false);
} }
from.send_raw(.{ .buf = message.items }) catch return error.ClientFailed; from.send_raw(.{ .buf = message.items }) catch return error.ClientFailed;
self.logger.print("{d} projects found", .{recent_projects.items.len});
} }
fn query_recent_files(self: *Process, from: tp.pid_ref, project_directory: []const u8, max: usize, query: []const u8) (ProjectError || Project.ClientError)!void { fn query_recent_files(self: *Process, from: tp.pid_ref, project_directory: []const u8, max: usize, query: []const u8) (ProjectError || Project.ClientError)!void {

View file

@ -27,10 +27,12 @@ pub fn deinit(palette: *Type) void {
palette.allocator.free(entry.label); palette.allocator.free(entry.label);
} }
pub fn load_entries(palette: *Type) !usize { pub fn load_entries_with_args(palette: *Type, ctx: command.Context) !usize {
const rsp = try project_manager.request_recent_projects(palette.allocator); var items_cbor: []const u8 = undefined;
defer palette.allocator.free(rsp.buf); if (!(cbor.match(ctx.args.buf, .{ "PRJ", "recent_projects", tp.extract_cbor(&items_cbor) }) catch false))
var iter: []const u8 = rsp.buf; return error.InvalidRecentProjects;
var iter: []const u8 = items_cbor;
var len = try cbor.decodeArrayHeader(&iter); var len = try cbor.decodeArrayHeader(&iter);
while (len > 0) : (len -= 1) { while (len > 0) : (len -= 1) {
var name_: []const u8 = undefined; var name_: []const u8 = undefined;

View file

@ -47,6 +47,10 @@ pub fn Create(options: type) type {
pub const ButtonState = Button.State(*Menu.State(*Self)); pub const ButtonState = Button.State(*Menu.State(*Self));
pub fn create(allocator: std.mem.Allocator) !tui.Mode { pub fn create(allocator: std.mem.Allocator) !tui.Mode {
return create_with_args(allocator, .{});
}
pub fn create_with_args(allocator: std.mem.Allocator, ctx: command.Context) !tui.Mode {
const mv = tui.mainview() orelse return error.NotFound; const mv = tui.mainview() orelse return error.NotFound;
const self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self); errdefer allocator.destroy(self);
@ -77,7 +81,10 @@ pub fn Create(options: type) type {
.entries = std.ArrayList(Entry).init(allocator), .entries = std.ArrayList(Entry).init(allocator),
}; };
if (self.menu.scrollbar) |scrollbar| scrollbar.style_factory = scrollbar_style; if (self.menu.scrollbar) |scrollbar| scrollbar.style_factory = scrollbar_style;
self.longest_hint = try options.load_entries(self); self.longest_hint = if (@hasDecl(options, "load_entries_with_args"))
try options.load_entries_with_args(self, ctx)
else
try options.load_entries(self);
if (@hasDecl(options, "restore_state")) if (@hasDecl(options, "restore_state"))
options.restore_state(self) catch {}; options.restore_state(self) catch {};
try self.commands.init(self); try self.commands.init(self);
@ -467,11 +474,15 @@ pub fn Create(options: type) type {
const button = self.menu.get_selected() orelse return; const button = self.menu.get_selected() orelse return;
const refresh = options.delete_item(self.menu, button); const refresh = options.delete_item(self.menu, button);
if (refresh) { if (refresh) {
options.clear_entries(self); if (@hasDecl(options, "load_entries")) {
self.longest_hint = try options.load_entries(self); options.clear_entries(self);
if (self.entries.items.len > 0) self.longest_hint = try options.load_entries(self);
self.initial_selected = self.menu.selected; if (self.entries.items.len > 0)
try self.start_query(0); self.initial_selected = self.menu.selected;
try self.start_query(0);
} else {
return palette_menu_cancel(self, .{});
}
} }
} }
} }

View file

@ -413,6 +413,9 @@ fn receive_safe(self: *Self, from: tp.pid_ref, m: tp.message) !void {
return; return;
} }
if (try m.match(.{ "PRJ", "recent_projects", tp.more })) // async recent projects request
return self.enter_overlay_mode_with_args(@import("mode/overlay/open_recent_project.zig").Type, .{ .args = m });
if (try m.match(.{ "PRJ", tp.more })) // drop late project manager query responses if (try m.match(.{ "PRJ", tp.more })) // drop late project manager query responses
return; return;
@ -684,6 +687,17 @@ fn enter_overlay_mode(self: *Self, mode: type) command.Result {
refresh_hover(); refresh_hover();
} }
fn enter_overlay_mode_with_args(self: *Self, mode: type, ctx: command.Context) command.Result {
command.executeName("disable_fast_scroll", .{}) catch {};
command.executeName("disable_jump_mode", .{}) catch {};
if (self.mini_mode_) |_| try cmds.exit_mini_mode(self, .{});
if (self.input_mode_outer_) |_| try cmds.exit_overlay_mode(self, .{});
self.input_mode_outer_ = self.input_mode_;
self.input_mode_ = try mode.create_with_args(self.allocator, ctx);
if (self.input_mode_) |*m| m.run_init();
refresh_hover();
}
fn get_input_mode(self: *Self, mode_name: []const u8) !Mode { fn get_input_mode(self: *Self, mode_name: []const u8) !Mode {
return keybind.mode(mode_name, self.allocator, .{}); return keybind.mode(mode_name, self.allocator, .{});
} }
@ -914,8 +928,8 @@ const cmds = struct {
} }
pub const open_recent_meta: Meta = .{ .description = "Open recent" }; pub const open_recent_meta: Meta = .{ .description = "Open recent" };
pub fn open_recent_project(self: *Self, _: Ctx) Result { pub fn open_recent_project(_: *Self, _: Ctx) Result {
return self.enter_overlay_mode(@import("mode/overlay/open_recent_project.zig").Type); try project_manager.request_recent_projects();
} }
pub const open_recent_project_meta: Meta = .{ .description = "Open project" }; pub const open_recent_project_meta: Meta = .{ .description = "Open project" };