-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathfd.rs
39 lines (34 loc) · 995 Bytes
/
fd.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::process::Stdio;
use anyhow::Result;
use tokio::{io::{AsyncBufReadExt, BufReader}, process::Command, sync::mpsc::{self, UnboundedReceiver}};
use yazi_shared::fs::{File, Url};
pub struct FdOpt {
pub cwd: Url,
pub hidden: bool,
pub subject: String,
pub args: Vec<String>,
}
pub fn fd(opt: FdOpt) -> Result<UnboundedReceiver<File>> {
let mut child = Command::new("fd")
.arg("--base-directory")
.arg(&opt.cwd)
.arg("--regex")
.args(if opt.hidden { ["--hidden", "--no-ignore"] } else { ["--no-hidden", "--ignore"] })
.args(opt.args)
.arg(opt.subject)
.kill_on_drop(true)
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()?;
let mut it = BufReader::new(child.stdout.take().unwrap()).lines();
let (tx, rx) = mpsc::unbounded_channel();
tokio::spawn(async move {
while let Ok(Some(line)) = it.next_line().await {
if let Ok(file) = File::from(opt.cwd.join(line)).await {
tx.send(file).ok();
}
}
child.wait().await.ok();
});
Ok(rx)
}