From da36cd6ab8b0207984e6755fcef5c7f05c415f28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Sun, 26 Jan 2025 01:54:18 +0800 Subject: [PATCH] feat: new `cx.layer` API to determine the current UI layer (#2247) --- yazi-fm/src/context.rs | 24 ++++++++++++++ yazi-fm/src/lives/context.rs | 38 ++++++++++++++++++++++ yazi-fm/src/lives/lives.rs | 11 +------ yazi-fm/src/lives/mod.rs | 5 +-- yazi-fm/src/router.rs | 28 +++++----------- yazi-plugin/preset/components/entity.lua | 4 +-- yazi-plugin/preset/components/linemode.lua | 4 +-- yazi-plugin/preset/components/root.lua | 9 +++++ yazi-plugin/src/bindings/layer.rs | 14 ++++++++ yazi-plugin/src/bindings/mod.rs | 2 +- yazi-plugin/src/composer.rs | 20 ++++++++++++ yazi-plugin/src/config/config.rs | 34 +++++++++---------- yazi-plugin/src/elements/bar.rs | 4 +-- yazi-plugin/src/elements/border.rs | 4 +-- yazi-plugin/src/elements/clear.rs | 4 +-- yazi-plugin/src/elements/elements.rs | 21 ++++-------- yazi-plugin/src/elements/gauge.rs | 4 +-- yazi-plugin/src/elements/layout.rs | 4 +-- yazi-plugin/src/elements/line.rs | 4 +-- yazi-plugin/src/elements/list.rs | 4 +-- yazi-plugin/src/elements/pad.rs | 4 +-- yazi-plugin/src/elements/pos.rs | 4 +-- yazi-plugin/src/elements/rect.rs | 4 +-- yazi-plugin/src/elements/row.rs | 4 +-- yazi-plugin/src/elements/span.rs | 4 +-- yazi-plugin/src/elements/style.rs | 4 +-- yazi-plugin/src/elements/table.rs | 4 +-- yazi-plugin/src/elements/text.rs | 4 +-- yazi-plugin/src/fs/fs.rs | 22 ++++--------- yazi-plugin/src/lib.rs | 2 +- yazi-plugin/src/process/command.rs | 4 +-- yazi-plugin/src/pubsub/mod.rs | 22 +++++-------- yazi-plugin/src/utils/utils.rs | 22 +++++-------- 33 files changed, 199 insertions(+), 147 deletions(-) create mode 100644 yazi-fm/src/lives/context.rs create mode 100644 yazi-plugin/src/bindings/layer.rs create mode 100644 yazi-plugin/src/composer.rs diff --git a/yazi-fm/src/context.rs b/yazi-fm/src/context.rs index 122c2891..f68997a6 100644 --- a/yazi-fm/src/context.rs +++ b/yazi-fm/src/context.rs @@ -1,5 +1,6 @@ use ratatui::layout::Rect; use yazi_core::{completion::Completion, confirm::Confirm, help::Help, input::Input, manager::Manager, notify::Notify, pick::Pick, tab::{Folder, Tab}, tasks::Tasks, which::Which}; +use yazi_shared::Layer; pub struct Ctx { pub manager: Manager, @@ -39,6 +40,29 @@ impl Ctx { } None } + + #[inline] + pub fn layer(&self) -> Layer { + if self.which.visible { + Layer::Which + } else if self.completion.visible { + Layer::Completion + } else if self.help.visible { + Layer::Help + } else if self.confirm.visible { + Layer::Confirm + } else if self.input.visible { + Layer::Input + } else if self.pick.visible { + Layer::Pick + } else if self.active().spot.visible() { + Layer::Spot + } else if self.tasks.visible { + Layer::Tasks + } else { + Layer::Manager + } + } } impl Ctx { diff --git a/yazi-fm/src/lives/context.rs b/yazi-fm/src/lives/context.rs new file mode 100644 index 00000000..59c6e462 --- /dev/null +++ b/yazi-fm/src/lives/context.rs @@ -0,0 +1,38 @@ +use std::ops::Deref; + +use mlua::{AnyUserData, IntoLua, MetaMethod, UserData, Value}; + +use super::Lives; + +pub(super) struct Ctx { + inner: *const crate::Ctx, +} + +impl Deref for Ctx { + type Target = crate::Ctx; + + fn deref(&self) -> &Self::Target { unsafe { &*self.inner } } +} + +impl Ctx { + #[inline] + pub(super) fn make(inner: &crate::Ctx) -> mlua::Result { + Lives::scoped_userdata(Self { inner }) + } +} + +impl UserData for Ctx { + fn add_methods>(methods: &mut M) { + methods.add_meta_method(MetaMethod::Index, |lua, me, key: mlua::String| { + match key.as_bytes().as_ref() { + b"active" => super::Tab::make(me.active())?, + b"tabs" => super::Tabs::make(&me.manager.tabs)?, + b"tasks" => super::Tasks::make(&me.tasks)?, + b"yanked" => super::Yanked::make(&me.manager.yanked)?, + b"layer" => return yazi_plugin::bindings::Layer::from(me.layer()).into_lua(lua), + _ => return Ok(Value::Nil), + } + .into_lua(lua) + }); + } +} diff --git a/yazi-fm/src/lives/lives.rs b/yazi-fm/src/lives/lives.rs index 8a323253..1a7ef8f8 100644 --- a/yazi-fm/src/lives/lives.rs +++ b/yazi-fm/src/lives/lives.rs @@ -27,16 +27,7 @@ impl Lives { }); LUA.set_named_registry_value("cx", scope.create_any_userdata_ref(cx)?)?; - LUA.globals().raw_set( - "cx", - LUA.create_table_from([ - ("active", super::Tab::make(cx.active())?), - ("tabs", super::Tabs::make(&cx.manager.tabs)?), - ("tasks", super::Tasks::make(&cx.tasks)?), - ("yanked", super::Yanked::make(&cx.manager.yanked)?), - ])?, - )?; - + LUA.globals().raw_set("cx", super::Ctx::make(cx)?)?; f() }); diff --git a/yazi-fm/src/lives/mod.rs b/yazi-fm/src/lives/mod.rs index 9ad65f0f..6fcc4036 100644 --- a/yazi-fm/src/lives/mod.rs +++ b/yazi-fm/src/lives/mod.rs @@ -1,6 +1,3 @@ #![allow(clippy::module_inception)] -yazi_macro::mod_flat!( - preference file files filter finder folder iter lives mode preview selected tab tabs - tasks yanked -); +yazi_macro::mod_flat!(context file files filter finder folder iter lives mode preference preview selected tab tabs tasks yanked); diff --git a/yazi-fm/src/router.rs b/yazi-fm/src/router.rs index 28f94fc5..e337e97d 100644 --- a/yazi-fm/src/router.rs +++ b/yazi-fm/src/router.rs @@ -15,10 +15,8 @@ impl<'a> Router<'a> { #[inline] pub(super) fn route(&mut self, key: Key) -> bool { let cx = &mut self.app.cx; + let layer = cx.layer(); - if cx.which.visible { - return cx.which.type_(key); - } if cx.help.visible && cx.help.type_(&key) { return true; } @@ -26,22 +24,14 @@ impl<'a> Router<'a> { return true; } - if cx.completion.visible { - self.matches(Layer::Completion, key) || self.matches(Layer::Input, key) - } else if cx.help.visible { - self.matches(Layer::Help, key) - } else if cx.input.visible { - self.matches(Layer::Input, key) - } else if cx.confirm.visible { - self.matches(Layer::Confirm, key) - } else if cx.pick.visible { - self.matches(Layer::Pick, key) - } else if cx.active().spot.visible() { - self.matches(Layer::Spot, key) - } else if cx.tasks.visible { - self.matches(Layer::Tasks, key) - } else { - self.matches(Layer::Manager, key) + use Layer as L; + match layer { + L::App => unreachable!(), + L::Manager | L::Tasks | L::Spot | L::Pick | L::Input | L::Confirm | L::Help => { + self.matches(layer, key) + } + L::Completion => self.matches(L::Completion, key) || self.matches(L::Input, key), + L::Which => cx.which.type_(key), } } diff --git a/yazi-plugin/preset/components/entity.lua b/yazi-plugin/preset/components/entity.lua index b885daf4..0dc385df 100644 --- a/yazi-plugin/preset/components/entity.lua +++ b/yazi-plugin/preset/components/entity.lua @@ -1,7 +1,7 @@ Entity = { _inc = 1000, _children = { - { "space", id = 1, order = 1000 }, + { "spacer", id = 1, order = 1000 }, { "icon", id = 2, order = 2000 }, { "prefix", id = 3, order = 3000 }, { "highlights", id = 4, order = 4000 }, @@ -12,7 +12,7 @@ Entity = { function Entity:new(file) return setmetatable({ _file = file }, { __index = self }) end -function Entity:space() return " " end +function Entity:spacer() return " " end function Entity:icon() local icon = self._file:icon() diff --git a/yazi-plugin/preset/components/linemode.lua b/yazi-plugin/preset/components/linemode.lua index 3b9c3f7a..676569e0 100644 --- a/yazi-plugin/preset/components/linemode.lua +++ b/yazi-plugin/preset/components/linemode.lua @@ -2,13 +2,13 @@ Linemode = { _inc = 1000, _children = { { "solo", id = 1, order = 1000 }, - { "space", id = 2, order = 2000 }, + { "spacer", id = 2, order = 2000 }, }, } function Linemode:new(file) return setmetatable({ _file = file }, { __index = self }) end -function Linemode:space() return " " end +function Linemode:spacer() return " " end function Linemode:solo() local mode = cx.active.pref.linemode diff --git a/yazi-plugin/preset/components/root.lua b/yazi-plugin/preset/components/root.lua index 8f2079b1..6d9bbbfa 100644 --- a/yazi-plugin/preset/components/root.lua +++ b/yazi-plugin/preset/components/root.lua @@ -48,16 +48,25 @@ end -- Mouse events function Root:click(event, up) + if tostring(cx.layer) ~= "manager" then + return + end local c = ya.child_at(ui.Rect { x = event.x, y = event.y }, self:reflow()) return c and c:click(event, up) end function Root:scroll(event, step) + if tostring(cx.layer) ~= "manager" then + return + end local c = ya.child_at(ui.Rect { x = event.x, y = event.y }, self:reflow()) return c and c:scroll(event, step) end function Root:touch(event, step) + if tostring(cx.layer) ~= "manager" then + return + end local c = ya.child_at(ui.Rect { x = event.x, y = event.y }, self:reflow()) return c and c:touch(event, step) end diff --git a/yazi-plugin/src/bindings/layer.rs b/yazi-plugin/src/bindings/layer.rs new file mode 100644 index 00000000..e666ab95 --- /dev/null +++ b/yazi-plugin/src/bindings/layer.rs @@ -0,0 +1,14 @@ +use mlua::{MetaMethod, UserData}; + +#[derive(Clone, Copy)] +pub struct Layer(yazi_shared::Layer); + +impl From for Layer { + fn from(event: yazi_shared::Layer) -> Self { Self(event) } +} + +impl UserData for Layer { + fn add_methods>(methods: &mut M) { + methods.add_meta_method(MetaMethod::ToString, |_, me, ()| Ok(me.0.to_string())); + } +} diff --git a/yazi-plugin/src/bindings/mod.rs b/yazi-plugin/src/bindings/mod.rs index 913a52ff..450fc82b 100644 --- a/yazi-plugin/src/bindings/mod.rs +++ b/yazi-plugin/src/bindings/mod.rs @@ -1,3 +1,3 @@ #![allow(clippy::module_inception)] -yazi_macro::mod_flat!(bindings cha chan icon image input mouse permit range window); +yazi_macro::mod_flat!(bindings cha chan icon image input layer mouse permit range window); diff --git a/yazi-plugin/src/composer.rs b/yazi-plugin/src/composer.rs new file mode 100644 index 00000000..82c419eb --- /dev/null +++ b/yazi-plugin/src/composer.rs @@ -0,0 +1,20 @@ +use mlua::{IntoLua, Lua, MetaMethod, Table, Value}; + +pub struct Composer; + +impl Composer { + pub fn make(lua: &Lua, cap: usize, f: F) -> mlua::Result + where + F: Fn(&Lua, &[u8]) -> mlua::Result + 'static, + { + let index = lua.create_function(move |lua, (ts, key): (Table, mlua::String)| { + let v = f(lua, key.as_bytes().as_ref())?; + ts.raw_set(key, v.clone())?; + Ok(v) + })?; + + let tbl = lua.create_table_with_capacity(0, cap)?; + tbl.set_metatable(Some(lua.create_table_from([(MetaMethod::Index.name(), index)])?)); + tbl.into_lua(lua) + } +} diff --git a/yazi-plugin/src/config/config.rs b/yazi-plugin/src/config/config.rs index ba217e06..0dfa2fae 100644 --- a/yazi-plugin/src/config/config.rs +++ b/yazi-plugin/src/config/config.rs @@ -1,8 +1,9 @@ -use mlua::{IntoLua, Lua, LuaSerdeExt, MetaMethod, SerializeOptions, Table, Value}; +use mlua::{IntoLua, Lua, LuaSerdeExt, SerializeOptions, Value}; use yazi_boot::BOOT; use yazi_config::{MANAGER, PREVIEW, THEME}; use super::Plugin; +use crate::Composer; pub const SER_OPTS: SerializeOptions = SerializeOptions::new().serialize_none_to_null(false).serialize_unit_to_null(false); @@ -35,24 +36,19 @@ impl<'a> Config<'a> { } pub fn install_plugin(self) -> mlua::Result { - let index = self.lua.create_function(|lua, (ts, key): (Table, mlua::String)| { - let value = match key.as_bytes().as_ref() { - b"fetchers" => Plugin::fetchers(lua)?, - b"spotter" => Plugin::spotter(lua)?, - b"preloaders" => Plugin::preloaders(lua)?, - b"previewer" => Plugin::previewer(lua)?, - _ => return Ok(Value::Nil), - } - .into_lua(lua)?; - - ts.raw_set(key, value.clone())?; - Ok(value) - })?; - - let fetcher = self.lua.create_table()?; - fetcher.set_metatable(Some(self.lua.create_table_from([(MetaMethod::Index.name(), index)])?)); - - self.lua.globals().raw_set("PLUGIN", fetcher)?; + self.lua.globals().raw_set( + "PLUGIN", + Composer::make(self.lua, 5, |lua, name| { + match name { + b"fetchers" => Plugin::fetchers(lua)?, + b"spotter" => Plugin::spotter(lua)?, + b"preloaders" => Plugin::preloaders(lua)?, + b"previewer" => Plugin::previewer(lua)?, + _ => return Ok(Value::Nil), + } + .into_lua(lua) + })?, + )?; Ok(self) } } diff --git a/yazi-plugin/src/elements/bar.rs b/yazi-plugin/src/elements/bar.rs index 63e403ab..cbfc9f89 100644 --- a/yazi-plugin/src/elements/bar.rs +++ b/yazi-plugin/src/elements/bar.rs @@ -1,4 +1,4 @@ -use mlua::{Lua, Table, UserData}; +use mlua::{Lua, MetaMethod, Table, UserData}; use ratatui::widgets::Borders; use super::Area; @@ -28,7 +28,7 @@ impl Bar { ("ALL", Borders::ALL.bits()), ])?; - bar.set_metatable(Some(lua.create_table_from([("__call", new)])?)); + bar.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); Ok(bar) } diff --git a/yazi-plugin/src/elements/border.rs b/yazi-plugin/src/elements/border.rs index b85897d8..c4d8c364 100644 --- a/yazi-plugin/src/elements/border.rs +++ b/yazi-plugin/src/elements/border.rs @@ -1,4 +1,4 @@ -use mlua::{Lua, Table, UserData, Value}; +use mlua::{Lua, MetaMethod, Table, UserData, Value}; use ratatui::widgets::{Borders, Widget}; use super::Area; @@ -49,7 +49,7 @@ impl Border { ("QUADRANT_OUTSIDE", QUADRANT_OUTSIDE), ])?; - border.set_metatable(Some(lua.create_table_from([("__call", new)])?)); + border.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); Ok(border) } diff --git a/yazi-plugin/src/elements/clear.rs b/yazi-plugin/src/elements/clear.rs index 7d01e6b2..f7eec593 100644 --- a/yazi-plugin/src/elements/clear.rs +++ b/yazi-plugin/src/elements/clear.rs @@ -1,6 +1,6 @@ use std::sync::atomic::{AtomicBool, Ordering}; -use mlua::{Lua, Table, UserData}; +use mlua::{Lua, MetaMethod, Table, UserData}; use yazi_adapter::ADAPTOR; use super::Area; @@ -17,7 +17,7 @@ impl Clear { let new = lua.create_function(|_, (_, area): (Table, Area)| Ok(Clear { area }))?; let clear = lua.create_table()?; - clear.set_metatable(Some(lua.create_table_from([("__call", new)])?)); + clear.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); Ok(clear) } diff --git a/yazi-plugin/src/elements/elements.rs b/yazi-plugin/src/elements/elements.rs index d4d8d1b6..b508137e 100644 --- a/yazi-plugin/src/elements/elements.rs +++ b/yazi-plugin/src/elements/elements.rs @@ -1,11 +1,12 @@ -use mlua::{AnyUserData, IntoLua, Lua, MetaMethod, Table, Value}; +use mlua::{AnyUserData, IntoLua, Lua, Table, Value}; use tracing::error; use super::Renderable; +use crate::Composer; -pub fn compose(lua: &Lua) -> mlua::Result { - let index = lua.create_function(|lua, (ts, key): (Table, mlua::String)| { - let value = match key.as_bytes().as_ref() { +pub fn compose(lua: &Lua) -> mlua::Result { + Composer::make(lua, 20, |lua, key| { + match key { b"Bar" => super::Bar::compose(lua)?, b"Border" => super::Border::compose(lua)?, b"Clear" => super::Clear::compose(lua)?, @@ -26,16 +27,8 @@ pub fn compose(lua: &Lua) -> mlua::Result
{ b"Text" => super::Text::compose(lua)?, _ => return Ok(Value::Nil), } - .into_lua(lua)?; - - ts.raw_set(key, value.clone())?; - Ok(value) - })?; - - let ui = lua.create_table_with_capacity(0, 20)?; - ui.set_metatable(Some(lua.create_table_from([(MetaMethod::Index.name(), index)])?)); - - Ok(ui) + .into_lua(lua) + }) } pub fn render_once(widgets: Table, buf: &mut ratatui::buffer::Buffer, trans: F) diff --git a/yazi-plugin/src/elements/gauge.rs b/yazi-plugin/src/elements/gauge.rs index 5418fe0c..15a5cd16 100644 --- a/yazi-plugin/src/elements/gauge.rs +++ b/yazi-plugin/src/elements/gauge.rs @@ -1,4 +1,4 @@ -use mlua::{ExternalError, Lua, Table, UserData, UserDataMethods, Value}; +use mlua::{ExternalError, Lua, MetaMethod, Table, UserData, UserDataMethods, Value}; use ratatui::widgets::Widget; use super::{Area, Span}; @@ -19,7 +19,7 @@ impl Gauge { let new = lua.create_function(|_, _: Table| Ok(Gauge::default()))?; let gauge = lua.create_table()?; - gauge.set_metatable(Some(lua.create_table_from([("__call", new)])?)); + gauge.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); Ok(gauge) } diff --git a/yazi-plugin/src/elements/layout.rs b/yazi-plugin/src/elements/layout.rs index 6b42b52d..cc75ca8a 100644 --- a/yazi-plugin/src/elements/layout.rs +++ b/yazi-plugin/src/elements/layout.rs @@ -1,4 +1,4 @@ -use mlua::{AnyUserData, Lua, Table, UserData, UserDataMethods}; +use mlua::{AnyUserData, Lua, MetaMethod, Table, UserData, UserDataMethods}; use super::{Constraint, Rect}; @@ -17,7 +17,7 @@ impl Layout { let new = lua.create_function(|_, _: Table| Ok(Self::default()))?; let layout = lua.create_table_from([("HORIZONTAL", HORIZONTAL), ("VERTICAL", VERTICAL)])?; - layout.set_metatable(Some(lua.create_table_from([("__call", new)])?)); + layout.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); Ok(layout) } diff --git a/yazi-plugin/src/elements/line.rs b/yazi-plugin/src/elements/line.rs index e3b8ace2..ea8ae592 100644 --- a/yazi-plugin/src/elements/line.rs +++ b/yazi-plugin/src/elements/line.rs @@ -1,7 +1,7 @@ use std::mem; use ansi_to_tui::IntoText; -use mlua::{AnyUserData, ExternalError, ExternalResult, FromLua, IntoLua, Lua, Table, UserData, UserDataMethods, Value}; +use mlua::{AnyUserData, ExternalError, ExternalResult, FromLua, IntoLua, Lua, MetaMethod, Table, UserData, UserDataMethods, Value}; use unicode_width::UnicodeWidthChar; use super::Span; @@ -41,7 +41,7 @@ impl Line { ("RIGHT", RIGHT.into_lua(lua)?), ])?; - line.set_metatable(Some(lua.create_table_from([("__call", new)])?)); + line.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); Ok(line) } } diff --git a/yazi-plugin/src/elements/list.rs b/yazi-plugin/src/elements/list.rs index 6aa228c5..7607a282 100644 --- a/yazi-plugin/src/elements/list.rs +++ b/yazi-plugin/src/elements/list.rs @@ -1,4 +1,4 @@ -use mlua::{ExternalError, Lua, Table, UserData, Value}; +use mlua::{ExternalError, Lua, MetaMethod, Table, UserData, Value}; use ratatui::widgets::Widget; use super::{Area, Text}; @@ -25,7 +25,7 @@ impl List { })?; let list = lua.create_table()?; - list.set_metatable(Some(lua.create_table_from([("__call", new)])?)); + list.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); Ok(list) } diff --git a/yazi-plugin/src/elements/pad.rs b/yazi-plugin/src/elements/pad.rs index 112aab8f..141ccaf7 100644 --- a/yazi-plugin/src/elements/pad.rs +++ b/yazi-plugin/src/elements/pad.rs @@ -1,6 +1,6 @@ use std::ops::{Add, AddAssign, Deref}; -use mlua::{FromLua, Lua, Table, UserData}; +use mlua::{FromLua, Lua, MetaMethod, Table, UserData}; #[derive(Clone, Copy, Default, FromLua)] pub struct Pad(ratatui::widgets::Padding); @@ -51,7 +51,7 @@ impl Pad { ), ])?; - pad.set_metatable(Some(lua.create_table_from([("__call", new)])?)); + pad.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); Ok(pad) } } diff --git a/yazi-plugin/src/elements/pos.rs b/yazi-plugin/src/elements/pos.rs index 1e299db6..bafd6195 100644 --- a/yazi-plugin/src/elements/pos.rs +++ b/yazi-plugin/src/elements/pos.rs @@ -1,6 +1,6 @@ use std::{ops::Deref, str::FromStr}; -use mlua::{AnyUserData, ExternalResult, Lua, Table, UserData}; +use mlua::{AnyUserData, ExternalResult, Lua, MetaMethod, Table, UserData}; use super::Pad; @@ -50,7 +50,7 @@ impl Pos { let new = lua.create_function(|_, (_, t): (Table, Table)| Self::try_from(t))?; let position = lua.create_table()?; - position.set_metatable(Some(lua.create_table_from([("__call", new)])?)); + position.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); Ok(position) } diff --git a/yazi-plugin/src/elements/rect.rs b/yazi-plugin/src/elements/rect.rs index 6978d8e6..cf78df1f 100644 --- a/yazi-plugin/src/elements/rect.rs +++ b/yazi-plugin/src/elements/rect.rs @@ -1,6 +1,6 @@ use std::ops::Deref; -use mlua::{FromLua, Lua, Table, UserData}; +use mlua::{FromLua, Lua, MetaMethod, Table, UserData}; use super::Pad; @@ -36,7 +36,7 @@ impl Rect { let rect = lua.create_table_from([("default", Self(ratatui::layout::Rect::default()))])?; - rect.set_metatable(Some(lua.create_table_from([("__call", new)])?)); + rect.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); Ok(rect) } diff --git a/yazi-plugin/src/elements/row.rs b/yazi-plugin/src/elements/row.rs index 8a74b76d..39922b7c 100644 --- a/yazi-plugin/src/elements/row.rs +++ b/yazi-plugin/src/elements/row.rs @@ -1,4 +1,4 @@ -use mlua::{AnyUserData, FromLua, Lua, Table, UserData}; +use mlua::{AnyUserData, FromLua, Lua, MetaMethod, Table, UserData}; use super::Cell; @@ -18,7 +18,7 @@ impl Row { })?; let row = lua.create_table()?; - row.set_metatable(Some(lua.create_table_from([("__call", new)])?)); + row.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); Ok(row) } diff --git a/yazi-plugin/src/elements/span.rs b/yazi-plugin/src/elements/span.rs index 147de23e..139c4383 100644 --- a/yazi-plugin/src/elements/span.rs +++ b/yazi-plugin/src/elements/span.rs @@ -1,4 +1,4 @@ -use mlua::{ExternalError, FromLua, Lua, Table, UserData, UserDataMethods, Value}; +use mlua::{ExternalError, FromLua, Lua, MetaMethod, Table, UserData, UserDataMethods, Value}; use unicode_width::UnicodeWidthChar; const EXPECTED: &str = "expected a string or Span"; @@ -11,7 +11,7 @@ impl Span { let new = lua.create_function(|_, (_, value): (Table, Value)| Span::try_from(value))?; let span = lua.create_table()?; - span.set_metatable(Some(lua.create_table_from([("__call", new)])?)); + span.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); Ok(span) } diff --git a/yazi-plugin/src/elements/style.rs b/yazi-plugin/src/elements/style.rs index fd2273fd..11411d23 100644 --- a/yazi-plugin/src/elements/style.rs +++ b/yazi-plugin/src/elements/style.rs @@ -1,6 +1,6 @@ use std::str::FromStr; -use mlua::{AnyUserData, ExternalError, ExternalResult, Lua, Table, UserData, UserDataMethods, Value}; +use mlua::{AnyUserData, ExternalError, ExternalResult, Lua, MetaMethod, Table, UserData, UserDataMethods, Value}; use yazi_shared::theme::Color; #[derive(Clone, Copy, Default)] @@ -11,7 +11,7 @@ impl Style { let new = lua.create_function(|_, (_, value): (Table, Value)| Self::try_from(value))?; let style = lua.create_table()?; - style.set_metatable(Some(lua.create_table_from([("__call", new)])?)); + style.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); Ok(style) } diff --git a/yazi-plugin/src/elements/table.rs b/yazi-plugin/src/elements/table.rs index e55ec8b1..80861e4c 100644 --- a/yazi-plugin/src/elements/table.rs +++ b/yazi-plugin/src/elements/table.rs @@ -1,4 +1,4 @@ -use mlua::{AnyUserData, Lua, UserData, Value}; +use mlua::{AnyUserData, Lua, MetaMethod, UserData, Value}; use ratatui::widgets::StatefulWidget; use super::{Area, Row}; @@ -36,7 +36,7 @@ impl Table { })?; let table = lua.create_table()?; - table.set_metatable(Some(lua.create_table_from([("__call", new)])?)); + table.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); Ok(table) } diff --git a/yazi-plugin/src/elements/text.rs b/yazi-plugin/src/elements/text.rs index 6f66b34e..2f905015 100644 --- a/yazi-plugin/src/elements/text.rs +++ b/yazi-plugin/src/elements/text.rs @@ -1,7 +1,7 @@ use std::mem; use ansi_to_tui::IntoText; -use mlua::{ExternalError, ExternalResult, FromLua, IntoLua, Lua, Table, UserData, Value}; +use mlua::{ExternalError, ExternalResult, FromLua, IntoLua, Lua, MetaMethod, Table, UserData, Value}; use ratatui::widgets::Widget; use super::{Area, Line, Span}; @@ -48,7 +48,7 @@ impl Text { ("WRAP_TRIM", WRAP_TRIM.into_lua(lua)?), ])?; - text.set_metatable(Some(lua.create_table_from([("__call", new)])?)); + text.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); Ok(text) } diff --git a/yazi-plugin/src/fs/fs.rs b/yazi-plugin/src/fs/fs.rs index e22a2711..d9eab082 100644 --- a/yazi-plugin/src/fs/fs.rs +++ b/yazi-plugin/src/fs/fs.rs @@ -1,13 +1,13 @@ use globset::GlobBuilder; -use mlua::{ExternalError, ExternalResult, Function, IntoLua, IntoLuaMulti, Lua, MetaMethod, Table, Value}; +use mlua::{ExternalError, ExternalResult, Function, IntoLua, IntoLuaMulti, Lua, Table, Value}; use tokio::fs; use yazi_fs::{mounts::PARTITIONS, remove_dir_clean}; -use crate::{Error, bindings::{Cast, Cha}, file::File, url::{Url, UrlRef}}; +use crate::{Composer, Error, bindings::{Cast, Cha}, file::File, url::{Url, UrlRef}}; -pub fn compose(lua: &Lua) -> mlua::Result
{ - let index = lua.create_function(|lua, (ts, key): (Table, mlua::String)| { - let value = match key.as_bytes().as_ref() { +pub fn compose(lua: &Lua) -> mlua::Result { + Composer::make(lua, 10, |lua, key| { + match key { b"cwd" => cwd(lua)?, b"cha" => cha(lua)?, b"write" => write(lua)?, @@ -18,16 +18,8 @@ pub fn compose(lua: &Lua) -> mlua::Result
{ b"partitions" => partitions(lua)?, _ => return Ok(Value::Nil), } - .into_lua(lua)?; - - ts.raw_set(key, value.clone())?; - Ok(value) - })?; - - let fs = lua.create_table_with_capacity(0, 10)?; - fs.set_metatable(Some(lua.create_table_from([(MetaMethod::Index.name(), index)])?)); - - Ok(fs) + .into_lua(lua) + }) } fn cwd(lua: &Lua) -> mlua::Result { diff --git a/yazi-plugin/src/lib.rs b/yazi-plugin/src/lib.rs index 81e3d743..ca4bb83a 100644 --- a/yazi-plugin/src/lib.rs +++ b/yazi-plugin/src/lib.rs @@ -4,7 +4,7 @@ mod macros; yazi_macro::mod_pub!(bindings config elements external file fs isolate loader process pubsub url utils); -yazi_macro::mod_flat!(clipboard error lua runtime); +yazi_macro::mod_flat!(clipboard composer error lua runtime); pub fn init() -> anyhow::Result<()> { CLIPBOARD.with(<_>::default); diff --git a/yazi-plugin/src/process/command.rs b/yazi-plugin/src/process/command.rs index d2db0255..574dc637 100644 --- a/yazi-plugin/src/process/command.rs +++ b/yazi-plugin/src/process/command.rs @@ -1,6 +1,6 @@ use std::process::Stdio; -use mlua::{AnyUserData, ExternalError, IntoLuaMulti, Lua, Table, UserData, Value}; +use mlua::{AnyUserData, ExternalError, IntoLuaMulti, Lua, MetaMethod, Table, UserData, Value}; use tokio::process::{ChildStderr, ChildStdin, ChildStdout}; use super::{Child, output::Output}; @@ -30,7 +30,7 @@ impl Command { ("INHERIT", INHERIT), ])?; - command.set_metatable(Some(lua.create_table_from([("__call", new)])?)); + command.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); lua.globals().raw_set("Command", command) } diff --git a/yazi-plugin/src/pubsub/mod.rs b/yazi-plugin/src/pubsub/mod.rs index 9b9eb913..af519c39 100644 --- a/yazi-plugin/src/pubsub/mod.rs +++ b/yazi-plugin/src/pubsub/mod.rs @@ -1,12 +1,14 @@ #![allow(clippy::module_inception)] -use mlua::{IntoLua, Lua, MetaMethod, Table, Value}; +use mlua::{IntoLua, Lua, Value}; + +use crate::Composer; yazi_macro::mod_flat!(pubsub); -pub(super) fn compose(lua: &Lua) -> mlua::Result
{ - let index = lua.create_function(|lua, (ts, key): (Table, mlua::String)| { - let value = match key.as_bytes().as_ref() { +pub(super) fn compose(lua: &Lua) -> mlua::Result { + Composer::make(lua, 10, |lua, key| { + match key { b"pub" => Pubsub::pub_(lua)?, b"pub_to" => Pubsub::pub_to(lua)?, b"sub" => Pubsub::sub(lua)?, @@ -15,14 +17,6 @@ pub(super) fn compose(lua: &Lua) -> mlua::Result
{ b"unsub_remote" => Pubsub::unsub_remote(lua)?, _ => return Ok(Value::Nil), } - .into_lua(lua)?; - - ts.raw_set(key, value.clone())?; - Ok(value) - })?; - - let ps = lua.create_table_with_capacity(0, 10)?; - ps.set_metatable(Some(lua.create_table_from([(MetaMethod::Index.name(), index)])?)); - - Ok(ps) + .into_lua(lua) + }) } diff --git a/yazi-plugin/src/utils/utils.rs b/yazi-plugin/src/utils/utils.rs index af7d699f..71c02ffa 100644 --- a/yazi-plugin/src/utils/utils.rs +++ b/yazi-plugin/src/utils/utils.rs @@ -1,10 +1,12 @@ -use mlua::{IntoLua, Lua, MetaMethod, Table, Value}; +use mlua::{IntoLua, Lua, Value}; + +use crate::Composer; pub(super) struct Utils; -pub fn compose(lua: &Lua, isolate: bool) -> mlua::Result
{ - let index = lua.create_function(move |lua, (ts, key): (Table, mlua::String)| { - let value = match key.as_bytes().as_ref() { +pub fn compose(lua: &Lua, isolate: bool) -> mlua::Result { + Composer::make(lua, 40, move |lua, key| { + match key { // App b"hide" => Utils::hide(lua)?, @@ -80,14 +82,6 @@ pub fn compose(lua: &Lua, isolate: bool) -> mlua::Result
{ _ => return Ok(Value::Nil), } - .into_lua(lua)?; - - ts.raw_set(key, value.clone())?; - Ok(value) - })?; - - let ya = lua.create_table_with_capacity(0, 40)?; - ya.set_metatable(Some(lua.create_table_from([(MetaMethod::Index.name(), index)])?)); - - Ok(ya) + .into_lua(lua) + }) }