yazi/yazi-plugin/preset/components/folder.lua

90 lines
2.1 KiB
Lua
Raw Normal View History

2023-10-11 19:09:10 +03:00
Folder = {
PARENT = 0,
CURRENT = 1,
PREVIEW = 2,
2023-10-11 19:09:10 +03:00
}
function Folder:linemode(area, files)
2023-10-21 00:42:13 +03:00
local mode = cx.active.conf.linemode
if mode == "none" then
return {}
end
local lines = {}
for _, f in ipairs(files) do
2023-10-21 00:42:13 +03:00
local spans = { ui.Span(" ") }
if mode == "size" then
local size = f:size()
spans[#spans + 1] = ui.Span(size and ya.readable_size(size) or "")
2023-10-21 00:42:13 +03:00
elseif mode == "mtime" then
local time = f.cha.modified
spans[#spans + 1] = ui.Span(time and os.date("%y-%m-%d %H:%M", time // 1) or "")
2023-10-21 00:42:13 +03:00
elseif mode == "permissions" then
spans[#spans + 1] = ui.Span(f.cha:permissions() or "")
2023-10-21 00:42:13 +03:00
end
2023-10-21 08:27:26 +03:00
spans[#spans + 1] = ui.Span(" ")
2023-10-21 00:42:13 +03:00
lines[#lines + 1] = ui.Line(spans)
end
2023-12-30 18:41:41 +03:00
return ui.Paragraph(area, lines):align(ui.Paragraph.RIGHT)
2023-10-21 00:42:13 +03:00
end
2023-10-11 19:09:10 +03:00
function Folder:markers(area, markers)
if #markers == 0 or area.w * area.h == 0 then
2023-10-11 19:09:10 +03:00
return {}
end
local elements = {}
local append = function(last)
local y = math.min(area.y + last[1], area.y + area.h) - 1
local bar = ui.Bar(
2023-10-11 19:09:10 +03:00
ui.Rect {
x = math.max(0, area.x - 1),
y = y,
2023-10-11 19:09:10 +03:00
w = 1,
2024-01-13 16:13:23 +03:00
h = math.min(1 + last[2] - last[1], area.y + area.h - y),
2023-10-11 19:09:10 +03:00
},
2023-12-30 18:41:41 +03:00
ui.Bar.LEFT
2023-10-11 19:09:10 +03:00
)
if last[3] == 1 then
bar = bar:style(THEME.manager.marker_copied)
2023-10-11 19:09:10 +03:00
elseif last[3] == 2 then
bar = bar:style(THEME.manager.marker_cut)
2023-10-11 19:09:10 +03:00
elseif last[3] == 3 then
2024-02-15 15:24:20 +03:00
bar = bar:style(THEME.manager.marker_marked)
elseif last[3] == 4 then
bar = bar:style(THEME.manager.marker_selected)
2023-10-11 19:09:10 +03:00
end
elements[#elements + 1] = bar
2023-10-11 19:09:10 +03:00
end
local last = { markers[1][1], markers[1][1], markers[1][2] } -- start, end, type
for _, m in ipairs(markers) do
if m[1] - last[2] > 1 or last[3] ~= m[2] then
append(last)
last = { m[1], m[1], m[2] }
else
last[2] = m[1]
end
end
append(last)
return elements
end
2024-02-24 04:11:53 +03:00
function Folder:by_kind(kind)
if kind == self.PARENT then
return cx.active.parent
elseif kind == self.CURRENT then
return cx.active.current
elseif kind == self.PREVIEW then
return cx.active.preview.folder
end
end
2024-06-03 09:31:55 +03:00
function Folder:window(kind)
local folder = self:by_kind(kind)
return folder and folder.window
end