feat: ya.input() plugin API

This commit is contained in:
sxyazi 2024-03-02 14:39:02 +08:00
parent b39b506e27
commit b71bd8a30e
No known key found for this signature in database
17 changed files with 89 additions and 30 deletions

12
Cargo.lock generated
View file

@ -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",
]

View file

@ -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" }

View file

@ -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()));

View file

@ -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(),

View file

@ -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) };
});
}

View file

@ -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

View file

@ -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();

View file

@ -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" }

View file

@ -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);

View file

@ -43,7 +43,7 @@ impl Signals {
#[cfg(unix)]
fn spawn_system_task(&self) -> Result<JoinHandle<()>> {
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(),
_ => {}
}
}

View file

@ -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<usize>,
// pub position: Position,
// pub realtime: bool,
// pub completion: bool,
// pub highlight: bool,
Ok(())
})?,
)?;
Ok(())
}
}

16
yazi-proxy/Cargo.toml Normal file
View file

@ -0,0 +1,16 @@
[package]
name = "yazi-proxy"
version = "0.2.3"
edition = "2021"
license = "MIT"
authors = [ "sxyazi <sxyazi@gmail.com>" ]
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" ] }

16
yazi-proxy/src/app.rs Normal file
View file

@ -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));
}
}

3
yazi-proxy/src/lib.rs Normal file
View file

@ -0,0 +1,3 @@
mod app;
pub use app::*;

View file

@ -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"

View file

@ -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<TaskProg>,
@ -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 {

View file

@ -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));