yazi_plugin/utils/
call.rs

1use mlua::{Function, Lua, Table};
2use yazi_dds::Sendable;
3use yazi_macro::{emit, render};
4use yazi_shared::{Layer, event::Cmd};
5
6use super::Utils;
7
8impl Utils {
9	pub(super) fn render(lua: &Lua) -> mlua::Result<Function> {
10		lua.create_function(|_, ()| {
11			render!();
12			Ok(())
13		})
14	}
15
16	pub(super) fn emit(lua: &Lua) -> mlua::Result<Function> {
17		lua.create_function(|_, (name, args): (mlua::String, Table)| {
18			let mut cmd = Cmd::new_or(&name.to_str()?, Layer::Mgr)?;
19			cmd.args = Sendable::table_to_args(args)?;
20			Ok(emit!(Call(cmd)))
21		})
22	}
23
24	// TODO: remove
25	pub(super) fn app_emit(lua: &Lua) -> mlua::Result<Function> {
26		lua.create_function(|lua, (name, args): (String, Table)| {
27			crate::deprecate!(
28				lua,
29				"`ya.app_emit()` is deprecated, use `ya.emit()` instead, in your {}\n\nSee #2653 for more details: https://github.com/sxyazi/yazi/pull/2653"
30			);
31			emit!(Call(Cmd { name, args: Sendable::table_to_args(args)?, layer: Layer::App }));
32			Ok(())
33		})
34	}
35
36	pub(super) fn mgr_emit(lua: &Lua) -> mlua::Result<Function> {
37		lua.create_function(|_, (name, args): (String, Table)| {
38			emit!(Call(Cmd { name, args: Sendable::table_to_args(args)?, layer: Layer::Mgr }));
39			Ok(())
40		})
41	}
42
43	// TODO: remove
44	pub(super) fn input_emit(lua: &Lua) -> mlua::Result<Function> {
45		lua.create_function(|lua, (name, args): (String, Table)| {
46			crate::deprecate!(
47				lua,
48				"`ya.input_emit()` is deprecated, use `ya.emit()` instead, in your {}\n\nSee #2653 for more details: https://github.com/sxyazi/yazi/pull/2653"
49			);
50			emit!(Call(Cmd { name, args: Sendable::table_to_args(args)?, layer: Layer::Input }));
51			Ok(())
52		})
53	}
54}