Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Oct 28, 2024
1 parent fbfe8d9 commit 176773b
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 40 deletions.
2 changes: 1 addition & 1 deletion yazi-fm/src/lives/selected.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl UserData for Selected {

methods.add_meta_method(MetaMethod::Pairs, |lua, me, ()| {
let iter = lua.create_function(
// TODO: UserDataRef
// FIXME: UserDataRef
|lua, mut iter: UserDataRefMut<Iter<hash_map::Keys<yazi_shared::fs::Url, u64>, _>>| {
if let Some(next) = iter.next() {
(next.0, Url::cast(lua, next.1.clone())?).into_lua_multi(lua)
Expand Down
2 changes: 1 addition & 1 deletion yazi-fm/src/lives/yanked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl UserData for Yanked {

methods.add_meta_method(MetaMethod::Pairs, |lua, me, ()| {
let iter = lua.create_function(
// TODO: UserDataRef
// FIXME: UserDataRef
|lua, mut iter: UserDataRefMut<Iter<hash_set::Iter<yazi_shared::fs::Url>, _>>| {
if let Some(next) = iter.next() {
(next.0, Url::cast(lua, next.1.clone())?).into_lua_multi(lua)
Expand Down
2 changes: 2 additions & 0 deletions yazi-plugin/src/bindings/permit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::{mem, ops::Deref};
use mlua::{UserData, prelude::LuaUserDataMethods};
use tokio::sync::SemaphorePermit;

pub type PermitRef<'lua, F> = mlua::UserDataRef<Permit<F>>;

pub struct Permit<F: FnOnce()> {
inner: Option<SemaphorePermit<'static>>,
destruct: Option<F>,
Expand Down
22 changes: 6 additions & 16 deletions yazi-plugin/src/cha/cha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
use mlua::{AnyUserData, ExternalError, Lua, Table, UserDataFields, UserDataMethods};
use yazi_shared::fs::ChaKind;

use crate::{Runtime, bindings::Cast};
use crate::{RtRef, bindings::Cast};

pub struct Cha;

Expand Down Expand Up @@ -76,33 +76,23 @@ impl Cha {
// TODO: remove these deprecated properties in the future
{
reg.add_field_method_get("length", |lua, me| {
lua
.named_registry_value::<AnyUserData>("rt")?
.borrow_scoped(|rt: &Runtime| warn_deprecated(rt.current()))?;
warn_deprecated(lua.named_registry_value::<RtRef>("rt")?.current());
Ok(me.len)
});
reg.add_field_method_get("created", |lua, me| {
lua
.named_registry_value::<AnyUserData>("rt")?
.borrow_scoped(|rt: &Runtime| warn_deprecated(rt.current()))?;
warn_deprecated(lua.named_registry_value::<RtRef>("rt")?.current());
Ok(me.btime.and_then(|t| t.duration_since(UNIX_EPOCH).map(|d| d.as_secs_f64()).ok()))
});
reg.add_field_method_get("modified", |lua, me| {
lua
.named_registry_value::<AnyUserData>("rt")?
.borrow_scoped(|rt: &Runtime| warn_deprecated(rt.current()))?;
warn_deprecated(lua.named_registry_value::<RtRef>("rt")?.current());
Ok(me.mtime.and_then(|t| t.duration_since(UNIX_EPOCH).map(|d| d.as_secs_f64()).ok()))
});
reg.add_field_method_get("accessed", |lua, me| {
lua
.named_registry_value::<AnyUserData>("rt")?
.borrow_scoped(|rt: &Runtime| warn_deprecated(rt.current()))?;
warn_deprecated(lua.named_registry_value::<RtRef>("rt")?.current());
Ok(me.atime.and_then(|t| t.duration_since(UNIX_EPOCH).map(|d| d.as_secs_f64()).ok()))
});
reg.add_method("permissions", |lua, _me, ()| {
lua
.named_registry_value::<AnyUserData>("rt")?
.borrow_scoped(|rt: &Runtime| warn_deprecated(rt.current()))?;
warn_deprecated(lua.named_registry_value::<RtRef>("rt")?.current());
Ok(
#[cfg(unix)]
Some(yazi_shared::fs::permissions(_me.mode, _me.is_dummy())),
Expand Down
12 changes: 4 additions & 8 deletions yazi-plugin/src/elements/paragraph.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::time::Duration;

use mlua::{AnyUserData, FromLua, Lua, MultiValue, Table, Value};
use mlua::{FromLua, Lua, MultiValue, Table, Value};

use super::Rect;
use crate::Runtime;
use crate::RtRef;

static WARNED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

Expand Down Expand Up @@ -36,9 +36,7 @@ impl Paragraph {
(
"__call",
lua.create_function(|lua, (_, area, lines): (Table, Rect, Value)| {
lua
.named_registry_value::<AnyUserData>("rt")?
.borrow_scoped(|rt: &Runtime| warn_deprecated(rt.current()))?;
warn_deprecated(lua.named_registry_value::<RtRef>("rt")?.current());
lua
.load(mlua::chunk! {
return ui.Text($lines):area($area)
Expand All @@ -49,9 +47,7 @@ impl Paragraph {
(
"__index",
lua.create_function(|lua, (_, key): (Table, mlua::String)| {
lua
.named_registry_value::<AnyUserData>("rt")?
.borrow_scoped(|rt: &Runtime| warn_deprecated(rt.current()))?;
warn_deprecated(lua.named_registry_value::<RtRef>("rt")?.current());
lua
.load(mlua::chunk! {
return ui.Text[$key]
Expand Down
5 changes: 1 addition & 4 deletions yazi-plugin/src/elements/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ impl Text {
let new = lua.create_function(|_, (_, value): (Table, Value)| Text::try_from(value))?;

let parse = lua.create_function(|_, code: mlua::String| {
todo!();
Ok(1)
// Ok(Text { inner: code.into_text().into_lua_err()?, ..Default::default()
// })
Ok(Text { inner: code.as_bytes().into_text().into_lua_err()?, ..Default::default() })
})?;

let text = lua.create_table_from([
Expand Down
2 changes: 2 additions & 0 deletions yazi-plugin/src/file/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use mlua::{AnyUserData, Lua, Table, UserDataRef};

use crate::{bindings::Cast, impl_file_fields, impl_file_methods};

pub type FileRef<'lua> = UserDataRef<yazi_shared::fs::File>;

pub struct File;

impl File {
Expand Down
2 changes: 1 addition & 1 deletion yazi-plugin/src/fs/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use mlua::{ExternalError, ExternalResult, IntoLuaMulti, Lua, Table, Value};
use tokio::fs;
use yazi_shared::fs::remove_dir_clean;

use crate::{bindings::Cast, cha::Cha, file::File, url::Url};
use crate::{bindings::Cast, cha::Cha, file::File, url::{Url, UrlRef}};

pub fn install(lua: &Lua) -> mlua::Result<()> {
lua.globals().raw_set(
Expand Down
2 changes: 2 additions & 0 deletions yazi-plugin/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ struct RuntimeFrame {
calls: usize,
}

pub type RtRef<'lua> = mlua::UserDataRefMut<Runtime>;

impl Runtime {
pub fn new(id: &str) -> Self {
Self {
Expand Down
2 changes: 2 additions & 0 deletions yazi-plugin/src/url/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use mlua::{AnyUserData, ExternalError, Lua, MetaMethod, UserDataFields, UserData

use crate::bindings::Cast;

pub type UrlRef = UserDataRef<yazi_shared::fs::Url>;

pub struct Url;

impl Url {
Expand Down
17 changes: 8 additions & 9 deletions yazi-plugin/src/utils/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ impl Utils {

ya.raw_set(
"quote",
lua.create_function(|_, (s, unix): (mlua::String, Option<bool>)| {
todo!();
Ok(1)
// let s = match unix {
// Some(true) => yazi_shared::shell::escape_unix(&s.to_str()?),
// Some(false) => yazi_shared::shell::escape_windows(&s.to_str()?),
// None => yazi_shared::shell::escape_native(&s.to_str()?),
// };
// Ok(s.into_owned())
lua.create_function(|lua, (s, unix): (mlua::String, Option<bool>)| {
let s = s.to_str()?;
let s = match unix {
Some(true) => yazi_shared::shell::escape_unix(s.as_ref()),
Some(false) => yazi_shared::shell::escape_windows(s.as_ref()),
None => yazi_shared::shell::escape_native(s.as_ref()),
};
lua.create_string(s.as_ref())
})?,
)?;

Expand Down

0 comments on commit 176773b

Please sign in to comment.