2024-07-12 13:32:01 +03:00
|
|
|
Entity = {
|
|
|
|
_inc = 1000,
|
2024-07-15 09:59:24 +03:00
|
|
|
_children = {
|
2024-09-01 05:54:04 +03:00
|
|
|
{ "space", id = 1, order = 1000 },
|
|
|
|
{ "icon", id = 2, order = 2000 },
|
|
|
|
{ "prefix", id = 3, order = 3000 },
|
|
|
|
{ "highlights", id = 4, order = 4000 },
|
|
|
|
{ "found", id = 5, order = 5000 },
|
|
|
|
{ "symlink", id = 6, order = 6000 },
|
2024-07-15 09:59:24 +03:00
|
|
|
},
|
2024-07-12 13:32:01 +03:00
|
|
|
}
|
|
|
|
|
2024-07-15 09:59:24 +03:00
|
|
|
function Entity:new(file) return setmetatable({ _file = file }, { __index = self }) end
|
2024-07-12 13:32:01 +03:00
|
|
|
|
2024-09-01 05:54:04 +03:00
|
|
|
function Entity:space() return ui.Line(" ") end
|
|
|
|
|
2024-07-15 09:59:24 +03:00
|
|
|
function Entity:icon()
|
|
|
|
local icon = self._file:icon()
|
2024-07-12 13:32:01 +03:00
|
|
|
if not icon then
|
|
|
|
return ui.Line("")
|
2024-07-15 09:59:24 +03:00
|
|
|
elseif self._file:is_hovered() then
|
2024-09-01 05:54:04 +03:00
|
|
|
return ui.Line(icon.text .. " ")
|
2024-07-12 13:32:01 +03:00
|
|
|
else
|
2024-09-01 05:54:04 +03:00
|
|
|
return ui.Line(icon.text .. " "):style(icon.style)
|
2024-07-12 13:32:01 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-15 09:59:24 +03:00
|
|
|
function Entity:prefix()
|
|
|
|
local prefix = self._file:prefix() or ""
|
2024-07-12 13:32:01 +03:00
|
|
|
return ui.Line(prefix ~= "" and prefix .. "/" or "")
|
|
|
|
end
|
|
|
|
|
2024-07-15 09:59:24 +03:00
|
|
|
function Entity:highlights()
|
|
|
|
local name = self._file.name:gsub("\r", "?", 1)
|
|
|
|
local highlights = self._file:highlights()
|
2024-07-12 13:32:01 +03:00
|
|
|
if not highlights or #highlights == 0 then
|
|
|
|
return ui.Line(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
local spans, last = {}, 0
|
|
|
|
for _, h in ipairs(highlights) do
|
|
|
|
if h[1] > last then
|
|
|
|
spans[#spans + 1] = ui.Span(name:sub(last + 1, h[1]))
|
|
|
|
end
|
|
|
|
spans[#spans + 1] = ui.Span(name:sub(h[1] + 1, h[2])):style(THEME.manager.find_keyword)
|
|
|
|
last = h[2]
|
|
|
|
end
|
|
|
|
if last < #name then
|
|
|
|
spans[#spans + 1] = ui.Span(name:sub(last + 1))
|
|
|
|
end
|
|
|
|
return ui.Line(spans)
|
|
|
|
end
|
|
|
|
|
2024-07-15 09:59:24 +03:00
|
|
|
function Entity:found()
|
|
|
|
if not self._file:is_hovered() then
|
2024-07-12 13:32:01 +03:00
|
|
|
return ui.Line {}
|
|
|
|
end
|
|
|
|
|
2024-07-15 09:59:24 +03:00
|
|
|
local found = self._file:found()
|
2024-07-12 13:32:01 +03:00
|
|
|
if not found then
|
|
|
|
return ui.Line {}
|
|
|
|
end
|
|
|
|
|
|
|
|
return ui.Line {
|
|
|
|
ui.Span(" "),
|
|
|
|
ui.Span(string.format("[%d/%d]", found[1] + 1, found[2])):style(THEME.manager.find_position),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-07-15 09:59:24 +03:00
|
|
|
function Entity:symlink()
|
2024-07-12 13:32:01 +03:00
|
|
|
if not MANAGER.show_symlink then
|
|
|
|
return ui.Line {}
|
|
|
|
end
|
|
|
|
|
2024-07-15 09:59:24 +03:00
|
|
|
local to = self._file.link_to
|
2024-08-24 21:56:38 +03:00
|
|
|
return to and ui.Line(" -> " .. tostring(to)):italic() or ui.Line {}
|
2024-07-12 13:32:01 +03:00
|
|
|
end
|
|
|
|
|
2024-07-15 09:59:24 +03:00
|
|
|
function Entity:render()
|
2024-07-12 13:32:01 +03:00
|
|
|
local lines = {}
|
2024-07-15 09:59:24 +03:00
|
|
|
for _, c in ipairs(self._children) do
|
|
|
|
lines[#lines + 1] = (type(c[1]) == "string" and self[c[1]] or c[1])(self)
|
2024-07-12 13:32:01 +03:00
|
|
|
end
|
2024-10-13 20:54:03 +03:00
|
|
|
return ui.Line(lines):style(self:style())
|
2024-07-12 13:32:01 +03:00
|
|
|
end
|
|
|
|
|
2024-07-15 09:59:24 +03:00
|
|
|
function Entity:style()
|
|
|
|
local s = self._file:style()
|
|
|
|
if not self._file:is_hovered() then
|
|
|
|
return s
|
|
|
|
elseif self._file:in_preview() then
|
|
|
|
return s and s:patch(THEME.manager.preview_hovered) or THEME.manager.preview_hovered
|
|
|
|
else
|
|
|
|
return s and s:patch(THEME.manager.hovered) or THEME.manager.hovered
|
|
|
|
end
|
|
|
|
end
|
2024-07-12 13:32:01 +03:00
|
|
|
|
2024-07-15 09:59:24 +03:00
|
|
|
-- Children
|
2024-07-12 13:32:01 +03:00
|
|
|
function Entity:children_add(fn, order)
|
|
|
|
self._inc = self._inc + 1
|
|
|
|
self._children[#self._children + 1] = { fn, id = self._inc, order = order }
|
|
|
|
table.sort(self._children, function(a, b) return a.order < b.order end)
|
|
|
|
return self._inc
|
|
|
|
end
|
|
|
|
|
|
|
|
function Entity:children_remove(id)
|
|
|
|
for i, child in ipairs(self._children) do
|
|
|
|
if child.id == id then
|
|
|
|
table.remove(self._children, i)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|