Skip to content

Instantly share code, notes, and snippets.

@boydaihungst
Last active November 20, 2024 04:00
Show Gist options
  • Save boydaihungst/277884f56f661358ccd6a3d16a9b69c4 to your computer and use it in GitHub Desktop.
Save boydaihungst/277884f56f661358ccd6a3d16a9b69c4 to your computer and use it in GitHub Desktop.
yazi/init.lua
--------------------------------- Components -----------------------------------------
function Status:file_size_and_folder_childs()
local h = self._tab.current.hovered
if not h or h.cha.is_link then
return ui.Line({})
end
local size = h:size()
if size then
return ui.Line({
ui.Span(ya.readable_size(h:size() or h.cha.len)),
ui.Span(" "),
})
else
local folder = cx.active:history(h.url)
return ui.Line({
ui.Span(folder and tostring(#folder.files) or ""),
ui.Span(" "),
})
end
end
function Status:mtime()
local h = self._tab.current.hovered
if not h or h.cha.is_link then
return ui.Line({})
end
local time = math.floor(h.cha.mtime or 0)
if time == 0 then
return ui.Line("")
else
return ui.Line({
ui.Span(os.date("%Y-%m-%d %H:%M", time)),
ui.Span(" "),
})
end
end
-- Status mode
function Status:mode()
local mode = tostring(self._tab.mode):upper()
local style = self:style()
return ui.Line({
ui.Span(" " .. mode .. " "):style(style),
ui.Span(""):fg(style.bg):bg(THEME.status.separator_style.fg),
ui.Span(""):fg(THEME.status.separator_style.fg):bg("#26343F"),
})
end
---shorten string
---@param _s string string
---@param t string tail
---@param _w number max characters
---@return string
local shorten = function(_s, t, _w)
local s = _s or utf8.len(_s)
local e_with_dot = t or ""
local ellipsis = "…" .. e_with_dot
local w = _w < utf8.len(ellipsis) and utf8.len(ellipsis) or _w
local n_ellipsis = utf8.len(ellipsis) or 0
if utf8.len(s) > w then
return s:sub(1, (utf8.offset(s, w - n_ellipsis + 1) or 2) - 1) .. ellipsis
end
return s
end
function Status:name()
local h = self._tab.current.hovered
if not h then
return ui.Line({})
end
local icon = h:icon()
local file_name = h.name
local tail = h.cha.is_dir and "" or ("." .. (h.url.ext(h.url) or ""))
-- local file_extention = h.url.ext(h.url)
if h.link_to ~= nil then
file_name = " " .. tostring(h.link_to)
end
local right_width = self:children_redraw(self.RIGHT):width()
local left_lines = {}
for _, c in ipairs(self._left) do
if c[1] ~= Status.name then
left_lines[#left_lines + 1] = (type(c[1]) == "string" and self[c[1]] or c[1])(self)
end
end
local left_width = ui.Line(left_lines):width()
local max_width = math.floor(self._area.w - right_width - left_width - utf8.len("…" .. tail))
file_name = shorten(file_name, tail, max_width)
return ui.Line({
ui.Span(" " .. icon.text):style(icon.style):bg("#26343F"),
ui.Span(" " .. file_name .. " "):fg(THEME.status.separator_style.fg):bg("#26343F"),
ui.Span(""):fg("#26343F"):bg(THEME.status.separator_style.bg),
})
end
function Status:link_count()
local h = cx.active.current.hovered
if h == nil or ya.target_family() ~= "unix" then
return ui.Line({})
end
return ui.Line({
ui.Span("  "),
ui.Span(tostring(h.cha.nlink)):fg("#BCC4C9"),
ui.Span(" "),
})
end
function Status:owner_group()
local h = cx.active.current.hovered
if h == nil or ya.target_family() ~= "unix" then
return ui.Line({})
end
return ui.Line({
ui.Span(ya.user_name(h.cha.uid) or tostring(h.cha.uid)):fg("#BCC4C9"),
ui.Span(" "),
ui.Span(ya.group_name(h.cha.gid) or tostring(h.cha.gid)):fg("#BCC4C9"),
ui.Span(" "),
})
end
-- -- Status position - Right
function Status:position()
local cursor = self._tab.current.cursor
local length = #self._tab.current.files
-- local style = self:style()
return ui.Line({
-- ui.Span(THEME.status.separator_open):fg(style.bg):bg(THEME.status.separator_style.fg),
ui.Span(string.format(" %2d/%-2d ", cursor + 1, length)),
-- ui.Span(""):fg(style.bg),
})
end
-- Remove left components
Status:children_remove(1, Status.LEFT)
Status:children_remove(2, Status.LEFT)
Status:children_remove(3, Status.LEFT)
-- Remove right components
Status:children_remove(4, Status.RIGHT)
Status:children_remove(5, Status.RIGHT)
Status:children_remove(6, Status.RIGHT)
local render_status = (function()
local status_left_section = {
Status.mode,
Status.name,
Status.link_count,
}
-- Left Status
local status_right_section = {
Status.mtime,
Status.file_size_and_folder_childs,
Status.owner_group,
Status.perm,
Status.position,
}
for idx, component in ipairs(status_left_section) do
Status:children_add(component, idx, Status.LEFT)
end
for idx, component in ipairs(status_right_section) do
Status:children_add(component, idx, Status.RIGHT)
end
end)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment