mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-23 15:51:03 +00:00
33 lines
1,008 B
Rust
33 lines
1,008 B
Rust
use mlua::{ExternalError, ExternalResult, Table, TableExt};
|
|
use tokio::runtime::Handle;
|
|
use yazi_config::LAYOUT;
|
|
|
|
use super::slim_lua;
|
|
use crate::{bindings::{Cast, File}, elements::Rect, loader::LOADER};
|
|
|
|
pub async fn prefetch(name: &str, files: Vec<yazi_shared::fs::File>) -> mlua::Result<u8> {
|
|
LOADER.ensure(name).await.into_lua_err()?;
|
|
|
|
let name = name.to_owned();
|
|
tokio::task::spawn_blocking(move || {
|
|
let lua = slim_lua(&name)?;
|
|
let plugin: Table = if let Some(b) = LOADER.read().get(&name) {
|
|
lua.load(b.as_ref()).call(())?
|
|
} else {
|
|
return Err("unloaded plugin".into_lua_err());
|
|
};
|
|
|
|
let files = files.into_iter().filter_map(|f| File::cast(&lua, f).ok()).collect::<Vec<_>>();
|
|
if files.is_empty() {
|
|
return Err("no files".into_lua_err());
|
|
}
|
|
|
|
plugin.raw_set("skip", 0)?;
|
|
plugin.raw_set("area", Rect::cast(&lua, LAYOUT.load().preview)?)?;
|
|
plugin.raw_set("files", files)?;
|
|
|
|
Handle::current().block_on(plugin.call_async_method("prefetch", ()))
|
|
})
|
|
.await
|
|
.into_lua_err()?
|
|
}
|