2023-12-26 14:48:33 +03:00
|
|
|
Header = {
|
2024-07-15 09:59:24 +03:00
|
|
|
LEFT = 0,
|
|
|
|
RIGHT = 1,
|
|
|
|
|
2024-07-12 13:32:01 +03:00
|
|
|
_id = "header",
|
2024-07-15 09:59:24 +03:00
|
|
|
_inc = 1000,
|
|
|
|
_left = {
|
|
|
|
{ "cwd", id = 1, order = 1000 },
|
|
|
|
},
|
|
|
|
_right = {
|
|
|
|
{ "count", id = 1, order = 1000 },
|
|
|
|
{ "tabs", id = 2, order = 2000 },
|
|
|
|
},
|
2023-12-26 14:48:33 +03:00
|
|
|
}
|
2023-10-11 19:09:10 +03:00
|
|
|
|
2024-07-12 13:32:01 +03:00
|
|
|
function Header:new(area, tab)
|
|
|
|
return setmetatable({
|
|
|
|
_area = area,
|
|
|
|
_tab = tab,
|
|
|
|
}, { __index = self })
|
|
|
|
end
|
|
|
|
|
2024-07-15 09:59:24 +03:00
|
|
|
function Header:cwd()
|
|
|
|
local max = self._area.w - self._right_width
|
|
|
|
if max <= 0 then
|
|
|
|
return ui.Span("")
|
|
|
|
end
|
|
|
|
|
2024-07-12 13:32:01 +03:00
|
|
|
local s = ya.readable_path(tostring(self._tab.current.cwd)) .. self:flags()
|
2024-06-22 14:01:51 +03:00
|
|
|
return ui.Span(ya.truncate(s, { max = max, rtl = true })):style(THEME.manager.cwd)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Header:flags()
|
2024-07-12 13:32:01 +03:00
|
|
|
local cwd = self._tab.current.cwd
|
|
|
|
local filter = self._tab.current.files.filter
|
2023-10-11 19:09:10 +03:00
|
|
|
|
2024-06-22 14:01:51 +03:00
|
|
|
local s = cwd.is_search and string.format(" (search: %s", cwd:frag()) or ""
|
|
|
|
if not filter then
|
|
|
|
return s == "" and s or s .. ")"
|
|
|
|
elseif s == "" then
|
|
|
|
return string.format(" (filter: %s)", tostring(filter))
|
|
|
|
else
|
|
|
|
return string.format("%s, filter: %s)", s, tostring(filter))
|
|
|
|
end
|
2023-10-11 19:09:10 +03:00
|
|
|
end
|
|
|
|
|
2024-02-17 14:03:26 +03:00
|
|
|
function Header:count()
|
2024-02-15 17:54:57 +03:00
|
|
|
local yanked = #cx.yanked
|
|
|
|
|
|
|
|
local count, style
|
|
|
|
if yanked == 0 then
|
2024-07-12 13:32:01 +03:00
|
|
|
count = #self._tab.selected
|
2024-02-15 17:54:57 +03:00
|
|
|
style = THEME.manager.count_selected
|
|
|
|
elseif cx.yanked.is_cut then
|
|
|
|
count = yanked
|
|
|
|
style = THEME.manager.count_cut
|
|
|
|
else
|
|
|
|
count = yanked
|
|
|
|
style = THEME.manager.count_copied
|
|
|
|
end
|
|
|
|
|
|
|
|
if count == 0 then
|
|
|
|
return ui.Line {}
|
|
|
|
end
|
|
|
|
|
|
|
|
return ui.Line {
|
|
|
|
ui.Span(string.format(" %d ", count)):style(style),
|
|
|
|
ui.Span(" "),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-10-11 19:09:10 +03:00
|
|
|
function Header:tabs()
|
2024-02-15 17:54:57 +03:00
|
|
|
local tabs = #cx.tabs
|
|
|
|
if tabs == 1 then
|
|
|
|
return ui.Line {}
|
|
|
|
end
|
|
|
|
|
2023-10-11 19:09:10 +03:00
|
|
|
local spans = {}
|
2024-02-15 17:54:57 +03:00
|
|
|
for i = 1, tabs do
|
2023-10-11 19:09:10 +03:00
|
|
|
local text = i
|
2023-10-13 13:49:24 +03:00
|
|
|
if THEME.manager.tab_width > 2 then
|
2024-03-08 12:54:29 +03:00
|
|
|
text = ya.truncate(text .. " " .. cx.tabs[i]:name(), { max = THEME.manager.tab_width })
|
2023-10-11 19:09:10 +03:00
|
|
|
end
|
2024-02-24 22:41:00 +03:00
|
|
|
if i == cx.tabs.idx then
|
2023-10-13 13:49:24 +03:00
|
|
|
spans[#spans + 1] = ui.Span(" " .. text .. " "):style(THEME.manager.tab_active)
|
2023-10-11 19:09:10 +03:00
|
|
|
else
|
2023-10-13 13:49:24 +03:00
|
|
|
spans[#spans + 1] = ui.Span(" " .. text .. " "):style(THEME.manager.tab_inactive)
|
2023-10-11 19:09:10 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return ui.Line(spans)
|
|
|
|
end
|
|
|
|
|
2024-07-12 13:32:01 +03:00
|
|
|
function Header:render()
|
2024-07-15 09:59:24 +03:00
|
|
|
local right = self:children_render(self.RIGHT)
|
|
|
|
self._right_width = right:width()
|
|
|
|
|
|
|
|
local left = self:children_render(self.LEFT)
|
2023-10-11 19:09:10 +03:00
|
|
|
return {
|
2024-07-12 13:32:01 +03:00
|
|
|
ui.Paragraph(self._area, { left }),
|
|
|
|
ui.Paragraph(self._area, { right }):align(ui.Paragraph.RIGHT),
|
2023-10-11 19:09:10 +03:00
|
|
|
}
|
|
|
|
end
|
2024-06-03 09:31:55 +03:00
|
|
|
|
2024-07-12 13:32:01 +03:00
|
|
|
-- Mouse events
|
2024-06-03 09:31:55 +03:00
|
|
|
function Header:click(event, up) end
|
|
|
|
|
|
|
|
function Header:scroll(event, step) end
|
|
|
|
|
|
|
|
function Header:touch(event, step) end
|
2024-07-15 09:59:24 +03:00
|
|
|
|
|
|
|
-- Children
|
|
|
|
function Header:children_add(fn, order, side)
|
|
|
|
self._inc = self._inc + 1
|
|
|
|
local children = side == self.RIGHT and self._right or self._left
|
|
|
|
|
|
|
|
children[#children + 1] = { fn, id = self._inc, order = order }
|
|
|
|
table.sort(children, function(a, b) return a.order < b.order end)
|
|
|
|
|
|
|
|
return self._inc
|
|
|
|
end
|
|
|
|
|
|
|
|
function Header:children_remove(id, side)
|
|
|
|
local children = side == self.RIGHT and self._right or self._left
|
|
|
|
for i, child in ipairs(children) do
|
|
|
|
if child.id == id then
|
|
|
|
table.remove(children, i)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Header:children_render(side)
|
|
|
|
local lines = {}
|
|
|
|
for _, c in ipairs(side == self.RIGHT and self._right or self._left) do
|
|
|
|
lines[#lines + 1] = (type(c[1]) == "string" and self[c[1]] or c[1])(self)
|
|
|
|
end
|
|
|
|
return ui.Line(lines)
|
|
|
|
end
|