Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Jul 12, 2024
1 parent 8e1ceca commit 4ec235f
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 128 deletions.
4 changes: 2 additions & 2 deletions yazi-plugin/preset/components/current.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ function Current:render()

local items = {}
for _, f in ipairs(files) do
items[#items + 1] = ui.ListItem(File:children_render(f)):style(File:style(f))
items[#items + 1] = ui.ListItem(Entity:render(f)):style(Entity:style(f))
end

return {
ui.List(self._area, items),
Folder:linemode(self._area, files),
ui.Paragraph(self._area, Linemode:render(files)):align(ui.Paragraph.RIGHT),
}
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
File = {
Entity = {
_inc = 1000,
}

function File:style(file)
function Entity:style(file)
local style = file:style()
if not file:is_hovered() then
return style
Expand All @@ -13,22 +13,7 @@ function File:style(file)
end
end

function File:marker(file)
local yanked = file:is_yanked()
if yanked ~= 0 then
return yanked -- 1: copied, 2: cut
end

local marked = file:is_marked()
if marked == 1 then
return 3 -- 3: marked
elseif marked == 0 and file:is_selected() then
return 4 -- 4: selected
end
return 0
end

function File:icon(file)
function Entity:icon(file)
local icon = file:icon()
if not icon then
return ui.Line("")
Expand All @@ -39,12 +24,12 @@ function File:icon(file)
end
end

function File:prefix(file)
function Entity:prefix(file)
local prefix = file:prefix() or ""
return ui.Line(prefix ~= "" and prefix .. "/" or "")
end

function File:highlights(file)
function Entity:highlights(file)
local name = file.name:gsub("\r", "?", 1)
local highlights = file:highlights()
if not highlights or #highlights == 0 then
Expand All @@ -65,7 +50,7 @@ function File:highlights(file)
return ui.Line(spans)
end

function File:found(file)
function Entity:found(file)
if not file:is_hovered() then
return ui.Line {}
end
Expand All @@ -81,7 +66,7 @@ function File:found(file)
}
end

function File:symlink(file)
function Entity:symlink(file)
if not MANAGER.show_symlink then
return ui.Line {}
end
Expand All @@ -90,35 +75,35 @@ function File:symlink(file)
return ui.Line(to and { ui.Span(" -> " .. tostring(to)):italic() } or {})
end

function Entity:render(file)
local lines = {}
for _, child in ipairs(self._children) do
lines[#lines + 1] = child[1](self, file)
end
return ui.Line(lines)
end

-- Initialize children
File._children = {
{ File.icon, id = 1, order = 1000 },
{ File.prefix, id = 2, order = 2000 },
{ File.highlights, id = 3, order = 3000 },
{ File.found, id = 4, order = 4000 },
{ File.symlink, id = 5, order = 5000 },
Entity._children = {
{ Entity.icon, id = 1, order = 1000 },
{ Entity.prefix, id = 2, order = 2000 },
{ Entity.highlights, id = 3, order = 3000 },
{ Entity.found, id = 4, order = 4000 },
{ Entity.symlink, id = 5, order = 5000 },
}

function File:children_add(fn, order)
function Entity:children_add(fn, order)
self._inc = self._inc + 1
self._children[#self._children + 1] = { fn, id = self._inc, order = order }
table.sort(self._children, function(a, b) return a.order < b.order end)
return self._inc
end

function File:children_remove(id)
function Entity:children_remove(id)
for i, child in ipairs(self._children) do
if child.id == id then
table.remove(self._children, i)
break
end
end
end

function File:children_render(file)
local lines = {}
for _, child in ipairs(self._children) do
lines[#lines + 1] = child[1](self, file)
end
return ui.Line(lines)
end
73 changes: 0 additions & 73 deletions yazi-plugin/preset/components/folder.lua

This file was deleted.

77 changes: 77 additions & 0 deletions yazi-plugin/preset/components/linemode.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
Linemode = {
_inc = 1000,
}

function Linemode:solo(file)
local mode = cx.active.conf.linemode
if mode == "none" or mode == "solo" then
return ui.Line("")
end

if not self[mode] then
return ui.Line(" " .. mode .. " ")
end

return ui.Line {
ui.Span(" "),
self[mode](self, file),
ui.Span(" "),
}
end

function Linemode:size(file)
local size = file:size()
return ui.Line(size and ya.readable_size(size) or "")
end

function Linemode:mtime(file)
local time = file.cha.modified
return ui.Line(time and os.date("%y-%m-%d %H:%M", time // 1) or "")
end

function Linemode:permissions(file) return ui.Line(file.cha:permissions() or "") end

function Linemode:owner(file)
if not ya.user_name then
return ui.Line("")
else
return ui.Line(ya.user_name(file.cha.uid) .. ":" .. ya.group_name(file.cha.gid))
end
end

function Linemode:render(files)
local lines = {}
for _, f in ipairs(files) do
lines[#lines + 1] = self:children_render(f)
end
return lines
end

-- Initialize children
Linemode._children = {
{ Linemode.solo, id = 1, order = 1000 },
}

function Linemode:children_add(fn, order)
self._inc = self._inc + 1
self._children[#self._children + 1] = { fn, id = self._inc, order = order }
table.sort(self._children, function(a, b) return a.order < b.order end)
return self._inc
end

function Linemode:children_remove(id)
for i, child in ipairs(self._children) do
if child.id == id then
table.remove(self._children, i)
break
end
end
end

function Linemode:children_render(file)
local lines = {}
for _, child in ipairs(self._children) do
lines[#lines + 1] = child[1](self, file)
end
return ui.Line(lines)
end
54 changes: 51 additions & 3 deletions yazi-plugin/preset/components/marker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,57 @@ function Marker:new(area, folder)
end

function Marker:render()
return {
ui.Bar(self._area, ui.Bar.LEFT):symbol(THEME.manager.border_symbol):style(THEME.manager.border_style),
}
if self._area.w * self._area.h == 0 then
return {}
elseif not self._folder or #self._folder.window == 0 then
return {}
end

local elements = {}
local append = function(last)
if not last[3] then
return
end

local y = math.min(self._area.y + last[1], self._area.y + self._area.h) - 1
local rect = ui.Rect {
x = math.max(0, self._area.x - 1),
y = y,
w = 1,
h = math.min(1 + last[2] - last[1], self._area.y + self._area.h - y),
}
elements[#elements + 1] = ui.Bar(rect, ui.Bar.LEFT):style(last[3])
end

local last = { 0, 0, nil } -- start, end, style
for i, f in ipairs(self._folder.window) do
local style = self:style(f)
if i - last[2] > 1 or last[3] ~= style then
append(last)
last = { i, i, style }
else
last[2] = i
end
end

append(last)
return elements
end

function Marker:style(file)
local marked = file:is_marked()
if marked == 1 then
return THEME.manager.marker_marked
elseif marked == 0 and file:is_selected() then
return THEME.manager.marker_selected
end

local yanked = file:is_yanked()
if yanked == 1 then
return THEME.manager.marker_copied
elseif yanked == 2 then
return THEME.manager.marker_cut
end
end

-- Mouse events
Expand Down
2 changes: 1 addition & 1 deletion yazi-plugin/preset/components/parent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function Parent:render()

local items = {}
for _, f in ipairs(self._folder.window) do
items[#items + 1] = ui.ListItem(File:children_render(f)):style(File:style(f))
items[#items + 1] = ui.ListItem(Entity:render(f)):style(Entity:style(f))
end

return {
Expand Down
10 changes: 7 additions & 3 deletions yazi-plugin/preset/components/rail.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ function Rail:new(chunks, tab)
end

function Rail:build()
self._bars = {
ui.Bar(self._chunks[1], ui.Bar.RIGHT):symbol(THEME.manager.border_symbol):style(THEME.manager.border_style),
ui.Bar(self._chunks[3], ui.Bar.LEFT):symbol(THEME.manager.border_symbol):style(THEME.manager.border_style),
}
self._children = {
Marker:new(self._chunks[1], self._tab.parent),
Marker:new(self._chunks[2], self._tab.current),
Expand All @@ -19,11 +23,11 @@ function Rail:build()
end

function Rail:render()
local children = {}
local result = self._bars
for _, child in ipairs(self._children) do
children = ya.list_merge(children, ya.render_with(child))
result = ya.list_merge(result, ya.render_with(child))
end
return children
return result
end

-- Mouse events
Expand Down
6 changes: 1 addition & 5 deletions yazi-plugin/preset/components/tab.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ function Tab:build()
local chunks = self:layout()

self._children = {
-- Rail
Rail:new(chunks, self._tab):build(),
-- Parent
Rail:new(chunks, self._tab),
Parent:new(chunks[1]:padding(ui.Padding.x(1)), self._tab),
-- Current
Current:new(chunks[2], self._tab),
-- Preview
Preview:new(chunks[3]:padding(ui.Padding.x(1)), self._tab),
}
return self
Expand Down
Loading

0 comments on commit 4ec235f

Please sign in to comment.