mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-21 23:01:05 +00:00
refactor: remove unnecessary UI element construction (#1938)
This commit is contained in:
parent
870b0504e9
commit
99d6c81fbb
7 changed files with 40 additions and 40 deletions
|
|
@ -12,14 +12,14 @@ Entity = {
|
|||
|
||||
function Entity:new(file) return setmetatable({ _file = file }, { __index = self }) end
|
||||
|
||||
function Entity:space() return ui.Line(" ") end
|
||||
function Entity:space() return " " end
|
||||
|
||||
function Entity:icon()
|
||||
local icon = self._file:icon()
|
||||
if not icon then
|
||||
return ui.Line("")
|
||||
return ""
|
||||
elseif self._file:is_hovered() then
|
||||
return ui.Line(icon.text .. " ")
|
||||
return icon.text .. " "
|
||||
else
|
||||
return ui.Line(icon.text .. " "):style(icon.style)
|
||||
end
|
||||
|
|
@ -27,53 +27,53 @@ end
|
|||
|
||||
function Entity:prefix()
|
||||
local prefix = self._file:prefix() or ""
|
||||
return ui.Line(prefix ~= "" and prefix .. "/" or "")
|
||||
return prefix ~= "" and prefix .. "/" or ""
|
||||
end
|
||||
|
||||
function Entity:highlights()
|
||||
local name = self._file.name:gsub("\r", "?", 1)
|
||||
local highlights = self._file:highlights()
|
||||
if not highlights or #highlights == 0 then
|
||||
return ui.Line(name)
|
||||
return 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]))
|
||||
spans[#spans + 1] = 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))
|
||||
spans[#spans + 1] = name:sub(last + 1)
|
||||
end
|
||||
return ui.Line(spans)
|
||||
end
|
||||
|
||||
function Entity:found()
|
||||
if not self._file:is_hovered() then
|
||||
return ui.Line {}
|
||||
return ""
|
||||
end
|
||||
|
||||
local found = self._file:found()
|
||||
if not found then
|
||||
return ui.Line {}
|
||||
return ""
|
||||
end
|
||||
|
||||
return ui.Line {
|
||||
ui.Span(" "),
|
||||
" ",
|
||||
ui.Span(string.format("[%d/%d]", found[1] + 1, found[2])):style(THEME.manager.find_position),
|
||||
}
|
||||
end
|
||||
|
||||
function Entity:symlink()
|
||||
if not MANAGER.show_symlink then
|
||||
return ui.Line {}
|
||||
return ""
|
||||
end
|
||||
|
||||
local to = self._file.link_to
|
||||
return to and ui.Line(" -> " .. tostring(to)):italic() or ui.Line {}
|
||||
return to and ui.Span(string.format(" -> %s", to)):italic() or ""
|
||||
end
|
||||
|
||||
function Entity:redraw()
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ end
|
|||
function Header:cwd()
|
||||
local max = self._area.w - self._right_width
|
||||
if max <= 0 then
|
||||
return ui.Span("")
|
||||
return ""
|
||||
end
|
||||
|
||||
local s = ya.readable_path(tostring(self._current.cwd)) .. self:flags()
|
||||
|
|
@ -65,7 +65,7 @@ function Header:count()
|
|||
end
|
||||
|
||||
if count == 0 then
|
||||
return ui.Line {}
|
||||
return ""
|
||||
end
|
||||
|
||||
return ui.Line {
|
||||
|
|
@ -77,7 +77,7 @@ end
|
|||
function Header:tabs()
|
||||
local tabs = #cx.tabs
|
||||
if tabs == 1 then
|
||||
return ui.Line {}
|
||||
return ""
|
||||
end
|
||||
|
||||
local spans = {}
|
||||
|
|
|
|||
|
|
@ -8,58 +8,58 @@ Linemode = {
|
|||
|
||||
function Linemode:new(file) return setmetatable({ _file = file }, { __index = self }) end
|
||||
|
||||
function Linemode:space() return ui.Line(" ") end
|
||||
function Linemode:space() return " " end
|
||||
|
||||
function Linemode:solo()
|
||||
local mode = cx.active.pref.linemode
|
||||
if mode == "none" or mode == "solo" then
|
||||
return ui.Line("")
|
||||
return ""
|
||||
elseif not self[mode] then
|
||||
return ui.Line(" " .. mode)
|
||||
return " " .. mode
|
||||
else
|
||||
local line = self[mode](self)
|
||||
return line:visible() and ui.Line { ui.Span(" "), line } or line
|
||||
local line = ui.Line(self[mode](self))
|
||||
return line:visible() and ui.Line { " ", line } or line
|
||||
end
|
||||
end
|
||||
|
||||
function Linemode:size()
|
||||
local size = self._file:size()
|
||||
if size then
|
||||
return ui.Line(ya.readable_size(size))
|
||||
return ya.readable_size(size)
|
||||
else
|
||||
local folder = cx.active:history(self._file.url)
|
||||
return ui.Line(folder and tostring(#folder.files) or "")
|
||||
return folder and tostring(#folder.files) or ""
|
||||
end
|
||||
end
|
||||
|
||||
function Linemode:btime()
|
||||
local time = math.floor(self._file.cha.btime or 0)
|
||||
if time == 0 then
|
||||
return ui.Line("")
|
||||
return ""
|
||||
elseif os.date("%Y", time) == os.date("%Y") then
|
||||
return ui.Line(os.date("%m/%d %H:%M", time))
|
||||
return os.date("%m/%d %H:%M", time)
|
||||
else
|
||||
return ui.Line(os.date("%m/%d %Y", time))
|
||||
return os.date("%m/%d %Y", time)
|
||||
end
|
||||
end
|
||||
|
||||
function Linemode:mtime()
|
||||
local time = math.floor(self._file.cha.mtime or 0)
|
||||
if time == 0 then
|
||||
return ui.Line("")
|
||||
return ""
|
||||
elseif os.date("%Y", time) == os.date("%Y") then
|
||||
return ui.Line(os.date("%m/%d %H:%M", time))
|
||||
return os.date("%m/%d %H:%M", time)
|
||||
else
|
||||
return ui.Line(os.date("%m/%d %Y", time))
|
||||
return os.date("%m/%d %Y", time)
|
||||
end
|
||||
end
|
||||
|
||||
function Linemode:permissions() return ui.Line(self._file.cha:perm() or "") end
|
||||
function Linemode:permissions() return self._file.cha:perm() or "" end
|
||||
|
||||
function Linemode:owner()
|
||||
local user = self._file.cha.uid and ya.user_name(self._file.cha.uid) or self._file.cha.uid
|
||||
local group = self._file.cha.gid and ya.group_name(self._file.cha.gid) or self._file.cha.gid
|
||||
return ui.Line(string.format("%s:%s", user or "-", group or "-"))
|
||||
return string.format("%s:%s", user or "-", group or "-")
|
||||
end
|
||||
|
||||
function Linemode:redraw()
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ end
|
|||
function Status:size()
|
||||
local h = self._current.hovered
|
||||
if not h then
|
||||
return ui.Line {}
|
||||
return ""
|
||||
end
|
||||
|
||||
local style = self:style()
|
||||
|
|
@ -61,21 +61,21 @@ end
|
|||
function Status:name()
|
||||
local h = self._current.hovered
|
||||
if not h then
|
||||
return ui.Line {}
|
||||
return ""
|
||||
end
|
||||
|
||||
return ui.Line(" " .. h.name:gsub("\r", "?", 1))
|
||||
return " " .. h.name:gsub("\r", "?", 1)
|
||||
end
|
||||
|
||||
function Status:perm()
|
||||
local h = self._current.hovered
|
||||
if not h then
|
||||
return ui.Line {}
|
||||
return ""
|
||||
end
|
||||
|
||||
local perm = h.cha:perm()
|
||||
if not perm then
|
||||
return ui.Line {}
|
||||
return ""
|
||||
end
|
||||
|
||||
local spans = {}
|
||||
|
|
|
|||
|
|
@ -21,9 +21,9 @@ function M:peek()
|
|||
}):icon()
|
||||
|
||||
if icon then
|
||||
paths[#paths + 1] = ui.Line { ui.Span(" " .. icon.text .. " "):style(icon.style), ui.Span(f.path) }
|
||||
paths[#paths + 1] = ui.Line { ui.Span(" " .. icon.text .. " "):style(icon.style), f.path }
|
||||
else
|
||||
paths[#paths + 1] = ui.Line(f.path)
|
||||
paths[#paths + 1] = f.path
|
||||
end
|
||||
|
||||
if f.size > 0 then
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ function M:peek()
|
|||
if i > self.skip + limit then
|
||||
break
|
||||
elseif i > self.skip then
|
||||
lines[#lines + 1] = ui.Line(line)
|
||||
lines[#lines + 1] = line
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
|
|
|||
|
|
@ -65,8 +65,8 @@ impl UserData for Gauge {
|
|||
Ok(ud)
|
||||
});
|
||||
|
||||
methods.add_function_mut("label", |_, (ud, label): (AnyUserData, Span)| {
|
||||
ud.borrow_mut::<Self>()?.label = Some(label.0);
|
||||
methods.add_function_mut("label", |_, (ud, label): (AnyUserData, Value)| {
|
||||
ud.borrow_mut::<Self>()?.label = Some(Span::try_from(label)?.0);
|
||||
Ok(ud)
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue