From b1362814db59be420c7e2090dec57f6cf3615fa8 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Mon, 14 Oct 2024 19:27:38 +0200 Subject: [PATCH] fix: catch more errors in file browser --- src/main.zig | 14 ++++---------- src/project_manager.zig | 2 ++ src/tui/mode/mini/file_browser.zig | 16 +++++++++++----- src/tui/mode/mini/open_file.zig | 4 ++-- src/tui/mode/mini/save_as.zig | 2 +- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/main.zig b/src/main.zig index b12761e..d1f27e2 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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; } diff --git a/src/project_manager.zig b/src/project_manager.zig index 7cb006f..b29774f 100644 --- a/src/project_manager.zig +++ b/src/project_manager.zig @@ -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}); diff --git a/src/tui/mode/mini/file_browser.zig b/src/tui/mode/mini/file_browser.zig index ed4aa4b..dca661c 100644 --- a/src/tui/mode/mini/file_browser.zig +++ b/src/tui/mode/mini/file_browser.zig @@ -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); diff --git a/src/tui/mode/mini/open_file.zig b/src/tui/mode/mini/open_file.zig index 8e8744b..f5da436 100644 --- a/src/tui/mode/mini/open_file.zig +++ b/src/tui/mode/mini/open_file.zig @@ -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 {}; diff --git a/src/tui/mode/mini/save_as.zig b/src/tui/mode/mini/save_as.zig index edabaaf..39fc9a8 100644 --- a/src/tui/mode/mini/save_as.zig +++ b/src/tui/mode/mini/save_as.zig @@ -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 {};