From aa069cc30ec487dd78a04cf5e03ea654a9c90bab Mon Sep 17 00:00:00 2001 From: sxyazi Date: Mon, 4 Dec 2023 16:44:37 +0800 Subject: [PATCH] .. --- Cargo.lock | 2 +- yazi-core/src/files/sorter.rs | 8 +- yazi-core/src/manager/commands/peek.rs | 6 +- yazi-fm/src/lives/files.rs | 24 +++--- yazi-fm/src/lives/mod.rs | 3 + yazi-plugin/Cargo.toml | 5 +- yazi-plugin/preset/plugins/image.lua | 2 +- yazi-plugin/src/bindings/url.rs | 6 +- yazi-plugin/src/isolate/mod.rs | 5 ++ yazi-plugin/src/isolate/peek.rs | 20 +++++ yazi-plugin/src/isolate/preload.rs | 19 +++++ yazi-plugin/src/layout/bar.rs | 7 +- yazi-plugin/src/layout/base.rs | 8 +- yazi-plugin/src/layout/border.rs | 8 +- yazi-plugin/src/layout/gauge.rs | 10 +-- yazi-plugin/src/layout/layout.rs | 45 +++++------ yazi-plugin/src/layout/list.rs | 8 +- yazi-plugin/src/layout/padding.rs | 4 +- yazi-plugin/src/layout/paragraph.rs | 8 +- yazi-plugin/src/layout/rect.rs | 5 +- yazi-plugin/src/lib.rs | 1 + yazi-plugin/src/utils/preview.rs | 19 ++--- yazi-scheduler/Cargo.toml | 1 - yazi-scheduler/src/isolate/isolate.rs | 34 --------- yazi-scheduler/src/isolate/mod.rs | 3 - yazi-scheduler/src/lib.rs | 1 - yazi-scheduler/src/workers/preview.rs | 31 ++++---- yazi-shared/src/fs/file.rs | 2 +- yazi-shared/src/natsort.rs | 101 +------------------------ 29 files changed, 158 insertions(+), 238 deletions(-) create mode 100644 yazi-plugin/src/isolate/mod.rs create mode 100644 yazi-plugin/src/isolate/peek.rs create mode 100644 yazi-plugin/src/isolate/preload.rs delete mode 100644 yazi-scheduler/src/isolate/isolate.rs delete mode 100644 yazi-scheduler/src/isolate/mod.rs diff --git a/Cargo.lock b/Cargo.lock index dde536f6a..bc2d209c9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2632,6 +2632,7 @@ dependencies = [ "tokio", "tracing", "unicode-width", + "yazi-adaptor", "yazi-config", "yazi-shared", ] @@ -2651,7 +2652,6 @@ dependencies = [ "clipboard-win", "futures", "libc", - "mlua", "parking_lot", "regex", "serde", diff --git a/yazi-core/src/files/sorter.rs b/yazi-core/src/files/sorter.rs index bdf620ba1..4dea60833 100644 --- a/yazi-core/src/files/sorter.rs +++ b/yazi-core/src/files/sorter.rs @@ -1,7 +1,7 @@ use std::{cmp::Ordering, collections::BTreeMap, mem}; use yazi_config::manager::SortBy; -use yazi_shared::{fs::File, fs::Url, natsort}; +use yazi_shared::{fs::{File, Url}, natsort}; #[derive(Clone, Copy, Default, PartialEq)] pub struct FilesSorter { @@ -68,16 +68,16 @@ impl FilesSorter { let mut entities = Vec::with_capacity(items.len()); for (i, file) in items.iter().enumerate() { indices.push(i); - entities.push((file.url.to_string_lossy(), file)); + entities.push(file.url.as_os_str().as_encoded_bytes()); } indices.sort_unstable_by(|&a, &b| { - let promote = self.promote(entities[a].1, entities[b].1); + let promote = self.promote(&items[a], &items[b]); if promote != Ordering::Equal { return promote; } - let ordering = natsort(&entities[a].0, &entities[b].0, !self.sensitive); + let ordering = natsort(entities[a], entities[b], !self.sensitive); if self.reverse { ordering.reverse() } else { ordering } }); diff --git a/yazi-core/src/manager/commands/peek.rs b/yazi-core/src/manager/commands/peek.rs index 500ad8d04..e71462767 100644 --- a/yazi-core/src/manager/commands/peek.rs +++ b/yazi-core/src/manager/commands/peek.rs @@ -53,17 +53,17 @@ impl Manager { return self.active_mut().preview.reset(); }; - let (url, cha) = (hovered.url.clone(), hovered.cha); + let hovered = hovered.clone(); if opt.upper_bound { self.active_mut().preview.arrow(0, &mime, Some(opt.step as usize)); - } else if self.active().preview.same_url(&url) { + } else if self.active().preview.same_url(&hovered.url) { self.active_mut().preview.arrow(opt.step, &mime, None); } else { self.active_mut().preview.arrow(0, &mime, Some(0)); self.active_mut().preview.reset(); } - self.active_mut().preview.go(&url, cha, &mime); + self.active_mut().preview.go(hovered, &mime); false } diff --git a/yazi-fm/src/lives/files.rs b/yazi-fm/src/lives/files.rs index e215240ef..bff155e63 100644 --- a/yazi-fm/src/lives/files.rs +++ b/yazi-fm/src/lives/files.rs @@ -1,7 +1,9 @@ -use mlua::{AnyUserData, IntoLua, Lua, MetaMethod, UserDataMethods, UserDataRef}; +use mlua::{AnyUserData, IntoLua, Lua, MetaMethod, UserDataMethods}; use yazi_config::THEME; use yazi_plugin::{bindings::{Cast, File, Range}, layout::Style}; +use super::{FolderRef, ManagerRef}; + pub struct Files; impl Files { @@ -13,16 +15,16 @@ impl Files { return Ok(Some(file.len)); } - let folder = me.named_user_value::>("folder")?; + let folder = me.named_user_value::("folder")?; Ok(folder.files.sizes.get(&file.url).copied()) }); reg.add_function("mime", |_, me: AnyUserData| { - let manager = me.named_user_value::>("manager")?; + let manager = me.named_user_value::("manager")?; let file = me.borrow::()?; Ok(manager.mimetype.get(&file.url).cloned()) }); reg.add_function("prefix", |lua, me: AnyUserData| { - let folder = me.named_user_value::>("folder")?; + let folder = me.named_user_value::("folder")?; if !folder.cwd.is_search() { return Ok(None); } @@ -42,7 +44,7 @@ impl Files { ) }); reg.add_function("style", |_, me: AnyUserData| { - let manager = me.named_user_value::>("manager")?; + let manager = me.named_user_value::("manager")?; let file = me.borrow::()?; let mime = manager.mimetype.get(&file.url); Ok( @@ -54,12 +56,12 @@ impl Files { ) }); reg.add_function("is_hovered", |_, me: AnyUserData| { - let folder = me.named_user_value::>("folder")?; + let folder = me.named_user_value::("folder")?; let file = me.borrow::()?; Ok(matches!(folder.hovered(), Some(f) if f.url == file.url)) }); reg.add_function("is_yanked", |_, me: AnyUserData| { - let manager = me.named_user_value::>("manager")?; + let manager = me.named_user_value::("manager")?; let file = me.borrow::()?; Ok(if !manager.yanked.1.contains(&file.url) { 0u8 @@ -70,8 +72,8 @@ impl Files { }) }); reg.add_function("is_selected", |_, me: AnyUserData| { - let manager = me.named_user_value::>("manager")?; - let folder = me.named_user_value::>("folder")?; + let manager = me.named_user_value::("manager")?; + let folder = me.named_user_value::("folder")?; let file = me.borrow::()?; let selected = folder.files.is_selected(&file.url); @@ -83,7 +85,7 @@ impl Files { }) }); reg.add_function("found", |lua, me: AnyUserData| { - let manager = me.named_user_value::>("manager")?; + let manager = me.named_user_value::("manager")?; let Some(finder) = &manager.active().finder else { return Ok(None); }; @@ -98,7 +100,7 @@ impl Files { Ok(None) }); reg.add_function("highlights", |_, me: AnyUserData| { - let manager = me.named_user_value::>("manager")?; + let manager = me.named_user_value::("manager")?; let Some(finder) = &manager.active().finder else { return Ok(None); }; diff --git a/yazi-fm/src/lives/mod.rs b/yazi-fm/src/lives/mod.rs index f70a7554b..319a42460 100644 --- a/yazi-fm/src/lives/mod.rs +++ b/yazi-fm/src/lives/mod.rs @@ -11,3 +11,6 @@ pub(super) use files::*; pub(super) use lives::*; pub(super) use tabs::*; pub(super) use tasks::*; + +type FolderRef<'lua> = mlua::UserDataRef<'lua, yazi_core::tab::Folder>; +type ManagerRef<'lua> = mlua::UserDataRef<'lua, yazi_core::manager::Manager>; diff --git a/yazi-plugin/Cargo.toml b/yazi-plugin/Cargo.toml index aac051760..cca685be6 100644 --- a/yazi-plugin/Cargo.toml +++ b/yazi-plugin/Cargo.toml @@ -9,8 +9,9 @@ homepage = "https://yazi-rs.github.io" repository = "https://github.com/sxyazi/yazi" [dependencies] -yazi-config = { path = "../yazi-config", version = "0.1.5" } -yazi-shared = { path = "../yazi-shared", version = "0.1.5" } +yazi-adaptor = { path = "../yazi-adaptor", version = "0.1.5" } +yazi-config = { path = "../yazi-config", version = "0.1.5" } +yazi-shared = { path = "../yazi-shared", version = "0.1.5" } # External dependencies ansi-to-tui = "^3" diff --git a/yazi-plugin/preset/plugins/image.lua b/yazi-plugin/preset/plugins/image.lua index e0aadb090..bc2ba556a 100644 --- a/yazi-plugin/preset/plugins/image.lua +++ b/yazi-plugin/preset/plugins/image.lua @@ -8,7 +8,7 @@ end function Image:preload(file) if not cache_exists(file.url .. tostring(file.modified)) then local cache = cache_path(file.url .. file.modified) - return image_downscale(file.url, cache) and 1 or 0 + return image_precache(file.url, cache) and 1 or 0 end return 1 end diff --git a/yazi-plugin/src/bindings/url.rs b/yazi-plugin/src/bindings/url.rs index d4bce7c22..34ddd1e85 100644 --- a/yazi-plugin/src/bindings/url.rs +++ b/yazi-plugin/src/bindings/url.rs @@ -2,6 +2,8 @@ use mlua::{AnyUserData, Lua, MetaMethod, UserDataFields, UserDataMethods, UserDa use super::Cast; +pub type UrlRef<'lua> = UserDataRef<'lua, yazi_shared::fs::Url>; + pub struct Url; impl Url { @@ -12,9 +14,7 @@ impl Url { reg.add_field_method_get("is_search", |_, me| Ok(me.is_search())); reg.add_field_method_get("is_archive", |_, me| Ok(me.is_archive())); - reg.add_meta_method(MetaMethod::Eq, |_, me, other: UserDataRef| { - Ok(me == &*other) - }); + reg.add_meta_method(MetaMethod::Eq, |_, me, other: UrlRef| Ok(me == &*other)); reg.add_meta_method(MetaMethod::ToString, |lua, me, ()| { lua.create_string(me.as_os_str().as_encoded_bytes()) diff --git a/yazi-plugin/src/isolate/mod.rs b/yazi-plugin/src/isolate/mod.rs new file mode 100644 index 000000000..2f612a6a0 --- /dev/null +++ b/yazi-plugin/src/isolate/mod.rs @@ -0,0 +1,5 @@ +mod peek; +mod preload; + +pub use peek::*; +pub use preload::*; diff --git a/yazi-plugin/src/isolate/peek.rs b/yazi-plugin/src/isolate/peek.rs new file mode 100644 index 000000000..5734daf51 --- /dev/null +++ b/yazi-plugin/src/isolate/peek.rs @@ -0,0 +1,20 @@ +use mlua::{ExternalError, Lua, Table, TableExt}; + +use crate::bindings::{Cast, File}; + +pub async fn peek(file: yazi_shared::fs::File, skip: usize) -> mlua::Result<()> { + tokio::task::spawn_local(async move { + let lua = Lua::new(); + crate::bindings::Url::register(&lua)?; + crate::bindings::File::register(&lua, |_| {})?; + crate::Utils::install(&lua)?; + + let plugin: Table = + lua.load(include_str!("../../../yazi-plugin/preset/plugins/image.lua")).call(())?; + + let file = File::cast(&lua, file)?; + plugin.call_async_method::<_, ()>("peek", (file, skip)).await + }) + .await + .map_err(|_| "peek error".into_lua_err())? +} diff --git a/yazi-plugin/src/isolate/preload.rs b/yazi-plugin/src/isolate/preload.rs new file mode 100644 index 000000000..1dc7f415d --- /dev/null +++ b/yazi-plugin/src/isolate/preload.rs @@ -0,0 +1,19 @@ +use mlua::{Lua, Table, TableExt}; + +use crate::bindings::{Cast, File}; + +pub async fn preload(files: &[yazi_shared::fs::File]) -> mlua::Result<()> { + let lua = Lua::new(); + crate::bindings::Url::register(&lua)?; + crate::bindings::File::register(&lua, |_| {})?; + crate::Utils::install(&lua)?; + + let plugin: Table = + lua.load(include_str!("../../../yazi-plugin/preset/plugins/image.lua")).call(())?; + + for file in files { + let file = File::cast(&lua, file.clone())?; + plugin.call_async_method::<_, mlua::Error>("preload", file).await.ok(); + } + Ok(()) +} diff --git a/yazi-plugin/src/layout/bar.rs b/yazi-plugin/src/layout/bar.rs index 50ea78089..5b4afb8a0 100644 --- a/yazi-plugin/src/layout/bar.rs +++ b/yazi-plugin/src/layout/bar.rs @@ -1,7 +1,6 @@ -use mlua::{AnyUserData, ExternalError, Lua, Table, UserData, UserDataRef, Value}; -use ratatui::layout::Rect; +use mlua::{AnyUserData, ExternalError, Lua, Table, UserData, Value}; -use super::Style; +use super::{RectRef, Style}; #[derive(Clone)] pub struct Bar { @@ -17,7 +16,7 @@ impl Bar { let ui: Table = lua.globals().get("ui")?; ui.set( "Bar", - lua.create_function(|_, (area, direction): (UserDataRef, u8)| { + lua.create_function(|_, (area, direction): (RectRef, u8)| { Ok(Self { area: *area, diff --git a/yazi-plugin/src/layout/base.rs b/yazi-plugin/src/layout/base.rs index da8d2db72..d46c85efe 100644 --- a/yazi-plugin/src/layout/base.rs +++ b/yazi-plugin/src/layout/base.rs @@ -1,5 +1,7 @@ -use mlua::{Lua, Table, UserData, UserDataRef}; -use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget}; +use mlua::{Lua, Table, UserData}; +use ratatui::{buffer::Buffer, widgets::Widget}; + +use super::RectRef; #[derive(Clone, Copy)] pub struct Base { @@ -23,7 +25,7 @@ impl Base { let base: Table = ui.get("Base")?; base.set( "new", - lua.create_function(|_, (area, kind): (UserDataRef, u8)| { + lua.create_function(|_, (area, kind): (RectRef, u8)| { Ok(Self { area: *area, kind: kind.into() }) })?, ) diff --git a/yazi-plugin/src/layout/border.rs b/yazi-plugin/src/layout/border.rs index 0f2729267..966d33baa 100644 --- a/yazi-plugin/src/layout/border.rs +++ b/yazi-plugin/src/layout/border.rs @@ -1,7 +1,7 @@ -use mlua::{AnyUserData, ExternalError, Lua, Table, UserData, UserDataRef, Value}; -use ratatui::{layout::Rect, widgets::Widget}; +use mlua::{AnyUserData, ExternalError, Lua, Table, UserData, Value}; +use ratatui::widgets::Widget; -use super::Style; +use super::{RectRef, Style}; #[derive(Clone)] pub struct Border { @@ -18,7 +18,7 @@ impl Border { let border: Table = ui.get("Border")?; border.set( "new", - lua.create_function(|_, (area, position): (UserDataRef, u8)| { + lua.create_function(|_, (area, position): (RectRef, u8)| { Ok(Self { area: *area, position: ratatui::widgets::Borders::from_bits_truncate(position), diff --git a/yazi-plugin/src/layout/gauge.rs b/yazi-plugin/src/layout/gauge.rs index 601bc19ea..8d1fe402d 100644 --- a/yazi-plugin/src/layout/gauge.rs +++ b/yazi-plugin/src/layout/gauge.rs @@ -1,7 +1,7 @@ -use mlua::{AnyUserData, ExternalError, Lua, Table, UserData, UserDataMethods, UserDataRef, Value}; -use ratatui::{layout::Rect, widgets::Widget}; +use mlua::{AnyUserData, ExternalError, Lua, Table, UserData, UserDataMethods, Value}; +use ratatui::widgets::Widget; -use super::{Span, Style}; +use super::{RectRef, Span, Style}; #[derive(Clone, Default)] pub struct Gauge { @@ -18,9 +18,7 @@ impl Gauge { let ui: Table = lua.globals().get("ui")?; ui.set( "Gauge", - lua.create_function(|_, area: UserDataRef| { - Ok(Gauge { area: *area, ..Default::default() }) - })?, + lua.create_function(|_, area: RectRef| Ok(Gauge { area: *area, ..Default::default() }))?, ) } diff --git a/yazi-plugin/src/layout/layout.rs b/yazi-plugin/src/layout/layout.rs index a4eee2a67..deb4b7029 100644 --- a/yazi-plugin/src/layout/layout.rs +++ b/yazi-plugin/src/layout/layout.rs @@ -1,6 +1,6 @@ -use mlua::{AnyUserData, Lua, Table, UserData, UserDataMethods, UserDataRef}; +use mlua::{AnyUserData, Lua, Table, UserData, UserDataMethods}; -use super::{Constraint, Rect}; +use super::{Constraint, Rect, RectRef}; use crate::bindings::Cast; #[derive(Clone, Default)] @@ -53,30 +53,27 @@ impl UserData for Layout { ud.borrow_mut::()?.constraints = value.into_iter().map(|c| c.0).collect(); Ok(ud) }); - methods.add_function( - "split", - |lua, (ud, value): (AnyUserData, UserDataRef)| { - let me = ud.borrow::()?; + methods.add_function("split", |lua, (ud, value): (AnyUserData, RectRef)| { + let me = ud.borrow::()?; - let mut layout = ratatui::layout::Layout::new() - .direction(if me.direction { - ratatui::layout::Direction::Vertical - } else { - ratatui::layout::Direction::Horizontal - }) - .constraints(me.constraints.as_slice()); + let mut layout = ratatui::layout::Layout::new() + .direction(if me.direction { + ratatui::layout::Direction::Vertical + } else { + ratatui::layout::Direction::Horizontal + }) + .constraints(me.constraints.as_slice()); - if let Some(margin) = me.margin { - layout = layout.horizontal_margin(margin.horizontal); - layout = layout.vertical_margin(margin.vertical); - } + if let Some(margin) = me.margin { + layout = layout.horizontal_margin(margin.horizontal); + layout = layout.vertical_margin(margin.vertical); + } - let mut chunks = Vec::with_capacity(me.constraints.len()); - for chunk in &*layout.split(*value) { - chunks.push(Rect::cast(lua, *chunk)?); - } - Ok(chunks) - }, - ); + let mut chunks = Vec::with_capacity(me.constraints.len()); + for chunk in &*layout.split(*value) { + chunks.push(Rect::cast(lua, *chunk)?); + } + Ok(chunks) + }); } } diff --git a/yazi-plugin/src/layout/list.rs b/yazi-plugin/src/layout/list.rs index b8cd337c6..46129abe0 100644 --- a/yazi-plugin/src/layout/list.rs +++ b/yazi-plugin/src/layout/list.rs @@ -1,7 +1,7 @@ -use mlua::{AnyUserData, ExternalError, FromLua, Lua, Table, UserData, UserDataRef, Value}; -use ratatui::{layout::Rect, widgets::Widget}; +use mlua::{AnyUserData, ExternalError, FromLua, Lua, Table, UserData, Value}; +use ratatui::widgets::Widget; -use super::{Line, Span, Style}; +use super::{Line, RectRef, Span, Style}; // --- List #[derive(Clone)] @@ -16,7 +16,7 @@ impl List { let ui: Table = lua.globals().get("ui")?; ui.set( "List", - lua.create_function(|_, (area, items): (UserDataRef, Vec)| { + lua.create_function(|_, (area, items): (RectRef, Vec)| { let items: Vec<_> = items.into_iter().map(|x| x.into()).collect(); Ok(Self { area: *area, inner: ratatui::widgets::List::new(items) }) })?, diff --git a/yazi-plugin/src/layout/padding.rs b/yazi-plugin/src/layout/padding.rs index bb136c2ae..5649252b5 100644 --- a/yazi-plugin/src/layout/padding.rs +++ b/yazi-plugin/src/layout/padding.rs @@ -1,7 +1,9 @@ -use mlua::{AnyUserData, Lua, Table, UserDataFields}; +use mlua::{AnyUserData, Lua, Table, UserDataFields, UserDataRef}; use crate::bindings::Cast; +pub type PaddingRef<'lua> = UserDataRef<'lua, ratatui::widgets::Padding>; + pub struct Padding; impl Padding { diff --git a/yazi-plugin/src/layout/paragraph.rs b/yazi-plugin/src/layout/paragraph.rs index 35ada06aa..ae5f24b8e 100644 --- a/yazi-plugin/src/layout/paragraph.rs +++ b/yazi-plugin/src/layout/paragraph.rs @@ -1,7 +1,7 @@ -use mlua::{AnyUserData, ExternalError, Lua, Table, UserData, UserDataRef, Value}; -use ratatui::{layout::Rect, widgets::Widget}; +use mlua::{AnyUserData, ExternalError, Lua, Table, UserData, Value}; +use ratatui::widgets::Widget; -use super::Style; +use super::{RectRef, Style}; use crate::layout::Line; #[derive(Clone, Debug)] @@ -18,7 +18,7 @@ impl Paragraph { let ui: Table = lua.globals().get("ui")?; ui.set( "Paragraph", - lua.create_function(|_, (area, lines): (UserDataRef, Vec)| { + lua.create_function(|_, (area, lines): (RectRef, Vec)| { Ok(Self { area: *area, diff --git a/yazi-plugin/src/layout/rect.rs b/yazi-plugin/src/layout/rect.rs index 7d20996cb..5d4bd97ef 100644 --- a/yazi-plugin/src/layout/rect.rs +++ b/yazi-plugin/src/layout/rect.rs @@ -1,7 +1,10 @@ use mlua::{AnyUserData, Lua, Table, UserDataFields, UserDataMethods, UserDataRef}; +use super::PaddingRef; use crate::bindings::Cast; +pub type RectRef<'lua> = UserDataRef<'lua, ratatui::layout::Rect>; + pub struct Rect; impl Rect { @@ -32,7 +35,7 @@ impl Rect { reg.add_field_method_get("top", |_, me| Ok(me.top())); reg.add_field_method_get("bottom", |_, me| Ok(me.bottom())); - reg.add_method("padding", |lua, me, padding: UserDataRef| { + reg.add_method("padding", |lua, me, padding: PaddingRef| { let mut r = *me; r.x = r.x.saturating_add(padding.left); r.y = r.y.saturating_add(padding.top); diff --git a/yazi-plugin/src/lib.rs b/yazi-plugin/src/lib.rs index 0265b2fee..6bb013efe 100644 --- a/yazi-plugin/src/lib.rs +++ b/yazi-plugin/src/lib.rs @@ -2,6 +2,7 @@ pub mod bindings; mod config; +pub mod isolate; pub mod layout; mod plugin; mod utils; diff --git a/yazi-plugin/src/utils/preview.rs b/yazi-plugin/src/utils/preview.rs index 77b2c21b6..724f44505 100644 --- a/yazi-plugin/src/utils/preview.rs +++ b/yazi-plugin/src/utils/preview.rs @@ -1,23 +1,24 @@ -use mlua::{Lua, Table, UserDataRef}; +use mlua::{Lua, Table}; +use yazi_adaptor::{Image, ADAPTOR}; -use crate::Utils; +use crate::{bindings::UrlRef, layout::RectRef, Utils}; impl Utils { pub(super) fn preview(lua: &Lua, utils: &Table) -> mlua::Result<()> { utils.set( "image_show", - lua.create_function(|_, (url, rect): (UserDataRef, ())| { - // ADAPTOR.image_show(&url, rect) - + lua.create_async_function(|_, (url, rect): (UrlRef, RectRef)| async move { + ADAPTOR.image_show(&url, *rect).await.ok(); Ok(()) })?, )?; utils.set( - "image_downscale", - lua.create_async_function( - |_, (src, dist): (UserDataRef, UserDataRef)| async move { Ok(()) }, - )?, + "image_precache", + lua.create_async_function(|_, (src, dist): (UrlRef, UrlRef)| async move { + Image::precache(&src, dist.to_path_buf()).await.ok(); + Ok(()) + })?, )?; Ok(()) diff --git a/yazi-scheduler/Cargo.toml b/yazi-scheduler/Cargo.toml index 23bd2b56b..c05c90b64 100644 --- a/yazi-scheduler/Cargo.toml +++ b/yazi-scheduler/Cargo.toml @@ -19,7 +19,6 @@ anyhow = "^1" async-channel = "^1" futures = "^0" libc = "^0" -mlua = { version = "^0", features = [ "luajit52", "vendored" ] } parking_lot = "^0" regex = "^1" serde = "^1" diff --git a/yazi-scheduler/src/isolate/isolate.rs b/yazi-scheduler/src/isolate/isolate.rs deleted file mode 100644 index 797843081..000000000 --- a/yazi-scheduler/src/isolate/isolate.rs +++ /dev/null @@ -1,34 +0,0 @@ -use mlua::{Lua, Table, TableExt}; -use yazi_plugin::bindings::{Cast, File}; - -fn init_shim(lua: &Lua, globals: Table) -> mlua::Result<()> { - yazi_plugin::bindings::Url::register(lua)?; - yazi_plugin::bindings::File::register(lua, |_| {})?; - - Ok(()) -} - -pub async fn peek(file: &yazi_shared::fs::File, skip: usize) -> mlua::Result<()> { - let lua = Lua::new(); - init_shim(&lua, lua.globals())?; - - let plugin: Table = - lua.load(include_str!("../../../yazi-plugin/preset/plugins/image.lua")).call(())?; - - let file = File::cast(&lua, file)?; - plugin.call_async_method("peek", (file, skip)).await -} - -pub async fn preload(files: &[yazi_shared::fs::File]) -> mlua::Result<()> { - let lua = Lua::new(); - init_shim(&lua, lua.globals())?; - - let plugin: Table = - lua.load(include_str!("../../../yazi-plugin/preset/plugins/image.lua")).call(())?; - - for file in files { - let file = File::cast(&lua, file.clone())?; - plugin.call_async_method::<_, mlua::Error>("preload", file).await.ok(); - } - Ok(()) -} diff --git a/yazi-scheduler/src/isolate/mod.rs b/yazi-scheduler/src/isolate/mod.rs deleted file mode 100644 index 6cf5d5353..000000000 --- a/yazi-scheduler/src/isolate/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod isolate; - -pub use isolate::*; diff --git a/yazi-scheduler/src/lib.rs b/yazi-scheduler/src/lib.rs index 6f83d7a2d..ed2f4695b 100644 --- a/yazi-scheduler/src/lib.rs +++ b/yazi-scheduler/src/lib.rs @@ -2,7 +2,6 @@ mod blocker; pub mod external; -pub mod isolate; mod running; mod scheduler; mod task; diff --git a/yazi-scheduler/src/workers/preview.rs b/yazi-scheduler/src/workers/preview.rs index 88a859554..628f27dc1 100644 --- a/yazi-scheduler/src/workers/preview.rs +++ b/yazi-scheduler/src/workers/preview.rs @@ -3,7 +3,7 @@ use std::mem; use tokio::task::JoinHandle; use yazi_adaptor::ADAPTOR; use yazi_config::MANAGER; -use yazi_shared::{emit, event::{PreviewData, PreviewLock}, fs::{Cha, Url}, MimeKind, PeekError}; +use yazi_shared::{event::{PreviewData, PreviewLock}, fs::{Cha, File, Url}, MimeKind}; use super::Provider; @@ -16,25 +16,28 @@ pub struct Preview { } impl Preview { - pub fn go(&mut self, url: &Url, cha: Cha, mime: &str) { - if self.content_unchanged(url, &cha) { + pub fn go(&mut self, file: File, mime: &str) { + if self.content_unchanged(&file.url, &file.cha) { return; } self.abort(); - let (url, kind, skip) = (url.clone(), MimeKind::new(mime), self.skip); + let (kind, skip) = (MimeKind::new(mime), self.skip); self.handle = Some(tokio::spawn(async move { - match Provider::auto(kind, &url, skip).await { - Ok(data) => { - emit!(Preview(PreviewLock { url, cha: Some(cha), skip, data })); - } - Err(PeekError::Exceed(max)) => { - // TODO: plugin system - // Manager::_peek_upper_bound(max, &url); - } - _ => {} - } + let result = yazi_plugin::isolate::peek(file, skip).await; + + // TODO: plugin system + // match Provider::auto(kind, &url, skip).await { + // Ok(data) => { + // emit!(Preview(PreviewLock { url, cha: Some(cha), skip, data })); + // } + // Err(PeekError::Exceed(max)) => { + // // TODO: plugin system + // // Manager::_peek_upper_bound(max, &url); + // } + // _ => {} + // } })); } diff --git a/yazi-shared/src/fs/file.rs b/yazi-shared/src/fs/file.rs index 63018aec0..5e624ab61 100644 --- a/yazi-shared/src/fs/file.rs +++ b/yazi-shared/src/fs/file.rs @@ -1,4 +1,4 @@ -use std::{borrow::Cow, collections::BTreeMap, ffi::OsStr, fs::Metadata, ops::Deref}; +use std::{collections::BTreeMap, ffi::OsStr, fs::Metadata, ops::Deref}; use anyhow::Result; use tokio::fs; diff --git a/yazi-shared/src/natsort.rs b/yazi-shared/src/natsort.rs index c53470521..70e259692 100644 --- a/yazi-shared/src/natsort.rs +++ b/yazi-shared/src/natsort.rs @@ -64,10 +64,7 @@ fn compare_right(left: &[u8], right: &[u8], li: &mut usize, ri: &mut usize) -> O } } -pub fn natsort(left: &str, right: &str, insensitive: bool) -> Ordering { - let left = left.as_bytes(); - let right = right.as_bytes(); - +pub fn natsort(left: &[u8], right: &[u8], insensitive: bool) -> Ordering { let mut li = 0; let mut ri = 0; @@ -132,7 +129,7 @@ mod tests { fn cmp(left: &[&str]) { let mut right = left.to_vec(); - right.sort_by(|a, b| natsort(a, b, true)); + right.sort_by(|a, b| natsort(a.as_bytes(), b.as_bytes(), true)); assert_eq!(left, right); } @@ -179,98 +176,4 @@ mod tests { cmp(&fractions); cmp(&words); } - - // #[test] - // fn test_bench() { - // use std::time::Instant; - // - // let files = vec![ - // "pexels-asad-photo-maldives-1024967.jpg", - // "154586 (540p).mp4", - // "163333 (1080p).mp4", - // "166808 (540p).mp4", - // "178732 (1080p).mp4", - // "archive", - // "file.rs", - // "no copyright.pdf", - // "pexels-alex-fu-1302436.jpg", - // "pexels-alexander-grey-1191710.jpg", - // "pexels-benjamin-suter-2362002.jpg", - // "pexels-blaque-x-863963.jpg", - // "pexels-brakou-abdelghani-1723637.jpg", - // "pexels-chevanon-photography-1335971.jpg", - // "pexels-craig-adderley-1563356.jpg", - // "pexels-danne-516541.jpg", - // "pexels-eberhard-grossgasteiger-443446.jpg", - // "pexels-egil-sjøholt-1906658.jpg", - // "pexels-felix-mittermeier-2832041.jpg", - // "pexels-gabriel-peter-719396.jpg", - // "pexels-james-wheeler-1519088.jpg", - // "pexels-jonas-kakaroto-736230.jpg", - // "pexels-katie-burandt-1212693.jpg", - // "pexels-marta-branco-1173576.jpg", - // "pexels-matthew-montrone-1324803.jpg", - // "pexels-max-andrey-1366630.jpg", - // "pexels-nick-collins-1266741.jpg", - // "pexels-oliver-sjöström-1433052.jpg", - // "pexels-photomix-company-1002725.jpg", - // "pexels-pixabay-15239.jpg", - // "pexels-pixabay-33045.jpg", - // "pexels-pixabay-33101.jpg", - // "pexels-pixabay-33109.jpg", - // "pexels-pixabay-36717.jpg", - // "pexels-pixabay-36729.jpg", - // "pexels-pixabay-36762.jpg", - // "pexels-pixabay-45911.jpg", - // "pexels-pixabay-47334.jpg", - // "pexels-pixabay-50594.jpg", - // "pexels-pixabay-59990.jpg", - // "pexels-pixabay-60597.jpg", - // "pexels-pixabay-68507.jpg", - // "pexels-pixabay-158536.jpg", - // "pexels-pixabay-207088.jpg", - // "pexels-pixabay-327509.jpg", - // "pexels-pixabay-358457.jpg", - // "pexels-pixabay-372166.jpg", - // "pexels-pixabay-459203.jpg", - // "pexels-sevenstorm-juhaszimrus-891030.jpg", - // "pexels-steve-johnson-1266808.jpg", - // "pexels-suneo-103573.jpg", - // "pexels-tetyana-kovyrina-937980.jpg", - // "pexels-valeria-boltneva-1484657.jpg", - // "pexels-vlad-chețan-2604929.jpg", - // "pexels-wang-teck-heng-117139.jpg", - // "pexels-yuliya-strizhkina-1198802.jpg", - // "precache.rs", - // "scheduler.rs", - // "Symbols-0.73.0-x64.zip", - // "tasks.rs", - // ]; - // - // { - // let mut large1 = files.repeat(2000); - // let mut large2 = files.repeat(2000); - // - // let now = Instant::now(); - // large1.sort_unstable_by(|a, b| natord::compare_ignore_case(a, b)); - // println!("natord crate (insensitive) - Elapsed: {:.2?}", now.elapsed()); - // - // let now = Instant::now(); - // large2.sort_unstable_by(|a, b| natsort(a, b, true)); - // println!("Yazi (insensitive) - Elapsed: {:.2?}", now.elapsed()); - // } - // println!(); - // { - // let mut large1 = files.repeat(2000); - // let mut large2 = files.repeat(2000); - // - // let now = Instant::now(); - // large1.sort_unstable_by(|a, b| natord::compare(a, b)); - // println!("natord crate (sensitive) - Elapsed: {:.2?}", now.elapsed()); - // - // let now = Instant::now(); - // large2.sort_unstable_by(|a, b| natsort(a, b, false)); - // println!("Yazi (sensitive) - Elapsed: {:.2?}", now.elapsed()); - // } - // } }