refactor: remove unnecessary UI element construction (#1938)

This commit is contained in:
三咲雅 · Misaki Masa 2024-11-23 14:42:26 +08:00 committed by GitHub
parent 870b0504e9
commit 99d6c81fbb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 40 additions and 40 deletions

View file

@ -12,14 +12,14 @@ Entity = {
function Entity:new(file) return setmetatable({ _file = file }, { __index = self }) end 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() function Entity:icon()
local icon = self._file:icon() local icon = self._file:icon()
if not icon then if not icon then
return ui.Line("") return ""
elseif self._file:is_hovered() then elseif self._file:is_hovered() then
return ui.Line(icon.text .. " ") return icon.text .. " "
else else
return ui.Line(icon.text .. " "):style(icon.style) return ui.Line(icon.text .. " "):style(icon.style)
end end
@ -27,53 +27,53 @@ end
function Entity:prefix() function Entity:prefix()
local prefix = self._file:prefix() or "" local prefix = self._file:prefix() or ""
return ui.Line(prefix ~= "" and prefix .. "/" or "") return prefix ~= "" and prefix .. "/" or ""
end end
function Entity:highlights() function Entity:highlights()
local name = self._file.name:gsub("\r", "?", 1) local name = self._file.name:gsub("\r", "?", 1)
local highlights = self._file:highlights() local highlights = self._file:highlights()
if not highlights or #highlights == 0 then if not highlights or #highlights == 0 then
return ui.Line(name) return name
end end
local spans, last = {}, 0 local spans, last = {}, 0
for _, h in ipairs(highlights) do for _, h in ipairs(highlights) do
if h[1] > last then 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 end
spans[#spans + 1] = ui.Span(name:sub(h[1] + 1, h[2])):style(THEME.manager.find_keyword) spans[#spans + 1] = ui.Span(name:sub(h[1] + 1, h[2])):style(THEME.manager.find_keyword)
last = h[2] last = h[2]
end end
if last < #name then if last < #name then
spans[#spans + 1] = ui.Span(name:sub(last + 1)) spans[#spans + 1] = name:sub(last + 1)
end end
return ui.Line(spans) return ui.Line(spans)
end end
function Entity:found() function Entity:found()
if not self._file:is_hovered() then if not self._file:is_hovered() then
return ui.Line {} return ""
end end
local found = self._file:found() local found = self._file:found()
if not found then if not found then
return ui.Line {} return ""
end end
return ui.Line { return ui.Line {
ui.Span(" "), " ",
ui.Span(string.format("[%d/%d]", found[1] + 1, found[2])):style(THEME.manager.find_position), ui.Span(string.format("[%d/%d]", found[1] + 1, found[2])):style(THEME.manager.find_position),
} }
end end
function Entity:symlink() function Entity:symlink()
if not MANAGER.show_symlink then if not MANAGER.show_symlink then
return ui.Line {} return ""
end end
local to = self._file.link_to 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 end
function Entity:redraw() function Entity:redraw()

View file

@ -24,7 +24,7 @@ end
function Header:cwd() function Header:cwd()
local max = self._area.w - self._right_width local max = self._area.w - self._right_width
if max <= 0 then if max <= 0 then
return ui.Span("") return ""
end end
local s = ya.readable_path(tostring(self._current.cwd)) .. self:flags() local s = ya.readable_path(tostring(self._current.cwd)) .. self:flags()
@ -65,7 +65,7 @@ function Header:count()
end end
if count == 0 then if count == 0 then
return ui.Line {} return ""
end end
return ui.Line { return ui.Line {
@ -77,7 +77,7 @@ end
function Header:tabs() function Header:tabs()
local tabs = #cx.tabs local tabs = #cx.tabs
if tabs == 1 then if tabs == 1 then
return ui.Line {} return ""
end end
local spans = {} local spans = {}

View file

@ -8,58 +8,58 @@ Linemode = {
function Linemode:new(file) return setmetatable({ _file = file }, { __index = self }) end 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() function Linemode:solo()
local mode = cx.active.pref.linemode local mode = cx.active.pref.linemode
if mode == "none" or mode == "solo" then if mode == "none" or mode == "solo" then
return ui.Line("") return ""
elseif not self[mode] then elseif not self[mode] then
return ui.Line(" " .. mode) return " " .. mode
else else
local line = self[mode](self) local line = ui.Line(self[mode](self))
return line:visible() and ui.Line { ui.Span(" "), line } or line return line:visible() and ui.Line { " ", line } or line
end end
end end
function Linemode:size() function Linemode:size()
local size = self._file:size() local size = self._file:size()
if size then if size then
return ui.Line(ya.readable_size(size)) return ya.readable_size(size)
else else
local folder = cx.active:history(self._file.url) 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
end end
function Linemode:btime() function Linemode:btime()
local time = math.floor(self._file.cha.btime or 0) local time = math.floor(self._file.cha.btime or 0)
if time == 0 then if time == 0 then
return ui.Line("") return ""
elseif os.date("%Y", time) == os.date("%Y") then 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 else
return ui.Line(os.date("%m/%d %Y", time)) return os.date("%m/%d %Y", time)
end end
end end
function Linemode:mtime() function Linemode:mtime()
local time = math.floor(self._file.cha.mtime or 0) local time = math.floor(self._file.cha.mtime or 0)
if time == 0 then if time == 0 then
return ui.Line("") return ""
elseif os.date("%Y", time) == os.date("%Y") then 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 else
return ui.Line(os.date("%m/%d %Y", time)) return os.date("%m/%d %Y", time)
end end
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() function Linemode:owner()
local user = self._file.cha.uid and ya.user_name(self._file.cha.uid) or self._file.cha.uid 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 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 end
function Linemode:redraw() function Linemode:redraw()

View file

@ -48,7 +48,7 @@ end
function Status:size() function Status:size()
local h = self._current.hovered local h = self._current.hovered
if not h then if not h then
return ui.Line {} return ""
end end
local style = self:style() local style = self:style()
@ -61,21 +61,21 @@ end
function Status:name() function Status:name()
local h = self._current.hovered local h = self._current.hovered
if not h then if not h then
return ui.Line {} return ""
end end
return ui.Line(" " .. h.name:gsub("\r", "?", 1)) return " " .. h.name:gsub("\r", "?", 1)
end end
function Status:perm() function Status:perm()
local h = self._current.hovered local h = self._current.hovered
if not h then if not h then
return ui.Line {} return ""
end end
local perm = h.cha:perm() local perm = h.cha:perm()
if not perm then if not perm then
return ui.Line {} return ""
end end
local spans = {} local spans = {}

View file

@ -21,9 +21,9 @@ function M:peek()
}):icon() }):icon()
if icon then 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 else
paths[#paths + 1] = ui.Line(f.path) paths[#paths + 1] = f.path
end end
if f.size > 0 then if f.size > 0 then

View file

@ -16,7 +16,7 @@ function M:peek()
if i > self.skip + limit then if i > self.skip + limit then
break break
elseif i > self.skip then elseif i > self.skip then
lines[#lines + 1] = ui.Line(line) lines[#lines + 1] = line
end end
end end
end) end)

View file

@ -65,8 +65,8 @@ impl UserData for Gauge {
Ok(ud) Ok(ud)
}); });
methods.add_function_mut("label", |_, (ud, label): (AnyUserData, Span)| { methods.add_function_mut("label", |_, (ud, label): (AnyUserData, Value)| {
ud.borrow_mut::<Self>()?.label = Some(label.0); ud.borrow_mut::<Self>()?.label = Some(Span::try_from(label)?.0);
Ok(ud) Ok(ud)
}); });