From b71bd8a30e783d50b06a86be3a889d510d3b8f57 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sat, 2 Mar 2024 14:39:02 +0800 Subject: [PATCH] feat: `ya.input()` plugin API --- Cargo.lock | 12 ++++++++++++ yazi-core/Cargo.toml | 1 + yazi-core/src/manager/commands/open.rs | 2 +- yazi-core/src/manager/commands/rename.rs | 7 ++++--- yazi-core/src/manager/commands/suspend.rs | 4 ++-- yazi-core/src/tab/commands/jump.rs | 7 ++++--- yazi-core/src/tasks/commands/inspect.rs | 7 ++++--- yazi-fm/Cargo.toml | 1 + yazi-fm/src/help/layout.rs | 2 +- yazi-fm/src/signals.rs | 4 ++-- yazi-plugin/src/utils/layer.rs | 15 +++++++++++++++ yazi-proxy/Cargo.toml | 16 ++++++++++++++++ yazi-proxy/src/app.rs | 16 ++++++++++++++++ yazi-proxy/src/lib.rs | 3 +++ yazi-scheduler/Cargo.toml | 3 ++- yazi-scheduler/src/process/process.rs | 7 ++++--- yazi-scheduler/src/scheduler.rs | 12 +----------- 17 files changed, 89 insertions(+), 30 deletions(-) create mode 100644 yazi-proxy/Cargo.toml create mode 100644 yazi-proxy/src/app.rs create mode 100644 yazi-proxy/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index d800175d..1abbf061 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2740,6 +2740,7 @@ dependencies = [ "yazi-boot", "yazi-config", "yazi-plugin", + "yazi-proxy", "yazi-scheduler", "yazi-shared", ] @@ -2770,6 +2771,7 @@ dependencies = [ "yazi-config", "yazi-core", "yazi-plugin", + "yazi-proxy", "yazi-scheduler", "yazi-shared", ] @@ -2810,6 +2812,15 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4b6c8e12e39ac0f79fa96f36e5b88e0da8d230691abd729eec709b43c74f632" +[[package]] +name = "yazi-proxy" +version = "0.2.3" +dependencies = [ + "tokio", + "yazi-config", + "yazi-shared", +] + [[package]] name = "yazi-scheduler" version = "0.2.3" @@ -2828,6 +2839,7 @@ dependencies = [ "yazi-adaptor", "yazi-config", "yazi-plugin", + "yazi-proxy", "yazi-shared", ] diff --git a/yazi-core/Cargo.toml b/yazi-core/Cargo.toml index f6175b7b..c96dc59b 100644 --- a/yazi-core/Cargo.toml +++ b/yazi-core/Cargo.toml @@ -13,6 +13,7 @@ yazi-adaptor = { path = "../yazi-adaptor", version = "0.2.3" } yazi-boot = { path = "../yazi-boot", version = "0.2.3" } yazi-config = { path = "../yazi-config", version = "0.2.3" } yazi-plugin = { path = "../yazi-plugin", version = "0.2.3" } +yazi-proxy = { path = "../yazi-proxy", version = "0.2.3" } yazi-scheduler = { path = "../yazi-scheduler", version = "0.2.3" } yazi-shared = { path = "../yazi-shared", version = "0.2.3" } diff --git a/yazi-core/src/manager/commands/open.rs b/yazi-core/src/manager/commands/open.rs index 34dae598..40fc9875 100644 --- a/yazi-core/src/manager/commands/open.rs +++ b/yazi-core/src/manager/commands/open.rs @@ -50,7 +50,7 @@ impl Manager { let (mut done, mut todo) = (Vec::with_capacity(selected.len()), vec![]); for u in selected { - if self.mimetype.get(u).is_some() { + if self.mimetype.contains_key(u) { done.push((u.clone(), String::new())); } else if self.guess_folder(u) { done.push((u.clone(), MIME_DIR.to_owned())); diff --git a/yazi-core/src/manager/commands/rename.rs b/yazi-core/src/manager/commands/rename.rs index fd53d416..cb146360 100644 --- a/yazi-core/src/manager/commands/rename.rs +++ b/yazi-core/src/manager/commands/rename.rs @@ -4,7 +4,8 @@ use anyhow::{anyhow, bail, Result}; use tokio::{fs::{self, OpenOptions}, io::{stdin, AsyncReadExt, AsyncWriteExt}}; use yazi_config::{popup::InputCfg, OPEN, PREVIEW}; use yazi_plugin::external::{self, ShellOpt}; -use yazi_scheduler::{Scheduler, BLOCKER}; +use yazi_proxy::App; +use yazi_scheduler::BLOCKER; use yazi_shared::{event::Cmd, fs::{max_common_root, File, FilesOp, Url}, term::Term, Defer}; use crate::{input::Input, manager::Manager}; @@ -122,10 +123,10 @@ impl Manager { let _guard = BLOCKER.acquire().await.unwrap(); let _defer = Defer::new(|| { - Scheduler::app_resume(); + App::resume(); tokio::spawn(fs::remove_file(tmp.clone())) }); - Scheduler::app_stop().await; + App::stop().await; let mut child = external::shell(ShellOpt { cmd: (*opener.exec).into(), diff --git a/yazi-core/src/manager/commands/suspend.rs b/yazi-core/src/manager/commands/suspend.rs index 429d11e8..304c7d67 100644 --- a/yazi-core/src/manager/commands/suspend.rs +++ b/yazi-core/src/manager/commands/suspend.rs @@ -1,4 +1,4 @@ -use yazi_scheduler::Scheduler; +use yazi_proxy::App; use yazi_shared::event::Cmd; use crate::manager::Manager; @@ -7,7 +7,7 @@ impl Manager { pub fn suspend(&mut self, _: Cmd) { #[cfg(unix)] tokio::spawn(async move { - Scheduler::app_stop().await; + App::stop().await; unsafe { libc::raise(libc::SIGTSTP) }; }); } diff --git a/yazi-core/src/tab/commands/jump.rs b/yazi-core/src/tab/commands/jump.rs index c282205d..edb6f471 100644 --- a/yazi-core/src/tab/commands/jump.rs +++ b/yazi-core/src/tab/commands/jump.rs @@ -1,5 +1,6 @@ use yazi_plugin::external::{self, FzfOpt, ZoxideOpt}; -use yazi_scheduler::{Scheduler, BLOCKER}; +use yazi_proxy::App; +use yazi_scheduler::BLOCKER; use yazi_shared::{event::Cmd, fs::ends_with_slash, Defer}; use crate::tab::Tab; @@ -37,8 +38,8 @@ impl Tab { let cwd = self.current.cwd.clone(); tokio::spawn(async move { let _guard = BLOCKER.acquire().await.unwrap(); - let _defer = Defer::new(Scheduler::app_resume); - Scheduler::app_stop().await; + let _defer = Defer::new(App::resume); + App::stop().await; let result = if opt.type_ == OptType::Fzf { external::fzf(FzfOpt { cwd }).await diff --git a/yazi-core/src/tasks/commands/inspect.rs b/yazi-core/src/tasks/commands/inspect.rs index d48ab454..09d47e94 100644 --- a/yazi-core/src/tasks/commands/inspect.rs +++ b/yazi-core/src/tasks/commands/inspect.rs @@ -2,7 +2,8 @@ use std::io::{stdout, Write}; use crossterm::terminal::{disable_raw_mode, enable_raw_mode}; use tokio::{io::{stdin, AsyncReadExt}, select, sync::mpsc, time}; -use yazi_scheduler::{Scheduler, BLOCKER}; +use yazi_proxy::App; +use yazi_scheduler::BLOCKER; use yazi_shared::{event::Cmd, term::Term, Defer}; use crate::tasks::Tasks; @@ -26,10 +27,10 @@ impl Tasks { task.logs.clone() }; - Scheduler::app_stop().await; + App::stop().await; let _defer = Defer::new(|| { disable_raw_mode().ok(); - Scheduler::app_resume(); + App::resume(); }); Term::clear(&mut stdout()).ok(); diff --git a/yazi-fm/Cargo.toml b/yazi-fm/Cargo.toml index 30e4ac0e..e95d31df 100644 --- a/yazi-fm/Cargo.toml +++ b/yazi-fm/Cargo.toml @@ -14,6 +14,7 @@ yazi-boot = { path = "../yazi-boot", version = "0.2.3" } yazi-config = { path = "../yazi-config", version = "0.2.3" } yazi-core = { path = "../yazi-core", version = "0.2.3" } yazi-plugin = { path = "../yazi-plugin", version = "0.2.3" } +yazi-proxy = { path = "../yazi-proxy", version = "0.2.3" } yazi-scheduler = { path = "../yazi-scheduler", version = "0.2.3" } yazi-shared = { path = "../yazi-shared", version = "0.2.3" } diff --git a/yazi-fm/src/help/layout.rs b/yazi-fm/src/help/layout.rs index 1d78c169..633b3d67 100644 --- a/yazi-fm/src/help/layout.rs +++ b/yazi-fm/src/help/layout.rs @@ -19,7 +19,7 @@ impl<'a> Widget for Layout<'a> { let chunks = layout::Layout::vertical([Constraint::Fill(1), Constraint::Length(1)]).split(area); Line::styled( - help.keyword().unwrap_or_else(|| format!("{}.help", help.layer.to_string())), + help.keyword().unwrap_or_else(|| format!("{}.help", help.layer)), THEME.help.footer, ) .render(chunks[1], buf); diff --git a/yazi-fm/src/signals.rs b/yazi-fm/src/signals.rs index d7f93891..40ace180 100644 --- a/yazi-fm/src/signals.rs +++ b/yazi-fm/src/signals.rs @@ -43,7 +43,7 @@ impl Signals { #[cfg(unix)] fn spawn_system_task(&self) -> Result> { use libc::{SIGCONT, SIGHUP, SIGINT, SIGQUIT, SIGTERM}; - use yazi_scheduler::{Scheduler, BLOCKER}; + use yazi_scheduler::BLOCKER; let mut signals = signal_hook_tokio::Signals::new([ // Terminating signals @@ -65,7 +65,7 @@ impl Signals { break; } } - SIGCONT => Scheduler::app_resume(), + SIGCONT => yazi_proxy::App::resume(), _ => {} } } diff --git a/yazi-plugin/src/utils/layer.rs b/yazi-plugin/src/utils/layer.rs index d71b54bc..a8917ebd 100644 --- a/yazi-plugin/src/utils/layer.rs +++ b/yazi-plugin/src/utils/layer.rs @@ -53,6 +53,21 @@ impl Utils { })?, )?; + ya.raw_set( + "input", + lua.create_async_function(|_, t: Table| async move { + // pub title: String, + // pub value: String, + // pub cursor: Option, + // pub position: Position, + // pub realtime: bool, + // pub completion: bool, + // pub highlight: bool, + + Ok(()) + })?, + )?; + Ok(()) } } diff --git a/yazi-proxy/Cargo.toml b/yazi-proxy/Cargo.toml new file mode 100644 index 00000000..fbbb4753 --- /dev/null +++ b/yazi-proxy/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "yazi-proxy" +version = "0.2.3" +edition = "2021" +license = "MIT" +authors = [ "sxyazi " ] +description = "Yazi event proxy" +homepage = "https://yazi-rs.github.io" +repository = "https://github.com/sxyazi/yazi" + +[dependencies] +yazi-config = { path = "../yazi-config", version = "0.2.3" } +yazi-shared = { path = "../yazi-shared", version = "0.2.3" } + +# External dependencies +tokio = { version = "^1", features = [ "parking_lot" ] } diff --git a/yazi-proxy/src/app.rs b/yazi-proxy/src/app.rs new file mode 100644 index 00000000..18025033 --- /dev/null +++ b/yazi-proxy/src/app.rs @@ -0,0 +1,16 @@ +use tokio::sync::oneshot; +use yazi_shared::{emit, event::Cmd, Layer}; + +pub struct App; + +impl App { + pub async fn stop() { + let (tx, rx) = oneshot::channel::<()>(); + emit!(Call(Cmd::new("stop").with_data(tx), Layer::App)); + rx.await.ok(); + } + + pub fn resume() { + emit!(Call(Cmd::new("resume"), Layer::App)); + } +} diff --git a/yazi-proxy/src/lib.rs b/yazi-proxy/src/lib.rs new file mode 100644 index 00000000..73290375 --- /dev/null +++ b/yazi-proxy/src/lib.rs @@ -0,0 +1,3 @@ +mod app; + +pub use app::*; diff --git a/yazi-scheduler/Cargo.toml b/yazi-scheduler/Cargo.toml index 104ff99c..e6488457 100644 --- a/yazi-scheduler/Cargo.toml +++ b/yazi-scheduler/Cargo.toml @@ -11,8 +11,9 @@ repository = "https://github.com/sxyazi/yazi" [dependencies] yazi-adaptor = { path = "../yazi-adaptor", version = "0.2.3" } yazi-config = { path = "../yazi-config", version = "0.2.3" } -yazi-shared = { path = "../yazi-shared", version = "0.2.3" } yazi-plugin = { path = "../yazi-plugin", version = "0.2.3" } +yazi-proxy = { path = "../yazi-proxy", version = "0.2.3" } +yazi-shared = { path = "../yazi-shared", version = "0.2.3" } # External dependencies anyhow = "^1" diff --git a/yazi-scheduler/src/process/process.rs b/yazi-scheduler/src/process/process.rs index 2431db80..4535fc61 100644 --- a/yazi-scheduler/src/process/process.rs +++ b/yazi-scheduler/src/process/process.rs @@ -1,9 +1,10 @@ use anyhow::Result; use tokio::{io::{AsyncBufReadExt, BufReader}, select, sync::mpsc}; use yazi_plugin::external::{self, ShellOpt}; +use yazi_proxy::App; use super::ProcessOpOpen; -use crate::{Scheduler, TaskProg, BLOCKER}; +use crate::{TaskProg, BLOCKER}; pub struct Process { prog: mpsc::UnboundedSender, @@ -16,7 +17,7 @@ impl Process { let opt = ShellOpt::from(&mut task); if task.block { let _guard = BLOCKER.acquire().await.unwrap(); - Scheduler::app_stop().await; + App::stop().await; match external::shell(opt) { Ok(mut child) => { @@ -28,7 +29,7 @@ impl Process { self.fail(task.id, format!("Failed to spawn process: {e}"))?; } } - return Ok(Scheduler::app_resume()); + return Ok(App::resume()); } if task.orphan { diff --git a/yazi-scheduler/src/scheduler.rs b/yazi-scheduler/src/scheduler.rs index fc6dbf4c..9870b2ea 100644 --- a/yazi-scheduler/src/scheduler.rs +++ b/yazi-scheduler/src/scheduler.rs @@ -5,7 +5,7 @@ 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::Cmd, fs::{unique_path, Url}, Layer, Throttle}; +use yazi_shared::{fs::{unique_path, Url}, Throttle}; use super::{Running, TaskProg, TaskStage}; use crate::{file::{File, FileOpDelete, FileOpLink, FileOpPaste, FileOpTrash}, plugin::{Plugin, PluginOpEntry}, preload::{Preload, PreloadOpRule, PreloadOpSize}, process::{Process, ProcessOpOpen}, TaskKind, TaskOp, HIGH, LOW, NORMAL}; @@ -163,16 +163,6 @@ impl Scheduler { b } - pub async fn app_stop() { - let (tx, rx) = oneshot::channel::<()>(); - emit!(Call(Cmd::new("stop").with_data(tx), Layer::App)); - rx.await.ok(); - } - - pub fn app_resume() { - emit!(Call(Cmd::new("resume"), Layer::App)); - } - pub fn file_cut(&self, from: Url, mut to: Url, force: bool) { let mut running = self.running.lock(); let id = running.add(TaskKind::User, format!("Cut {:?} to {:?}", from, to));