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 } }); try tui_proc.send(.{ "cmd", "navigate", .{ .file = dest.file } });
} }
} else { } else {
try tui_proc.send(.{ "cmd", "open_project_cwd" });
try tui_proc.send(.{ "cmd", "show_home" }); try tui_proc.send(.{ "cmd", "show_home" });
} }
ctx.run(); ctx.run();

View file

@ -35,12 +35,12 @@ pub fn shutdown() void {
} }
pub fn open_cwd() tp.result { pub fn open_cwd() tp.result {
var cwd_buf: [std.fs.max_path_bytes]u8 = undefined; return open(".");
const cwd = std.fs.cwd().realpath(".", &cwd_buf) catch "(none)";
return open(cwd);
} }
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); tp.env.get().str_set("project", project_directory);
return (try get()).pid.send(.{ "open", 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 { 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) { if (self.projects.get(project_directory) == null) {
self.logger.print("opening: {s}", .{project_directory});
const project = try self.a.create(Project); const project = try self.a.create(Project);
project.* = try Project.init(self.a, project_directory); project.* = try Project.init(self.a, project_directory);
try self.projects.put(try self.a.dupe(u8, project_directory), project); 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 { pub fn create(a: std.mem.Allocator, n: Plane) !Widget {
try project_manager.open_cwd();
const self = try a.create(Self); const self = try a.create(Self);
self.* = .{ self.* = .{
.a = a, .a = a,
@ -165,6 +164,19 @@ const cmds = struct {
try tp.self_pid().send("quit"); 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 { pub fn navigate(self: *Self, ctx: Ctx) tp.result {
tui.reset_drag_context(); tui.reset_drag_context();
const frame = tracy.initZone(@src(), .{ .name = "navigate" }); const frame = tracy.initZone(@src(), .{ .name = "navigate" });
@ -202,6 +214,10 @@ const cmds = struct {
file = file_name; file = file_name;
} else return tp.exit_error(error.InvalidArgument); } 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 f = project_manager.normalize_file_path(file orelse return);
const same_file = if (self.editor) |editor| if (editor.file_path) |fp| const same_file = if (self.editor) |editor| if (editor.file_path) |fp|
std.mem.eql(u8, fp, f) std.mem.eql(u8, fp, f)

View file

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