refactor: re-work how projects are opened

Prep for opening directories on the command line.
This commit is contained in:
CJ van den Berg 2024-06-23 20:53:35 +02:00
parent 23fcf64b62
commit 692e04b6a8
4 changed files with 30 additions and 11 deletions

View file

@ -209,6 +209,7 @@ pub fn main() anyerror!void {
try tui_proc.send(.{ "cmd", "navigate", .{ .file = dest.file } });
}
} else {
try tui_proc.send(.{ "cmd", "open_project_cwd" });
try tui_proc.send(.{ "cmd", "show_home" });
}
ctx.run();

View file

@ -35,12 +35,12 @@ pub fn shutdown() void {
}
pub fn open_cwd() tp.result {
var cwd_buf: [std.fs.max_path_bytes]u8 = undefined;
const cwd = std.fs.cwd().realpath(".", &cwd_buf) catch "(none)";
return open(cwd);
return open(".");
}
pub fn open(project_directory: []const u8) tp.result {
pub fn open(rel_project_directory: []const u8) tp.result {
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
const project_directory = std.fs.cwd().realpath(rel_project_directory, &path_buf) catch "(none)";
tp.env.get().str_set("project", project_directory);
return (try get()).pid.send(.{ "open", project_directory });
}
@ -234,8 +234,8 @@ const Process = struct {
}
fn open(self: *Process, project_directory: []const u8) error{ OutOfMemory, Exit }!void {
self.logger.print("opening: {s}", .{project_directory});
if (self.projects.get(project_directory) == null) {
self.logger.print("opening: {s}", .{project_directory});
const project = try self.a.create(Project);
project.* = try Project.init(self.a, project_directory);
try self.projects.put(try self.a.dupe(u8, project_directory), project);

View file

@ -45,7 +45,6 @@ const NavState = struct {
};
pub fn create(a: std.mem.Allocator, n: Plane) !Widget {
try project_manager.open_cwd();
const self = try a.create(Self);
self.* = .{
.a = a,
@ -165,6 +164,19 @@ const cmds = struct {
try tp.self_pid().send("quit");
}
pub fn open_project_cwd(self: *Self, _: Ctx) tp.result {
try project_manager.open_cwd();
_ = try self.statusbar.msg(.{ "PRJ", "open" });
}
pub fn open_project_dir(self: *Self, ctx: Ctx) tp.result {
var project_dir: []const u8 = undefined;
if (!try ctx.args.match(.{tp.extract(&project_dir)}))
return;
try project_manager.open(project_dir);
_ = try self.statusbar.msg(.{ "PRJ", "open" });
}
pub fn navigate(self: *Self, ctx: Ctx) tp.result {
tui.reset_drag_context();
const frame = tracy.initZone(@src(), .{ .name = "navigate" });
@ -202,6 +214,10 @@ const cmds = struct {
file = file_name;
} else return tp.exit_error(error.InvalidArgument);
if (tp.env.get().str("project").len == 0) {
try open_project_cwd(self, .{});
}
const f = project_manager.normalize_file_path(file orelse return);
const same_file = if (self.editor) |editor| if (editor.file_path) |fp|
std.mem.eql(u8, fp, f)

View file

@ -29,7 +29,7 @@ column: usize,
file_exists: bool,
file_dirty: bool = false,
detailed: bool = false,
project: bool = false,
file: bool = false,
const project_icon = "";
const Self = @This();
@ -53,7 +53,6 @@ pub fn create(a: Allocator, parent: Plane) !Widget {
.on_render = render,
.on_receive = receive,
});
btn.opts.ctx.show_project();
return Widget.to(btn);
}
@ -131,7 +130,7 @@ fn render_detailed(self: *Self, plane: *Plane, theme: *const Widget.Theme) void
self.render_file_icon(plane, theme);
_ = plane.print(" ", .{}) catch {};
}
if (self.project) {
if (!self.file) {
const project_name = tp.env.get().str("project");
_ = plane.print("{s} ({s})", .{ self.name, project_name }) catch {};
} else {
@ -184,13 +183,17 @@ pub fn receive(self: *Self, _: *Button.State(Self), _: tp.pid_ref, m: tp.message
self.file_icon = self.file_icon_buf[0..file_icon.len :0];
self.file_dirty = false;
self.abbrv_home();
self.project = false;
self.file = true;
} else if (try m.match(.{ "E", "close" })) {
self.name = "";
self.lines = 0;
self.line = 0;
self.column = 0;
self.file_exists = true;
self.file = false;
self.show_project();
} else if (try m.match(.{ "PRJ", "open" })) {
if (!self.file)
self.show_project();
}
return false;
@ -214,7 +217,6 @@ fn show_project(self: *Self) void {
@memcpy(self.name_buf[0..project_name.len], project_name);
self.name = self.name_buf[0..project_name.len];
self.abbrv_home();
self.project = true;
}
fn abbrv_home(self: *Self) void {