This commit is contained in:
sxyazi 2024-11-23 17:43:09 +08:00
parent 143d13019f
commit e89eb384f6
No known key found for this signature in database
9 changed files with 49 additions and 30 deletions

View file

@ -1,4 +1,4 @@
use mlua::{IntoLua, Lua, LuaSerdeExt, SerializeOptions, Table, Value};
use mlua::{IntoLua, Lua, LuaSerdeExt, MetaMethod, SerializeOptions, Table, Value};
use yazi_boot::BOOT;
use yazi_config::{MANAGER, PREVIEW, THEME};
@ -50,7 +50,7 @@ impl<'a> Config<'a> {
})?;
let fetcher = self.lua.create_table()?;
fetcher.set_metatable(Some(self.lua.create_table_from([("__index", index)])?));
fetcher.set_metatable(Some(self.lua.create_table_from([(MetaMethod::Index.name(), index)])?));
self.lua.globals().raw_set("PLUGIN", fetcher)?;
Ok(self)

View file

@ -1,4 +1,4 @@
use mlua::{AnyUserData, IntoLua, Lua, Table, Value};
use mlua::{AnyUserData, IntoLua, Lua, MetaMethod, Table, Value};
use tracing::error;
use super::Renderable;
@ -34,7 +34,7 @@ pub fn compose(lua: &Lua) -> mlua::Result<Table> {
})?;
let ui = lua.create_table_with_capacity(0, 20)?;
ui.set_metatable(Some(lua.create_table_from([("__index", index)])?));
ui.set_metatable(Some(lua.create_table_from([(MetaMethod::Index.name(), index)])?));
Ok(ui)
}

View file

@ -1,6 +1,6 @@
use std::time::Duration;
use mlua::{FromLua, Lua, MultiValue, Table, Value};
use mlua::{FromLua, Lua, MetaMethod, MultiValue, Table, Value};
use super::Rect;
use crate::RtRef;
@ -34,7 +34,7 @@ impl Paragraph {
pub fn compose(lua: &Lua) -> mlua::Result<Table> {
let mt = lua.create_table_from([
(
"__call",
MetaMethod::Call.name(),
lua.create_function(|lua, (_, area, lines): (Table, Rect, Value)| {
warn_deprecated(lua.named_registry_value::<RtRef>("rt")?.current());
lua
@ -45,7 +45,7 @@ impl Paragraph {
})?,
),
(
"__index",
MetaMethod::Index.name(),
lua.create_function(|lua, (_, key): (Table, mlua::String)| {
warn_deprecated(lua.named_registry_value::<RtRef>("rt")?.current());
lua

View file

@ -1,4 +1,6 @@
use mlua::{IntoLua, Lua, MetaMethod, UserData, UserDataFields, UserDataMethods};
use std::borrow::Cow;
use mlua::{ExternalError, Lua, MetaMethod, UserData, UserDataFields, UserDataMethods, Value};
pub enum Error {
Io(std::io::Error),
@ -12,6 +14,14 @@ impl Error {
lua.globals().raw_set("Error", lua.create_table_from([("custom", new)])?)
}
fn to_string(&self) -> Cow<str> {
match self {
Error::Io(e) => Cow::Owned(e.to_string()),
Error::Serde(e) => Cow::Owned(e.to_string()),
Error::Custom(s) => Cow::Borrowed(s),
}
}
}
impl UserData for Error {
@ -26,11 +36,20 @@ impl UserData for Error {
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
methods.add_meta_method(MetaMethod::ToString, |lua, me, ()| {
Ok(match me {
Error::Io(e) => e.to_string().into_lua(lua),
Error::Serde(e) => e.to_string().into_lua(lua),
Error::Custom(s) => lua.create_string(s)?.into_lua(lua),
})
lua.create_string(me.to_string().as_ref())
});
methods.add_meta_function(MetaMethod::Concat, |lua, (lhs, rhs): (Value, Value)| {
match (lhs, rhs) {
(Value::String(l), Value::UserData(r)) => {
let r = r.borrow::<Self>()?;
lua.create_string([l.as_bytes().as_ref(), r.to_string().as_bytes()].concat())
}
(Value::UserData(l), Value::String(r)) => {
let l = l.borrow::<Self>()?;
lua.create_string([l.to_string().as_bytes(), r.as_bytes().as_ref()].concat())
}
_ => Err("only string can be concatenated with Error".into_lua_err()),
}
});
}
}

View file

@ -1,5 +1,5 @@
use globset::GlobBuilder;
use mlua::{ExternalError, ExternalResult, Function, IntoLua, IntoLuaMulti, Lua, Table, Value};
use mlua::{ExternalError, ExternalResult, Function, IntoLua, IntoLuaMulti, Lua, MetaMethod, Table, Value};
use tokio::fs;
use yazi_shared::fs::remove_dir_clean;
@ -22,7 +22,7 @@ pub fn compose(lua: &Lua) -> mlua::Result<Table> {
})?;
let fs = lua.create_table_with_capacity(0, 10)?;
fs.set_metatable(Some(lua.create_table_from([("__index", index)])?));
fs.set_metatable(Some(lua.create_table_from([(MetaMethod::Index.name(), index)])?));
Ok(fs)
}
@ -53,11 +53,11 @@ fn write(lua: &Lua) -> mlua::Result<Function> {
fn remove(lua: &Lua) -> mlua::Result<Function> {
lua.create_async_function(|lua, (type_, url): (mlua::String, UrlRef)| async move {
let result = match &*type_.to_str()? {
"file" => fs::remove_file(&*url).await,
"dir" => fs::remove_dir(&*url).await,
"dir_all" => fs::remove_dir_all(&*url).await,
"dir_clean" => Ok(remove_dir_clean(&url).await),
let result = match type_.as_bytes().as_ref() {
b"file" => fs::remove_file(&*url).await,
b"dir" => fs::remove_dir(&*url).await,
b"dir_all" => fs::remove_dir_all(&*url).await,
b"dir_clean" => Ok(remove_dir_clean(&url).await),
_ => Err("Removal type must be 'file', 'dir', 'dir_all', or 'dir_clean'".into_lua_err())?,
};

View file

@ -1,6 +1,6 @@
use std::sync::Arc;
use mlua::{ExternalResult, Function, IntoLua, Lua, MultiValue, ObjectLike, Table, Value};
use mlua::{ExternalResult, Function, IntoLua, Lua, MetaMethod, MultiValue, ObjectLike, Table, Value};
use super::LOADER;
use crate::RtRef;
@ -44,7 +44,7 @@ impl Require {
let id: Arc<str> = Arc::from(id);
let mt = lua.create_table_from([
(
"__index",
MetaMethod::Index.name(),
lua.create_function(move |lua, (ts, key): (Table, mlua::String)| {
match ts.raw_get::<Table>("__mod")?.raw_get::<Value>(&key)? {
Value::Function(_) => {
@ -55,7 +55,7 @@ impl Require {
})?,
),
(
"__newindex",
MetaMethod::NewIndex.name(),
lua.create_function(move |_, (ts, key, value): (Table, mlua::String, Value)| {
ts.raw_get::<Table>("__mod")?.raw_set(key, value)
})?,

View file

@ -1,6 +1,6 @@
#![allow(clippy::module_inception)]
use mlua::{IntoLua, Lua, Table, Value};
use mlua::{IntoLua, Lua, MetaMethod, Table, Value};
yazi_macro::mod_flat!(pubsub);
@ -22,7 +22,7 @@ pub(super) fn compose(lua: &Lua) -> mlua::Result<Table> {
})?;
let ps = lua.create_table_with_capacity(0, 10)?;
ps.set_metatable(Some(lua.create_table_from([("__index", index)])?));
ps.set_metatable(Some(lua.create_table_from([(MetaMethod::Index.name(), index)])?));
Ok(ps)
}

View file

@ -62,9 +62,9 @@ impl Url {
reg.add_meta_method(MetaMethod::ToString, |lua, me, ()| {
lua.create_string(me.as_os_str().as_encoded_bytes())
});
reg.add_meta_method(MetaMethod::Concat, |lua, me, other: mlua::String| {
let me = me.as_os_str().as_encoded_bytes();
let other = other.as_bytes();
reg.add_meta_method(MetaMethod::Concat, |lua, lhs, rhs: mlua::String| {
let me = lhs.as_os_str().as_encoded_bytes();
let other = rhs.as_bytes();
let mut out = Vec::with_capacity(me.len() + other.len());
out.extend_from_slice(me);

View file

@ -1,4 +1,4 @@
use mlua::{IntoLua, Lua, Table, Value};
use mlua::{IntoLua, Lua, MetaMethod, Table, Value};
pub(super) struct Utils;
@ -83,7 +83,7 @@ pub fn compose(lua: &Lua, isolate: bool) -> mlua::Result<Table> {
})?;
let ya = lua.create_table_with_capacity(0, 40)?;
ya.set_metatable(Some(lua.create_table_from([("__index", index)])?));
ya.set_metatable(Some(lua.create_table_from([(MetaMethod::Index.name(), index)])?));
Ok(ya)
}