Skip to content

Commit

Permalink
fix issue where viewing a pdf would result in zfe freezing
Browse files Browse the repository at this point in the history
  • Loading branch information
BrookJeynes committed Sep 1, 2024
1 parent 3364f73 commit 1c319c9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
34 changes: 20 additions & 14 deletions src/app.zig
Original file line number Diff line number Diff line change
Expand Up @@ -659,19 +659,22 @@ fn draw_preview(self: *App, win: vaxis.Window, file_name_win: vaxis.Window) !voi

// Handle pdf.
if (std.mem.eql(u8, std.fs.path.extension(entry.name), ".pdf")) {
var child = std.process.Child.init(&.{ "pdftotext", self.current_item_path, "-" }, self.alloc);
child.stdout_behavior = .Pipe;
child.stderr_behavior = .Close;
child.stdin_behavior = .Close;
try child.spawn();

const pdf_bytes = if (child.stdout) |stdout|
try stdout.reader().read(&self.directories.pdf_contents)
else
0;

const term = try child.wait();
if (term.Exited != 0) {
const output = std.process.Child.run(.{
.allocator = self.alloc,
.argv = &[_][]const u8{ "pdftotext", "-f", "0", "-l", "5", self.current_item_path, "-" },
.cwd_dir = self.directories.dir,
}) catch {
_ = try preview_win.print(&.{
.{
.text = "No preview available. Install pdftotext to get PDF previews.",
},
}, .{});
break :file;
};
defer self.alloc.free(output.stderr);
defer self.alloc.free(output.stdout);

if (output.term.Exited != 0) {
_ = try preview_win.print(&.{
.{
.text = "No preview available. Install pdftotext to get PDF previews.",
Expand All @@ -680,9 +683,12 @@ fn draw_preview(self: *App, win: vaxis.Window, file_name_win: vaxis.Window) !voi
break :file;
}

if (self.directories.pdf_contents) |contents| self.alloc.free(contents);
self.directories.pdf_contents = try self.alloc.dupe(u8, output.stdout);

_ = try preview_win.print(&.{
.{
.text = self.directories.pdf_contents[0..pdf_bytes],
.text = self.directories.pdf_contents.?,
},
}, .{});
break :file;
Expand Down
4 changes: 3 additions & 1 deletion src/directories.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ alloc: std.mem.Allocator,
dir: std.fs.Dir,
path_buf: [std.fs.max_path_bytes]u8 = undefined,
file_contents: [4096]u8 = undefined,
pdf_contents: [4096]u8 = undefined,
pdf_contents: ?[]u8 = null,
entries: List(std.fs.Dir.Entry),
history: CircStack(History, history_len),
sub_entries: List([]const u8),
Expand Down Expand Up @@ -49,6 +49,8 @@ pub fn deinit(self: *Self) void {

self.dir.close();
self.searcher.deinit();

if (self.pdf_contents) |contents| self.alloc.free(contents);
}

pub fn get_selected(self: *Self) !std.fs.Dir.Entry {
Expand Down

0 comments on commit 1c319c9

Please sign in to comment.