This commit is contained in:
sxyazi 2023-10-17 10:48:29 +08:00
parent 29ae9e06dd
commit f79ba67b53
No known key found for this signature in database
5 changed files with 49 additions and 31 deletions

View file

@ -1,17 +1,15 @@
Folder = {
Kind = {
Parent = 0,
Current = 1,
Preview = 2,
},
PARENT = 0,
CURRENT = 1,
PREVIEW = 2,
}
function Folder:by_kind(kind)
if kind == self.Kind.Parent then
if kind == self.PARENT then
return cx.active.parent
elseif kind == self.Kind.Current then
elseif kind == self.CURRENT then
return cx.active.current
elseif kind == self.Kind.Preview then
elseif kind == self.PREVIEW then
return cx.active.preview.folder
end
end
@ -23,14 +21,14 @@ function Folder:markers(area, markers)
local elements = {}
local append = function(last)
local p = ui.Paragraph(
local p = ui.Bar(
ui.Rect {
x = math.max(1, area.x) - 1,
y = area.y + last[1] - 1,
w = 1,
h = 1 + last[2] - last[1],
},
{}
ui.Position.LEFT
)
if last[3] == 1 then
@ -86,7 +84,7 @@ function Folder:highlighted_name(file)
end
function Folder:parent(area)
local folder = self:by_kind(self.Kind.Parent)
local folder = self:by_kind(self.PARENT)
if folder == nil then
return {}
end
@ -109,7 +107,7 @@ end
function Folder:current(area)
local markers = {}
local items = {}
for i, f in ipairs(self:by_kind(self.Kind.Current).window) do
for i, f in ipairs(self:by_kind(self.CURRENT).window) do
local name = self:highlighted_name(f)
-- Highlight hovered file
@ -134,7 +132,7 @@ function Folder:current(area)
end
function Folder:preview(area)
local folder = self:by_kind(self.Kind.Preview)
local folder = self:by_kind(self.PREVIEW)
if folder == nil then
return {}
end
@ -154,11 +152,11 @@ function Folder:preview(area)
end
function Folder:render(area, args)
if args.kind == self.Kind.Parent then
if args.kind == self.PARENT then
return self:parent(area)
elseif args.kind == self.Kind.Current then
elseif args.kind == self.CURRENT then
return self:current(area)
elseif args.kind == self.Kind.Preview then
elseif args.kind == self.PREVIEW then
return self:preview(area)
end
end

View file

@ -10,16 +10,16 @@ function Manager:render(area)
})
:split(area)
return {
-- Parent
table.unpack(Folder:render(chunks[1]:padding(ui.Padding.x(1)), { kind = Folder.Kind.Parent })),
-- Current
table.unpack(Folder:render(chunks[2], { kind = Folder.Kind.Current })),
-- Preview
ui.Base(chunks[3]:padding(ui.Padding.x(1)), "Preview"),
return utils.flat {
-- Borders
ui.Bar(chunks[1], ui.Position.RIGHT):symbol(THEME.manager.border_symbol):style(THEME.manager.border_style),
ui.Bar(chunks[3], ui.Position.LEFT):symbol(THEME.manager.border_symbol):style(THEME.manager.border_style),
-- Parent
Folder:render(chunks[1]:padding(ui.Padding.x(1)), { kind = Folder.PARENT }),
-- Current
Folder:render(chunks[2], { kind = Folder.CURRENT }),
-- Preview
ui.Base(chunks[3]:padding(ui.Padding.x(1)), ui.Base.PREVIEW),
}
end

View file

@ -16,6 +16,11 @@ ui = {
LEFT = 4,
},
Base = setmetatable({
PREVIEW = 0,
}, {
__call = function(_, ...) return ui.Base.new(...) end,
}),
Padding = setmetatable({
left = function(left) return ui.Padding.new(left, 0, 0, 0) end,
right = function(right) return ui.Padding.new(0, right, 0, 0) end,
@ -24,7 +29,7 @@ ui = {
x = function(x) return ui.Padding.new(x, x, 0, 0) end,
y = function(y) return ui.Padding.new(0, 0, y, y) end,
}, {
__call = function(...) return ui.Padding.new(...) end,
__call = function(_, ...) return ui.Padding.new(...) end,
}),
}

View file

@ -1,5 +1,19 @@
utils = utils or {}
function utils.flat(t)
local r = {}
for _, v in ipairs(t) do
if type(v) == "table" then
for _, v2 in ipairs(utils.flat(v)) do
r[#r + 1] = v2
end
else
r[#r + 1] = v
end
end
return r
end
function utils.basename(str) return string.gsub(str, "(.*[/\\])(.*)", "%2") end
function utils.readable_size(size)

View file

@ -7,21 +7,22 @@ use crate::{layout::Rect, GLOBALS, LUA};
pub struct Base {
area: ratatui::layout::Rect,
name: String,
kind: u8,
}
impl Base {
pub(crate) fn install() -> mlua::Result<()> {
let ui: Table = GLOBALS.get("ui")?;
ui.set(
"Base",
LUA.create_function(|_, (area, name): (Rect, String)| Ok(Self { area: area.0, name }))?,
let base: Table = ui.get("Base")?;
base.set(
"new",
LUA.create_function(|_, (area, kind): (Rect, u8)| Ok(Self { area: area.0, kind }))?,
)
}
pub fn render(self, cx: &core::Ctx, buf: &mut ratatui::buffer::Buffer) {
match self.name.as_ref() {
"Preview" => super::Preview::new(cx).render(self.area, buf),
match self.kind {
0 => super::Preview::new(cx).render(self.area, buf),
_ => {}
}
}