feat: allow opening project directories on the command line

This commit is contained in:
CJ van den Berg 2024-06-23 21:27:54 +02:00
parent 692e04b6a8
commit 1533ec2993
2 changed files with 37 additions and 2 deletions

View file

@ -38,7 +38,7 @@ pub fn main() anyerror!void {
\\--show-input Open the input view on start. \\--show-input Open the input view on start.
\\--show-log Open the log view on start. \\--show-log Open the log view on start.
\\-l, --language <lang> Force the language of the file to be opened. \\-l, --language <lang> Force the language of the file to be opened.
\\<file>... File to open. \\<file>... File or directory to open.
\\ Add +<LINE> to the command line or append \\ Add +<LINE> to the command line or append
\\ :LINE or :LINE:COL to the file name to jump \\ :LINE or :LINE:COL to the file name to jump
\\ to a location in the file. \\ to a location in the file.
@ -195,8 +195,28 @@ pub fn main() anyerror!void {
} }
} }
var have_project = false;
var files = std.ArrayList(Dest).init(a);
defer files.deinit();
for (dests.items) |dest| { for (dests.items) |dest| {
if (dest.file.len == 0) continue; if (dest.file.len == 0) continue;
if (try is_directory(dest.file)) {
if (have_project) {
std.debug.print("more than one directory is not allowed\n", .{});
exit(1);
}
try tui_proc.send(.{ "cmd", "open_project_dir", .{dest.file} });
have_project = true;
} else {
const curr = try files.addOne();
curr.* = dest;
}
}
for (files.items) |dest| {
if (dest.file.len == 0) continue;
if (dest.line) |l| { if (dest.line) |l| {
if (dest.column) |col| { if (dest.column) |col| {
try tui_proc.send(.{ "cmd", "navigate", .{ .file = dest.file, .line = l, .column = col } }); try tui_proc.send(.{ "cmd", "navigate", .{ .file = dest.file, .line = l, .column = col } });
@ -209,7 +229,8 @@ 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" }); if (!have_project)
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();
@ -536,3 +557,14 @@ fn restart() noreturn {
std.io.getStdErr().writer().print("\nrestart failed: {d}", .{ret}) catch {}; std.io.getStdErr().writer().print("\nrestart failed: {d}", .{ret}) catch {};
exit(234); exit(234);
} }
pub fn is_directory(rel_path: []const u8) !bool {
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
const abs_path = try std.fs.cwd().realpath(rel_path, &path_buf);
var dir = std.fs.openDirAbsolute(abs_path, .{}) catch |e| switch (e) {
error.NotDir => return false,
else => return e,
};
dir.close();
return true;
}

View file

@ -41,6 +41,9 @@ pub fn open_cwd() tp.result {
pub fn open(rel_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; var path_buf: [std.fs.max_path_bytes]u8 = undefined;
const project_directory = std.fs.cwd().realpath(rel_project_directory, &path_buf) catch "(none)"; const project_directory = std.fs.cwd().realpath(rel_project_directory, &path_buf) catch "(none)";
var dir = std.fs.openDirAbsolute(project_directory, .{}) catch |e| return tp.exit_error(e);
dir.setAsCwd() catch |e| return tp.exit_error(e);
dir.close();
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 });
} }