feat: limit number of recent files returned by project manager
This commit is contained in:
parent
20e9327ac7
commit
c4315d1dc5
2 changed files with 15 additions and 10 deletions
|
@ -41,11 +41,11 @@ pub fn open(project_directory: []const u8) tp.result {
|
||||||
return (try get()).pid.send(.{ "open", project_directory });
|
return (try get()).pid.send(.{ "open", project_directory });
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn request_recent_files() tp.result {
|
pub fn request_recent_files(max: usize) tp.result {
|
||||||
const project = tp.env.get().str("project");
|
const project = tp.env.get().str("project");
|
||||||
if (project.len == 0)
|
if (project.len == 0)
|
||||||
return tp.exit("No project");
|
return tp.exit("No project");
|
||||||
return (try get()).pid.send(.{ "request_recent_files", project });
|
return (try get()).pid.send(.{ "request_recent_files", project, max });
|
||||||
}
|
}
|
||||||
|
|
||||||
const Process = struct {
|
const Process = struct {
|
||||||
|
@ -95,6 +95,7 @@ const Process = struct {
|
||||||
var path: []const u8 = undefined;
|
var path: []const u8 = undefined;
|
||||||
var high: i64 = 0;
|
var high: i64 = 0;
|
||||||
var low: i64 = 0;
|
var low: i64 = 0;
|
||||||
|
var max: usize = 0;
|
||||||
|
|
||||||
if (try m.match(.{ "walk_tree_entry", tp.extract(&project_directory), tp.extract(&path), tp.extract(&high), tp.extract(&low) })) {
|
if (try m.match(.{ "walk_tree_entry", tp.extract(&project_directory), tp.extract(&path), tp.extract(&high), tp.extract(&low) })) {
|
||||||
const mtime = (@as(i128, @intCast(high)) << 64) | @as(i128, @intCast(low));
|
const mtime = (@as(i128, @intCast(high)) << 64) | @as(i128, @intCast(low));
|
||||||
|
@ -113,8 +114,8 @@ const Process = struct {
|
||||||
});
|
});
|
||||||
} else if (try m.match(.{ "open", tp.extract(&project_directory) })) {
|
} else if (try m.match(.{ "open", tp.extract(&project_directory) })) {
|
||||||
self.open(project_directory) catch |e| return from.send_raw(tp.exit_message(e));
|
self.open(project_directory) catch |e| return from.send_raw(tp.exit_message(e));
|
||||||
} else if (try m.match(.{ "request_recent_files", tp.extract(&project_directory) })) {
|
} else if (try m.match(.{ "request_recent_files", tp.extract(&project_directory), tp.extract(&max) })) {
|
||||||
self.request_recent_files(from, project_directory) catch |e| return from.send_raw(tp.exit_message(e));
|
self.request_recent_files(from, project_directory, max) catch |e| return from.send_raw(tp.exit_message(e));
|
||||||
} else if (try m.match(.{"shutdown"})) {
|
} else if (try m.match(.{"shutdown"})) {
|
||||||
if (self.walker) |pid| pid.send(.{"stop"}) catch {};
|
if (self.walker) |pid| pid.send(.{"stop"}) catch {};
|
||||||
try from.send(.{ "project_manager", "shutdown" });
|
try from.send(.{ "project_manager", "shutdown" });
|
||||||
|
@ -136,9 +137,10 @@ const Process = struct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn request_recent_files(self: *Process, from: tp.pid_ref, project_directory: []const u8) error{ OutOfMemory, Exit }!void {
|
fn request_recent_files(self: *Process, from: tp.pid_ref, project_directory: []const u8, max: usize) error{ OutOfMemory, Exit }!void {
|
||||||
const project = if (self.projects.get(project_directory)) |p| p else return tp.exit("No project");
|
const project = if (self.projects.get(project_directory)) |p| p else return tp.exit("No project");
|
||||||
return project.request_recent_files(from);
|
project.sort_files_by_mtime();
|
||||||
|
return project.request_recent_files(from, max);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -181,9 +183,11 @@ const Project = struct {
|
||||||
std.mem.sort(File, self.files.items, {}, less_fn);
|
std.mem.sort(File, self.files.items, {}, less_fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn request_recent_files(self: *Project, from: tp.pid_ref) error{ OutOfMemory, Exit }!void {
|
fn request_recent_files(self: *Project, from: tp.pid_ref, max: usize) error{ OutOfMemory, Exit }!void {
|
||||||
for (self.files.items) |file|
|
for (self.files.items, 0..) |file, i| {
|
||||||
try from.send(.{ "PRJ", "recent", file.path });
|
try from.send(.{ "PRJ", "recent", file.path });
|
||||||
|
if (i >= max) return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@ const mainview = @import("../../mainview.zig");
|
||||||
const project_manager = @import("project_manager");
|
const project_manager = @import("project_manager");
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
const max_recent_files: usize = 25;
|
||||||
|
|
||||||
a: std.mem.Allocator,
|
a: std.mem.Allocator,
|
||||||
f: usize = 0,
|
f: usize = 0,
|
||||||
|
@ -33,7 +34,7 @@ pub fn create(a: std.mem.Allocator) !tui.Mode {
|
||||||
};
|
};
|
||||||
try self.commands.init(self);
|
try self.commands.init(self);
|
||||||
try tui.current().message_filters.add(MessageFilter.bind(self, receive_project_manager));
|
try tui.current().message_filters.add(MessageFilter.bind(self, receive_project_manager));
|
||||||
try project_manager.request_recent_files();
|
try project_manager.request_recent_files(max_recent_files);
|
||||||
self.menu.resize(.{ .y = 0, .x = 25, .w = 32 });
|
self.menu.resize(.{ .y = 0, .x = 25, .w = 32 });
|
||||||
try mv.floating_views.add(self.menu.menu_widget);
|
try mv.floating_views.add(self.menu.menu_widget);
|
||||||
return .{
|
return .{
|
||||||
|
@ -87,7 +88,7 @@ fn receive_project_manager(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit
|
||||||
fn process_project_manager(self: *Self, m: tp.message) tp.result {
|
fn process_project_manager(self: *Self, m: tp.message) tp.result {
|
||||||
var file_name: []const u8 = undefined;
|
var file_name: []const u8 = undefined;
|
||||||
if (try m.match(.{ "PRJ", "recent", tp.extract(&file_name) })) {
|
if (try m.match(.{ "PRJ", "recent", tp.extract(&file_name) })) {
|
||||||
if (self.count < 15) {
|
if (self.count < max_recent_files) {
|
||||||
self.count += 1;
|
self.count += 1;
|
||||||
self.longest = @max(self.longest, file_name.len);
|
self.longest = @max(self.longest, file_name.len);
|
||||||
self.menu.add_item_with_handler(file_name, menu_action_open_file) catch |e| return tp.exit_error(e);
|
self.menu.add_item_with_handler(file_name, menu_action_open_file) catch |e| return tp.exit_error(e);
|
||||||
|
|
Loading…
Add table
Reference in a new issue