From 6f442b75d86c2daf211d95d7c7923f26d8ed14c7 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Fri, 9 Jan 2026 12:16:17 +0800 Subject: [PATCH] Simplify the code --- yazi-plugin/preset/plugins/archive.lua | 174 +++++++++++-------------- 1 file changed, 76 insertions(+), 98 deletions(-) diff --git a/yazi-plugin/preset/plugins/archive.lua b/yazi-plugin/preset/plugins/archive.lua index 53151ebf..eea6d827 100644 --- a/yazi-plugin/preset/plugins/archive.lua +++ b/yazi-plugin/preset/plugins/archive.lua @@ -9,97 +9,6 @@ function M:peek(job) files, bound, err = self.list_compressed_tar({ "-p", tostring(job.file.path) }, job.skip, limit) end - return self:display_file_list(job, files, bound, err) -end - --- Modifies files in-place to prepare for tree display. --- All parent directories will be included, if not already present. --- All returned files will have a "display_name" and a "depth" field. -local function prepare_tree(files) - if #files == 0 then - return - end - - local result = {} - local dir_stack, previous_path = {}, nil - - -- Initialize dir_stack with the first file's - local first_file_url, components = Url(files[1].path).path, {} - while true do - first_file_url = first_file_url.parent - if first_file_url then - components[#components + 1] = first_file_url - else - break - end - end - for i, comp in ipairs(components) do - table.insert(files, 1, { - path = tostring(comp), - size = 0, - attr = "", - is_dir = true, - display_name = comp.name, - depth = #components - i, - }) - end - - local path, parent, dirs_to_add, dir_path = nil, nil, nil, nil - for i, f in ipairs(files) do - path = Url(f.path).path - - while #dir_stack > 0 and not path:starts_with(dir_stack[#dir_stack]) do - dir_stack[#dir_stack] = nil - end - - f.display_name = path.name - f.depth = #dir_stack - - if f.is_dir then - dir_stack[#dir_stack + 1] = path - elseif not previous_path or (path.parent ~= previous_path.parent) then - parent = path.parent - dirs_to_add = {} - while parent and (not dir_stack[#dir_stack] or parent ~= dir_stack[#dir_stack]) do - dirs_to_add[#dirs_to_add + 1] = parent - parent = parent.parent - end - for j = #dirs_to_add, 1, -1 do - dir_path = dirs_to_add[j] - result[#result + 1] = { - path = tostring(dir_path), - size = 0, - attr = "", - is_dir = true, - display_name = dir_path.name, - depth = #dir_stack, - } - dir_stack[#dir_stack + 1] = dir_path - end - f.depth = #dir_stack - end - - result[#result + 1] = f - previous_path = path - end - - for i = #result, 1, -1 do - files[i] = result[i] - end -end - --- Lists the files in a previewer. Handles errors and empty archives. --- Can be called by other plugins, to implement their own archive peek logic. --- @param job Job --- @param files table List of files in the archive - { { path=string, size=integer, attr=string, [ is_dir = boolean ] }, ... } --- @param bound integer Last bound reached --- @param err Error? Error encountered during listing --- @param opts table Additional options - { tree = boolean (default: true), tree_indent = string (default: " │") } -function M:display_file_list(job, files, bound, err, opts) - opts = opts or {} - opts.tree = opts.tree ~= false - opts.tree_indent = opts.tree_indent or " │" - local limit = job.area.h if err then return ya.preview_widget(job, err) @@ -109,11 +18,7 @@ function M:display_file_list(job, files, bound, err, opts) files = { { path = job.file.url.stem, size = 0, attr = "" } } end - local dir_stack, depth, depth_indicator, display_name = {}, 0, nil, "" - - if opts.tree then - prepare_tree(files) - end + M.prepare_tree(files) local left, right = {}, {} for _, f in ipairs(files) do @@ -135,9 +40,9 @@ function M:display_file_list(job, files, bound, err, opts) end left[#left] = ui.Line { - opts.tree and ui.Span(string.rep(opts.tree_indent, f.depth)) or ui.Span(""), + ui.Span(string.rep(" │", f.depth)), left[#left], - ui.truncate(opts.tree and f.display_name or f.path, { + ui.truncate(f.display_name, { rtl = true, max = math.max(0, job.area.w - ui.width(left[#left]) - ui.width(right[#right])), }), @@ -379,4 +284,77 @@ function M.parse_7z_slt(child, skip, limit) return files, i, err end +function M.prepare_tree(files) + if #files == 0 then + return + end + + local result = {} + local dir_stack, previous_path = {}, nil + + -- Initialize dir_stack with the first file's + local first_file_url, components = Url(files[1].path).path, {} + while true do + first_file_url = first_file_url.parent + if first_file_url then + components[#components + 1] = first_file_url + else + break + end + end + for i, comp in ipairs(components) do + table.insert(files, 1, { + path = tostring(comp), + size = 0, + attr = "", + is_dir = true, + display_name = comp.name, + depth = #components - i, + }) + end + + local path, parent, dirs_to_add, dir_path = nil, nil, nil, nil + for _, f in ipairs(files) do + path = Url(f.path).path + + while #dir_stack > 0 and not path:starts_with(dir_stack[#dir_stack]) do + dir_stack[#dir_stack] = nil + end + + f.display_name = path.name + f.depth = #dir_stack + + if f.is_dir then + dir_stack[#dir_stack + 1] = path + elseif not previous_path or (path.parent ~= previous_path.parent) then + parent = path.parent + dirs_to_add = {} + while parent and (not dir_stack[#dir_stack] or parent ~= dir_stack[#dir_stack]) do + dirs_to_add[#dirs_to_add + 1] = parent + parent = parent.parent + end + for j = #dirs_to_add, 1, -1 do + dir_path = dirs_to_add[j] + result[#result + 1] = { + path = tostring(dir_path), + size = 0, + attr = "", + is_dir = true, + display_name = dir_path.name, + depth = #dir_stack, + } + dir_stack[#dir_stack + 1] = dir_path + end + f.depth = #dir_stack + end + + result[#result + 1] = f + previous_path = path + end + + for i = #result, 1, -1 do + files[i] = result[i] + end +end + return M