mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
Simplify the code
This commit is contained in:
parent
4ca15ccff6
commit
6f442b75d8
1 changed files with 76 additions and 98 deletions
|
|
@ -9,97 +9,6 @@ function M:peek(job)
|
||||||
files, bound, err = self.list_compressed_tar({ "-p", tostring(job.file.path) }, job.skip, limit)
|
files, bound, err = self.list_compressed_tar({ "-p", tostring(job.file.path) }, job.skip, limit)
|
||||||
end
|
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
|
local limit = job.area.h
|
||||||
if err then
|
if err then
|
||||||
return ya.preview_widget(job, err)
|
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 = "" } }
|
files = { { path = job.file.url.stem, size = 0, attr = "" } }
|
||||||
end
|
end
|
||||||
|
|
||||||
local dir_stack, depth, depth_indicator, display_name = {}, 0, nil, ""
|
M.prepare_tree(files)
|
||||||
|
|
||||||
if opts.tree then
|
|
||||||
prepare_tree(files)
|
|
||||||
end
|
|
||||||
|
|
||||||
local left, right = {}, {}
|
local left, right = {}, {}
|
||||||
for _, f in ipairs(files) do
|
for _, f in ipairs(files) do
|
||||||
|
|
@ -135,9 +40,9 @@ function M:display_file_list(job, files, bound, err, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
left[#left] = ui.Line {
|
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],
|
left[#left],
|
||||||
ui.truncate(opts.tree and f.display_name or f.path, {
|
ui.truncate(f.display_name, {
|
||||||
rtl = true,
|
rtl = true,
|
||||||
max = math.max(0, job.area.w - ui.width(left[#left]) - ui.width(right[#right])),
|
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
|
return files, i, err
|
||||||
end
|
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
|
return M
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue