feat: app_emit and manager_emit accepts boolean, integer, and number as option values (#625)

This commit is contained in:
三咲雅 · Misaki Masa 2024-02-04 08:38:10 +08:00 committed by GitHub
parent 82359ef290
commit a027c1be12
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 77 additions and 46 deletions

View File

@ -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<Cmd>, layer: Layer) {
if let Some(exec) = execs.pop_front() {
Executor::new(self).execute(exec, layer);
fn dispatch_seq(&mut self, mut cmds: VecDeque<Cmd>, 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));
}
}

View File

@ -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};

View File

@ -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)?;

View File

@ -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>;

View File

@ -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<T> {

View File

@ -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

View File

@ -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(

View File

@ -7,8 +7,9 @@ pub fn slim_lua() -> mlua::Result<Lua> {
// 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<Lua> {
// 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)

View File

@ -11,6 +11,7 @@ mod loader;
mod opt;
mod plugin;
pub mod process;
pub mod url;
pub mod utils;
pub use cast::*;

View File

@ -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()?;

View File

@ -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(())
}

View File

@ -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<T: Into<yazi_shared::fs::Url>> Cast<T> for Url {

View File

@ -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<()> {

View File

@ -11,18 +11,28 @@ impl Utils {
let mut args = vec![];
let mut named = BTreeMap::new();
for result in t.pairs::<Value, Value>() {
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<Value>) -> mlua::Result<Cmd> {
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<()> {

View File

@ -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<()> {

View File

@ -1,5 +1,3 @@
use mlua::{Lua, Table};
#[cfg(unix)]
pub(super) static USERS_CACHE: yazi_shared::RoCell<uzers::UsersCache> = yazi_shared::RoCell::new();
@ -7,13 +5,8 @@ pub(super) static HOSTNAME_CACHE: std::sync::OnceLock<Option<String>> = 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);
}

View File

@ -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();