From f370869bd84d5fe9c06ec69f302fdd814c044f3a Mon Sep 17 00:00:00 2001 From: Brook J Date: Fri, 24 May 2024 22:17:59 +1000 Subject: [PATCH] handle errors more gracefully --- src/main.zig | 138 +++++++++++++++++++++++++++++---------------------- 1 file changed, 78 insertions(+), 60 deletions(-) diff --git a/src/main.zig b/src/main.zig index a1e4ddb..2896533 100644 --- a/src/main.zig +++ b/src/main.zig @@ -56,6 +56,7 @@ pub fn main() !void { var last_pressed: ?vaxis.Key = null; var last_known_height: usize = vx.window().height; + var was_error: bool = false; while (true) { const event = loop.nextEvent(); @@ -68,8 +69,12 @@ pub fn main() !void { switch (key.codepoint) { '-', 'h', Key.left => { view.cleanup(); - try view.open("../"); - try view.populate(); + view.open("../") catch { + was_error = true; + }; + view.populate() catch { + was_error = true; + }; last_pressed = null; }, Key.enter, 'l', Key.right => { @@ -77,9 +82,13 @@ pub fn main() !void { switch (entry.kind) { .directory => { - try view.open(entry.name); + view.open(entry.name) catch { + was_error = true; + }; view.cleanup(); - try view.populate(); + view.populate() catch { + was_error = true; + }; }, .file => {}, else => {}, @@ -166,74 +175,83 @@ pub fn main() !void { .height = .{ .limit = win.height - (top_right_bar.height + bottom_right_bar.height + top_div + bottom_div) }, }); - if (view.entries.all().len > 0 and config.preview_file == true) { - const entry = try view.entries.get(view.entries.selected); - - @memcpy(&last_path, &path); - last_item_path = last_path[0..current_item_path.len]; - current_item_path = try std.fmt.bufPrint(&path, "{s}/{s}", .{ try view.full_path("."), entry.name }); + if (was_error == true) { + _ = try right_bar.print(&.{ + .{ + .text = "An error occured :(", + }, + }, .{}); + was_error = false; + } else { + if (view.entries.all().len > 0 and config.preview_file == true) { + const entry = try view.entries.get(view.entries.selected); - switch (entry.kind) { - .directory => { - view.cleanup_sub(); - try view.open_sub(current_item_path); - try view.populate_sub(); + @memcpy(&last_path, &path); + last_item_path = last_path[0..current_item_path.len]; + current_item_path = try std.fmt.bufPrint(&path, "{s}/{s}", .{ try view.full_path("."), entry.name }); - file_metadata = try view.sub_dir.metadata(); + switch (entry.kind) { + .directory => { + view.cleanup_sub(); + try view.open_sub(current_item_path); + try view.populate_sub(); - try view.sub_entries.render(right_bar, null, config.styles.list_item, null, null); - }, - .file => file: { - var file = try view.dir.openFile(entry.name, .{ .mode = .read_only }); - defer file.close(); - const bytes = try file.readAll(&file_buf); - - // Handle image. - if (config.show_images == true) unsupported_terminal: { - const supported: [1][]const u8 = .{".png"}; - - for (supported) |ext| { - if (std.mem.eql(u8, get_extension(entry.name), ext)) { - // Don't re-render preview if we haven't changed selection. - if (std.mem.eql(u8, last_item_path, current_item_path)) break :file; - - if (vx.loadImage(alloc, .{ .path = current_item_path })) |img| { - image = img; - } else |_| { - image = null; - break :unsupported_terminal; - } + file_metadata = try view.sub_dir.metadata(); - break :file; - } else { - // Free any image we might have already. - if (image) |img| { - vx.freeImage(img.id); + try view.sub_entries.render(right_bar, null, config.styles.list_item, null, null); + }, + .file => file: { + var file = try view.dir.openFile(entry.name, .{ .mode = .read_only }); + defer file.close(); + const bytes = try file.readAll(&file_buf); + + // Handle image. + if (config.show_images == true) unsupported_terminal: { + const supported: [1][]const u8 = .{".png"}; + + for (supported) |ext| { + if (std.mem.eql(u8, get_extension(entry.name), ext)) { + // Don't re-render preview if we haven't changed selection. + if (std.mem.eql(u8, last_item_path, current_item_path)) break :file; + + if (vx.loadImage(alloc, .{ .path = current_item_path })) |img| { + image = img; + } else |_| { + image = null; + break :unsupported_terminal; + } + + break :file; + } else { + // Free any image we might have already. + if (image) |img| { + vx.freeImage(img.id); + } } } } - } - // Handle utf-8. - if (std.unicode.utf8ValidateSlice(file_buf[0..bytes])) { + // Handle utf-8. + if (std.unicode.utf8ValidateSlice(file_buf[0..bytes])) { + _ = try right_bar.print(&.{ + .{ + .text = file_buf[0..bytes], + }, + }, .{}); + break :file; + } + + // Fallback to no preview. _ = try right_bar.print(&.{ .{ - .text = file_buf[0..bytes], + .text = "No preview available.", }, }, .{}); - break :file; - } - - // Fallback to no preview. - _ = try right_bar.print(&.{ - .{ - .text = "No preview available.", - }, - }, .{}); - }, - else => { - _ = try right_bar.print(&.{vaxis.Segment{ .text = current_item_path }}, .{}); - }, + }, + else => { + _ = try right_bar.print(&.{vaxis.Segment{ .text = current_item_path }}, .{}); + }, + } } }