From f79ba67b53be784f35014a37c4d780ddbbfacc08 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Tue, 17 Oct 2023 10:48:29 +0800 Subject: [PATCH] .. --- plugin/preset/components/folder.lua | 30 +++++++++++++--------------- plugin/preset/components/manager.lua | 16 +++++++-------- plugin/preset/ui.lua | 7 ++++++- plugin/preset/utils.lua | 14 +++++++++++++ plugin/src/components/base.rs | 13 ++++++------ 5 files changed, 49 insertions(+), 31 deletions(-) diff --git a/plugin/preset/components/folder.lua b/plugin/preset/components/folder.lua index 40869223..f589095a 100644 --- a/plugin/preset/components/folder.lua +++ b/plugin/preset/components/folder.lua @@ -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 diff --git a/plugin/preset/components/manager.lua b/plugin/preset/components/manager.lua index d2dc327c..c8e11ec1 100644 --- a/plugin/preset/components/manager.lua +++ b/plugin/preset/components/manager.lua @@ -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 diff --git a/plugin/preset/ui.lua b/plugin/preset/ui.lua index 2831a1b4..563cb916 100644 --- a/plugin/preset/ui.lua +++ b/plugin/preset/ui.lua @@ -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, }), } diff --git a/plugin/preset/utils.lua b/plugin/preset/utils.lua index 05c3d4be..583aa846 100644 --- a/plugin/preset/utils.lua +++ b/plugin/preset/utils.lua @@ -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) diff --git a/plugin/src/components/base.rs b/plugin/src/components/base.rs index b7e43864..52889050 100644 --- a/plugin/src/components/base.rs +++ b/plugin/src/components/base.rs @@ -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), _ => {} } }