This commit is contained in:
sxyazi 2024-01-27 15:07:56 +08:00
parent 84a0d148cd
commit 21b0caf93a
No known key found for this signature in database
6 changed files with 32 additions and 19 deletions

View file

@ -3,6 +3,7 @@ use std::{collections::{BTreeMap, HashMap, HashSet}, ffi::OsStr, mem, path::Path
use tokio::time::sleep;
use tracing::debug;
use yazi_config::{manager::SortBy, open::Opener, plugin::{PluginRule, MAX_PRELOADERS}, popup::InputCfg, OPEN, PLUGIN};
use yazi_plugin::ValueSendable;
use yazi_scheduler::{Scheduler, TaskSummary};
use yazi_shared::{emit, event::Exec, fs::{File, Url}, term::Term, Layer, MIME_DIR};
@ -145,9 +146,15 @@ impl Tasks {
});
}
pub fn plugin_micro(&self, name: &str) { self.scheduler.plugin_micro(name.to_owned()); }
#[inline]
pub fn plugin_micro(&self, name: String, args: Vec<ValueSendable>) {
self.scheduler.plugin_micro(name, args);
}
pub fn plugin_macro(&self, name: &str) { self.scheduler.plugin_macro(name.to_owned()); }
#[inline]
pub fn plugin_macro(&self, name: String, args: Vec<ValueSendable>) {
self.scheduler.plugin_macro(name, args);
}
pub fn preload_paged(&self, paged: &[File], mimetype: &HashMap<Url, String>) {
let mut single_tasks = Vec::with_capacity(paged.len());

View file

@ -8,13 +8,14 @@ use yazi_shared::{emit, event::Exec, Layer};
use crate::{app::App, lives::Lives};
impl App {
pub(crate) fn plugin(&mut self, opt: impl TryInto<yazi_plugin::Opt>) {
let Ok(opt) = opt.try_into() else {
return;
pub(crate) fn plugin(&mut self, opt: impl TryInto<yazi_plugin::Opt, Error = impl Display>) {
let opt = match opt.try_into() {
Ok(opt) => opt as yazi_plugin::Opt,
Err(e) => return warn!("{e}"),
};
if !opt.sync {
return self.cx.tasks.plugin_micro(&opt.name);
return self.cx.tasks.plugin_micro(opt.name, opt.data.args);
}
if LOADED.read().contains_key(&opt.name) {

View file

@ -1,17 +1,18 @@
use mlua::{ExternalError, ExternalResult, Table, TableExt};
use mlua::{ExternalError, ExternalResult, IntoLua, Table, TableExt, Variadic};
use tokio::runtime::Handle;
use super::slim_lua;
use crate::LOADED;
use crate::{ValueSendable, LOADED};
pub async fn entry(name: &str) -> mlua::Result<()> {
LOADED.ensure(name).await.into_lua_err()?;
pub async fn entry(name: String, args: Vec<ValueSendable>) -> mlua::Result<()> {
LOADED.ensure(&name).await.into_lua_err()?;
let name = name.to_owned();
tokio::task::spawn_blocking(move || {
let lua = slim_lua()?;
let args = Variadic::from_iter(args.into_iter().filter_map(|v| v.into_lua(&lua).ok()));
let plugin: Table = if let Some(b) = LOADED.read().get(&name) {
lua.load(b).call(())?
lua.load(b).call(args)?
} else {
return Err("unloaded plugin".into_lua_err());
};

View file

@ -1,3 +1,5 @@
use yazi_plugin::ValueSendable;
#[derive(Debug)]
pub enum PluginOp {
Entry(PluginOpEntry),
@ -11,8 +13,9 @@ impl PluginOp {
}
}
#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct PluginOpEntry {
pub id: usize,
pub name: String,
pub args: Vec<ValueSendable>,
}

View file

@ -21,7 +21,7 @@ impl Plugin {
pub async fn work(&self, op: PluginOp) -> Result<()> {
match op {
PluginOp::Entry(task) => {
isolate::entry(&task.name).await?;
isolate::entry(task.name, task.args).await?;
}
}
Ok(())
@ -30,7 +30,7 @@ impl Plugin {
pub async fn micro(&self, task: PluginOpEntry) -> Result<()> {
self.prog.send(TaskProg::New(task.id, 0))?;
if let Err(e) = isolate::entry(&task.name).await {
if let Err(e) = isolate::entry(task.name, task.args).await {
self.fail(task.id, format!("Micro plugin failed:\n{e}"))?;
return Err(e.into());
}

View file

@ -4,6 +4,7 @@ use futures::{future::BoxFuture, FutureExt};
use parking_lot::Mutex;
use tokio::{fs, select, sync::{mpsc::{self, UnboundedReceiver}, oneshot}};
use yazi_config::{open::Opener, plugin::PluginRule, TASKS};
use yazi_plugin::ValueSendable;
use yazi_shared::{emit, event::Exec, fs::{unique_path, Url}, Layer, Throttle};
use super::{Running, TaskProg, TaskStage};
@ -284,23 +285,23 @@ impl Scheduler {
);
}
pub fn plugin_micro(&self, name: String) {
pub fn plugin_micro(&self, name: String, args: Vec<ValueSendable>) {
let id = self.running.lock().add(TaskKind::User, format!("Run micro plugin `{name}`"));
let plugin = self.plugin.clone();
_ = self.micro.try_send(
async move {
plugin.micro(PluginOpEntry { id, name }).await.ok();
plugin.micro(PluginOpEntry { id, name, args }).await.ok();
}
.boxed(),
HIGH,
);
}
pub fn plugin_macro(&self, name: String) {
pub fn plugin_macro(&self, name: String, args: Vec<ValueSendable>) {
let id = self.running.lock().add(TaskKind::User, format!("Run macro plugin `{name}`"));
self.plugin.macro_(PluginOpEntry { id, name }).ok();
self.plugin.macro_(PluginOpEntry { id, name, args }).ok();
}
pub fn preload_paged(&self, rule: &PluginRule, targets: Vec<&yazi_shared::fs::File>) {