Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove unnecessary UI element construction #1938

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions yazi-plugin/preset/components/entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,68 @@ 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
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()
Expand Down
6 changes: 3 additions & 3 deletions yazi-plugin/preset/components/header.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -65,7 +65,7 @@ function Header:count()
end

if count == 0 then
return ui.Line {}
return ""
end

return ui.Line {
Expand All @@ -77,7 +77,7 @@ end
function Header:tabs()
local tabs = #cx.tabs
if tabs == 1 then
return ui.Line {}
return ""
end

local spans = {}
Expand Down
30 changes: 15 additions & 15 deletions yazi-plugin/preset/components/linemode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions yazi-plugin/preset/components/status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 = {}
Expand Down
4 changes: 2 additions & 2 deletions yazi-plugin/preset/plugins/archive.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion yazi-plugin/preset/plugins/empty.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions yazi-plugin/src/elements/gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});

Expand Down
Loading