diff --git a/yazi-fm/src/app/app.rs b/yazi-fm/src/app/app.rs index 867242d6..b175d65e 100644 --- a/yazi-fm/src/app/app.rs +++ b/yazi-fm/src/app/app.rs @@ -54,8 +54,8 @@ impl App { #[inline] fn dispatch(&mut self, event: Event) -> Result<()> { match event { - Event::Call(exec, layer) => self.dispatch_call(exec, layer), - Event::Seq(execs, layer) => self.dispatch_seq(execs, layer), + Event::Call(cmd, layer) => self.dispatch_call(cmd, layer), + Event::Seq(cmds, layer) => self.dispatch_seq(cmds, layer), Event::Render => self.dispatch_render(), Event::Key(key) => self.dispatch_key(key), Event::Resize => self.resize(()), @@ -69,12 +69,12 @@ impl App { fn dispatch_call(&mut self, cmd: Cmd, layer: Layer) { Executor::new(self).execute(cmd, layer); } #[inline] - fn dispatch_seq(&mut self, mut execs: VecDeque, layer: Layer) { - if let Some(exec) = execs.pop_front() { - Executor::new(self).execute(exec, layer); + fn dispatch_seq(&mut self, mut cmds: VecDeque, layer: Layer) { + if let Some(cmd) = cmds.pop_front() { + Executor::new(self).execute(cmd, layer); } - if !execs.is_empty() { - emit!(Seq(execs, layer)); + if !cmds.is_empty() { + emit!(Seq(cmds, layer)); } } diff --git a/yazi-fm/src/lives/folder.rs b/yazi-fm/src/lives/folder.rs index 59ec9b86..6181bf41 100644 --- a/yazi-fm/src/lives/folder.rs +++ b/yazi-fm/src/lives/folder.rs @@ -1,6 +1,6 @@ use mlua::{AnyUserData, IntoLua, Lua, MetaMethod, UserDataFields, UserDataMethods, Value}; use yazi_config::{LAYOUT, THEME}; -use yazi_plugin::{bindings::{Cast, File, Icon, Range, Url}, elements::Style}; +use yazi_plugin::{bindings::{Cast, File, Icon, Range}, elements::Style, url::Url}; use yazi_shared::MIME_DIR; use super::{CtxRef, FolderRef}; diff --git a/yazi-fm/src/lives/lives.rs b/yazi-fm/src/lives/lives.rs index de3ca8c4..4b64f572 100644 --- a/yazi-fm/src/lives/lives.rs +++ b/yazi-fm/src/lives/lives.rs @@ -11,10 +11,6 @@ pub(crate) struct Lives; impl Lives { pub(crate) fn register() -> mlua::Result<()> { - yazi_plugin::bindings::Cha::register(&LUA)?; - yazi_plugin::bindings::Icon::register(&LUA)?; - yazi_plugin::bindings::Url::register(&LUA)?; - super::Active::register(&LUA)?; super::Folder::register(&LUA)?; super::Tabs::register(&LUA)?; diff --git a/yazi-plugin/src/bindings/file.rs b/yazi-plugin/src/bindings/file.rs index 20dc6095..79f285b6 100644 --- a/yazi-plugin/src/bindings/file.rs +++ b/yazi-plugin/src/bindings/file.rs @@ -1,6 +1,7 @@ use mlua::{AnyUserData, Lua, UserDataFields, UserDataRef, UserDataRegistry}; -use super::{Cast, Cha, Url}; +use super::{Cast, Cha}; +use crate::url::Url; pub type FileRef<'lua> = UserDataRef<'lua, yazi_shared::fs::File>; diff --git a/yazi-plugin/src/bindings/mod.rs b/yazi-plugin/src/bindings/mod.rs index ed97e037..0ff28e9e 100644 --- a/yazi-plugin/src/bindings/mod.rs +++ b/yazi-plugin/src/bindings/mod.rs @@ -5,7 +5,6 @@ mod cha; mod file; mod icon; mod range; -mod url; mod window; #[allow(unused_imports)] @@ -14,7 +13,6 @@ pub use cha::*; pub use file::*; pub use icon::*; pub use range::*; -pub use url::*; pub use window::*; pub trait Cast { diff --git a/yazi-plugin/src/elements/elements.rs b/yazi-plugin/src/elements/elements.rs index c9238bd5..5db045dc 100644 --- a/yazi-plugin/src/elements/elements.rs +++ b/yazi-plugin/src/elements/elements.rs @@ -2,7 +2,7 @@ use mlua::{AnyUserData, Lua, Table}; use crate::cast_to_renderable; -pub fn init(lua: &Lua) -> mlua::Result<()> { +pub fn pour(lua: &Lua) -> mlua::Result<()> { let ui: Table = lua.create_table()?; // Register diff --git a/yazi-plugin/src/fs/fs.rs b/yazi-plugin/src/fs/fs.rs index 54ac9321..4d0f1c25 100644 --- a/yazi-plugin/src/fs/fs.rs +++ b/yazi-plugin/src/fs/fs.rs @@ -1,7 +1,7 @@ use mlua::{IntoLua, Lua, Value}; use tokio::fs; -use crate::bindings::{Cast, Cha, UrlRef}; +use crate::{bindings::{Cast, Cha}, url::UrlRef}; pub fn install(lua: &Lua) -> mlua::Result<()> { lua.globals().set( diff --git a/yazi-plugin/src/isolate/isolate.rs b/yazi-plugin/src/isolate/isolate.rs index e0102ee5..ad966f5d 100644 --- a/yazi-plugin/src/isolate/isolate.rs +++ b/yazi-plugin/src/isolate/isolate.rs @@ -7,8 +7,9 @@ pub fn slim_lua() -> mlua::Result { // Base bindings::Cha::register(&lua)?; - bindings::Url::register(&lua)?; bindings::File::register(&lua, |_| {})?; + crate::url::pour(&lua)?; + crate::fs::install(&lua)?; crate::process::install(&lua)?; crate::utils::install(&lua)?; @@ -16,9 +17,11 @@ pub fn slim_lua() -> mlua::Result { // Elements let ui = lua.create_table()?; + elements::Line::install(&lua, &ui)?; elements::Paragraph::install(&lua, &ui)?; - elements::Rect::install(&lua, &ui)?; elements::Rect::register(&lua)?; + elements::Rect::install(&lua, &ui)?; + elements::Span::install(&lua, &ui)?; lua.globals().set("ui", ui)?; Ok(lua) diff --git a/yazi-plugin/src/lib.rs b/yazi-plugin/src/lib.rs index a5f0dc24..0c1ffddf 100644 --- a/yazi-plugin/src/lib.rs +++ b/yazi-plugin/src/lib.rs @@ -11,6 +11,7 @@ mod loader; mod opt; mod plugin; pub mod process; +pub mod url; pub mod utils; pub use cast::*; diff --git a/yazi-plugin/src/plugin.rs b/yazi-plugin/src/plugin.rs index a8b7cda1..ec4cc732 100644 --- a/yazi-plugin/src/plugin.rs +++ b/yazi-plugin/src/plugin.rs @@ -16,7 +16,10 @@ pub fn init() { lua.load(include_str!("../preset/inspect/inspect.lua")).exec()?; lua.load(include_str!("../preset/state.lua")).exec()?; lua.load(include_str!("../preset/ya.lua")).exec()?; - crate::elements::init(lua)?; + crate::bindings::Cha::register(lua)?; + crate::bindings::Icon::register(lua)?; + crate::elements::pour(lua)?; + crate::url::pour(lua)?; // Components lua.load(include_str!("../preset/components/current.lua")).exec()?; diff --git a/yazi-plugin/src/url/mod.rs b/yazi-plugin/src/url/mod.rs new file mode 100644 index 00000000..81119723 --- /dev/null +++ b/yazi-plugin/src/url/mod.rs @@ -0,0 +1,12 @@ +#![allow(clippy::module_inception)] + +mod url; + +pub use url::*; + +pub fn pour(lua: &mlua::Lua) -> mlua::Result<()> { + url::Url::register(lua)?; + url::Url::install(lua)?; + + Ok(()) +} diff --git a/yazi-plugin/src/bindings/url.rs b/yazi-plugin/src/url/url.rs similarity index 84% rename from yazi-plugin/src/bindings/url.rs rename to yazi-plugin/src/url/url.rs index 34ddd1e8..36755cae 100644 --- a/yazi-plugin/src/bindings/url.rs +++ b/yazi-plugin/src/url/url.rs @@ -1,6 +1,6 @@ use mlua::{AnyUserData, Lua, MetaMethod, UserDataFields, UserDataMethods, UserDataRef}; -use super::Cast; +use crate::bindings::Cast; pub type UrlRef<'lua> = UserDataRef<'lua, yazi_shared::fs::Url>; @@ -31,6 +31,15 @@ impl Url { }); }) } + + pub fn install(lua: &Lua) -> mlua::Result<()> { + lua.globals().set( + "Url", + lua.create_function(|lua, url: mlua::String| { + Self::cast(lua, yazi_shared::fs::Url::from(url.to_str()?)) + })?, + ) + } } impl> Cast for Url { diff --git a/yazi-plugin/src/utils/cache.rs b/yazi-plugin/src/utils/cache.rs index 94aa4aac..ba73a680 100644 --- a/yazi-plugin/src/utils/cache.rs +++ b/yazi-plugin/src/utils/cache.rs @@ -3,7 +3,7 @@ use mlua::{Lua, Table}; use yazi_config::PREVIEW; use super::Utils; -use crate::bindings::{Cast, FileRef, Url}; +use crate::{bindings::{Cast, FileRef}, url::Url}; impl Utils { pub(super) fn cache(lua: &Lua, ya: &Table) -> mlua::Result<()> { diff --git a/yazi-plugin/src/utils/call.rs b/yazi-plugin/src/utils/call.rs index adeb237b..02e0a1f8 100644 --- a/yazi-plugin/src/utils/call.rs +++ b/yazi-plugin/src/utils/call.rs @@ -11,18 +11,28 @@ impl Utils { let mut args = vec![]; let mut named = BTreeMap::new(); for result in t.pairs::() { - let (k, Value::String(v)) = result? else { - return Err("invalid value in exec".into_lua_err()); - }; - + let (k, v) = result?; match k { Value::Integer(_) => { - args.push(v.to_string_lossy().into_owned()); + args.push(match v { + Value::Integer(i) => i.to_string(), + Value::Number(n) => n.to_string(), + Value::String(s) => s.to_string_lossy().into_owned(), + _ => return Err("invalid value in cmd".into_lua_err()), + }); } Value::String(s) => { - named.insert(s.to_str()?.replace('_', "-"), v.to_string_lossy().into_owned()); + let v = match v { + Value::Boolean(b) if b => String::new(), + Value::Boolean(b) if !b => continue, + Value::Integer(i) => i.to_string(), + Value::Number(n) => n.to_string(), + Value::String(s) => s.to_string_lossy().into_owned(), + _ => return Err("invalid value in cmd".into_lua_err()), + }; + named.insert(s.to_str()?.replace('_', "-"), v); } - _ => return Err("invalid key in exec".into_lua_err()), + _ => return Err("invalid key in cmd".into_lua_err()), } } Ok((args, named)) @@ -31,12 +41,12 @@ impl Utils { #[inline] fn create_cmd(name: String, table: Table, data: Option) -> mlua::Result { let (args, named) = Self::parse_args(table)?; - let mut exec = Cmd { name, args, named, ..Default::default() }; + let mut cmd = Cmd { name, args, named, ..Default::default() }; if let Some(data) = data.and_then(|v| ValueSendable::try_from(v).ok()) { - exec = exec.with_data(data); + cmd = cmd.with_data(data); } - Ok(exec) + Ok(cmd) } pub(super) fn call(lua: &Lua, ya: &Table) -> mlua::Result<()> { diff --git a/yazi-plugin/src/utils/image.rs b/yazi-plugin/src/utils/image.rs index 09744963..7cc8e1c5 100644 --- a/yazi-plugin/src/utils/image.rs +++ b/yazi-plugin/src/utils/image.rs @@ -2,7 +2,7 @@ use mlua::{IntoLuaMulti, Lua, Table, Value}; use yazi_adaptor::{Image, ADAPTOR}; use super::Utils; -use crate::{bindings::UrlRef, elements::RectRef}; +use crate::{elements::RectRef, url::UrlRef}; impl Utils { pub(super) fn image(lua: &Lua, ya: &Table) -> mlua::Result<()> { diff --git a/yazi-plugin/src/utils/utils.rs b/yazi-plugin/src/utils/utils.rs index 80c975e4..571b25a8 100644 --- a/yazi-plugin/src/utils/utils.rs +++ b/yazi-plugin/src/utils/utils.rs @@ -1,5 +1,3 @@ -use mlua::{Lua, Table}; - #[cfg(unix)] pub(super) static USERS_CACHE: yazi_shared::RoCell = yazi_shared::RoCell::new(); @@ -7,13 +5,8 @@ pub(super) static HOSTNAME_CACHE: std::sync::OnceLock> = std::syn pub(super) struct Utils; -pub fn init() { - #[cfg(unix)] - USERS_CACHE.with(Default::default); -} - -pub fn install(lua: &Lua) -> mlua::Result<()> { - let ya: Table = lua.create_table()?; +pub fn install(lua: &mlua::Lua) -> mlua::Result<()> { + let ya: mlua::Table = lua.create_table()?; Utils::cache(lua, &ya)?; Utils::call(lua, &ya)?; @@ -29,3 +22,8 @@ pub fn install(lua: &Lua) -> mlua::Result<()> { lua.globals().set("ya", ya) } + +pub fn init() { + #[cfg(unix)] + USERS_CACHE.with(Default::default); +} diff --git a/yazi-shared/src/event/event.rs b/yazi-shared/src/event/event.rs index 02cd0858..19006883 100644 --- a/yazi-shared/src/event/event.rs +++ b/yazi-shared/src/event/event.rs @@ -44,11 +44,11 @@ macro_rules! emit { (Quit($opt:expr)) => { $crate::event::Event::Quit($opt).emit(); }; - (Call($exec:expr, $layer:expr)) => { - $crate::event::Event::Call($exec, $layer).emit(); + (Call($cmd:expr, $layer:expr)) => { + $crate::event::Event::Call($cmd, $layer).emit(); }; - (Seq($execs:expr, $layer:expr)) => { - $crate::event::Event::Seq($execs, $layer).emit(); + (Seq($cmds:expr, $layer:expr)) => { + $crate::event::Event::Seq($cmds, $layer).emit(); }; ($event:ident) => { $crate::event::Event::$event.emit();