fix: catch more errors in file browser

This commit is contained in:
CJ van den Berg 2024-10-14 19:27:38 +02:00
parent 67f49110dc
commit b1362814db
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
5 changed files with 20 additions and 18 deletions

View file

@ -221,7 +221,7 @@ pub fn main() anyerror!void {
defer files.deinit();
for (dests.items) |dest| {
if (dest.file.len == 0) continue;
if (try is_directory(dest.file)) {
if (is_directory(dest.file)) {
if (have_project) {
std.debug.print("more than one directory is not allowed\n", .{});
exit(1);
@ -585,16 +585,10 @@ fn restart() noreturn {
exit(234);
}
pub fn is_directory(rel_path: []const u8) !bool {
pub fn is_directory(rel_path: []const u8) bool {
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
const abs_path = std.fs.cwd().realpath(rel_path, &path_buf) catch |e| switch (e) {
error.FileNotFound => return false,
else => return e,
};
var dir = std.fs.openDirAbsolute(abs_path, .{}) catch |e| switch (e) {
error.NotDir => return false,
else => return e,
};
const abs_path = std.fs.cwd().realpath(rel_path, &path_buf) catch return false;
var dir = std.fs.openDirAbsolute(abs_path, .{}) catch return false;
dir.close();
return true;
}

View file

@ -16,6 +16,8 @@ const Self = @This();
const module_name = @typeName(Self);
const request_timeout = std.time.ns_per_s * 5;
pub const Error = ProjectError || ProjectManagerError;
pub const ProjectError = error{NoProject};
const SpawnError = (OutOfMemoryError || error{ThespianSpawnFailed});

View file

@ -175,13 +175,13 @@ pub fn Create(options: type) type {
self.entries.clearRetainingCapacity();
}
fn try_complete_file(self: *Self) !void {
fn try_complete_file(self: *Self) project_manager.Error!void {
self.complete_trigger_count += 1;
if (self.complete_trigger_count == 1) {
self.query.clearRetainingCapacity();
self.match.clearRetainingCapacity();
self.clear_entries();
if (try root.is_directory(self.file_path.items)) {
if (root.is_directory(self.file_path.items)) {
try self.query.appendSlice(self.file_path.items);
} else if (self.file_path.items.len > 0) blk: {
const basename_begin = std.mem.lastIndexOfScalar(u8, self.file_path.items, std.fs.path.sep) orelse {
@ -198,7 +198,7 @@ pub fn Create(options: type) type {
}
}
fn reverse_complete_file(self: *Self) !void {
fn reverse_complete_file(self: *Self) error{OutOfMemory}!void {
if (self.complete_trigger_count < 2) {
self.complete_trigger_count = 0;
self.file_path.clearRetainingCapacity();
@ -222,6 +222,10 @@ pub fn Create(options: type) type {
try self.process_project_manager(m);
return true;
}
if (try cbor.match(m.buf, .{ "exit", "error.FileNotFound" })) {
message("path not found", .{});
return true;
}
return false;
}
@ -262,13 +266,15 @@ pub fn Create(options: type) type {
self.file_path.clearRetainingCapacity();
if (self.match.items.len > 0) {
try self.match_path();
} else {
} else if (self.entries.items.len > 0) {
try self.construct_path(self.query.items, self.entries.items[self.complete_trigger_count - 1], self.complete_trigger_count - 1);
} else {
try self.construct_path(self.query.items, .{ .name = "", .type = .file }, 0);
}
message("{d}/{d}", .{ self.matched_entry + 1, self.entries.items.len });
}
fn construct_path(self: *Self, path_: []const u8, entry: Entry, entry_no: usize) !void {
fn construct_path(self: *Self, path_: []const u8, entry: Entry, entry_no: usize) error{OutOfMemory}!void {
self.matched_entry = entry_no;
const path = project_manager.normalize_file_path(path_);
try self.file_path.appendSlice(path);

View file

@ -10,7 +10,7 @@ pub const Type = @import("file_browser.zig").Create(@This());
pub const create = Type.create;
pub fn load_entries(self: *Type) !void {
pub fn load_entries(self: *Type) error{ Exit, OutOfMemory }!void {
if (tui.current().mainview.dynamic_cast(mainview)) |mv_| if (mv_.get_editor()) |editor| {
if (editor.is_dirty()) return tp.exit("unsaved changes");
if (editor.file_path) |old_path|
@ -31,7 +31,7 @@ pub fn name(_: *Type) []const u8 {
}
pub fn select(self: *Type) void {
if (root.is_directory(self.file_path.items) catch false) return;
if (root.is_directory(self.file_path.items)) return;
if (self.file_path.items.len > 0)
tp.self_pid().send(.{ "cmd", "navigate", .{ .file = self.file_path.items } }) catch {};
command.executeName("exit_mini_mode", .{}) catch {};

View file

@ -28,7 +28,7 @@ pub fn name(_: *Type) []const u8 {
}
pub fn select(self: *Type) void {
if (root.is_directory(self.file_path.items) catch false) return;
if (root.is_directory(self.file_path.items)) return;
if (self.file_path.items.len > 0)
tp.self_pid().send(.{ "cmd", "save_file_as", .{self.file_path.items} }) catch {};
command.executeName("exit_mini_mode", .{}) catch {};