Skip to content

Commit

Permalink
handle errors more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
BrookJeynes committed May 24, 2024
1 parent efca328 commit f370869
Showing 1 changed file with 78 additions and 60 deletions.
138 changes: 78 additions & 60 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -68,18 +69,26 @@ 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 => {
const entry = view.entries.get(view.entries.selected) catch continue;

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 => {},
Expand Down Expand Up @@ -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 }}, .{});
},
}
}
}

Expand Down

0 comments on commit f370869

Please sign in to comment.