From 1ddbbfea711636dcb997239ea9d2b483cea16f80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Mon, 17 Feb 2025 15:20:50 +0800 Subject: [PATCH] fix: add maximum preview limit under `/proc` virtual file system (#2355) --- yazi-plugin/preset/plugins/empty.lua | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/yazi-plugin/preset/plugins/empty.lua b/yazi-plugin/preset/plugins/empty.lua index fae655e8..a0b23b0f 100644 --- a/yazi-plugin/preset/plugins/empty.lua +++ b/yazi-plugin/preset/plugins/empty.lua @@ -12,10 +12,25 @@ function M:peek(job) return self.msg(job, "Empty file") end + local i, j, lines = 0, 0, {} + local file = io.open(path, "r") + if not file then + return self.msg(job, "Failed to open file") + end + local limit = job.area.h - local i, lines = 0, {} - local ok, err = pcall(function() - for line in io.lines(path) do + while true do + local chunk = file:read(4096) + if not chunk then + break + end + + j = j + #chunk + if j > 5242880 then + return self.msg(job, "File too large") + end + + for line in chunk:gmatch("[^\n]*\n?") do i = i + 1 if i > job.skip + limit then break @@ -23,11 +38,9 @@ function M:peek(job) lines[#lines + 1] = line end end - end) + end - if not ok then - self.msg(job, err) - elseif job.skip > 0 and i < job.skip + limit then + if job.skip > 0 and i < job.skip + limit then ya.manager_emit("peek", { math.max(0, i - limit), only_if = job.file.url, upper_bound = true }) else ya.preview_widgets(job, { ui.Text(lines):area(job.area) })