mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-21 23:01:05 +00:00
feat: add ripgrep-all support for search (#2383)
Co-authored-by: 三咲雅 · Misaki Masa <sxyazi@gmail.com>
This commit is contained in:
parent
36ed32d2c3
commit
6ca27597f9
4 changed files with 56 additions and 7 deletions
|
|
@ -1,5 +1,6 @@
|
|||
use std::{borrow::Cow, mem, time::Duration};
|
||||
|
||||
use anyhow::bail;
|
||||
use tokio::pin;
|
||||
use tokio_stream::{StreamExt, wrappers::UnboundedReceiverStream};
|
||||
use tracing::error;
|
||||
|
|
@ -48,20 +49,26 @@ impl Tab {
|
|||
let hidden = self.pref.show_hidden;
|
||||
|
||||
self.search = Some(tokio::spawn(async move {
|
||||
let rx = if opt.via == SearchOptVia::Rg {
|
||||
external::rg(external::RgOpt {
|
||||
let rx = match opt.via {
|
||||
SearchOptVia::Rg => external::rg(external::RgOpt {
|
||||
cwd: cwd.clone(),
|
||||
hidden,
|
||||
subject: opt.subject.into_owned(),
|
||||
args: opt.args,
|
||||
})
|
||||
} else {
|
||||
external::fd(external::FdOpt {
|
||||
}),
|
||||
SearchOptVia::Rga => external::rga(external::RgaOpt {
|
||||
cwd: cwd.clone(),
|
||||
hidden,
|
||||
subject: opt.subject.into_owned(),
|
||||
args: opt.args,
|
||||
})
|
||||
}),
|
||||
SearchOptVia::Fd => external::fd(external::FdOpt {
|
||||
cwd: cwd.clone(),
|
||||
hidden,
|
||||
subject: opt.subject.into_owned(),
|
||||
args: opt.args,
|
||||
}),
|
||||
SearchOptVia::None => bail!("Invalid `via` option for `search` command"),
|
||||
}?;
|
||||
|
||||
let rx = UnboundedReceiverStream::new(rx).chunks_timeout(5000, Duration::from_millis(500));
|
||||
|
|
|
|||
2
yazi-plugin/src/external/mod.rs
vendored
2
yazi-plugin/src/external/mod.rs
vendored
|
|
@ -1 +1 @@
|
|||
yazi_macro::mod_flat!(fd highlighter rg);
|
||||
yazi_macro::mod_flat!(fd highlighter rg rga);
|
||||
|
|
|
|||
39
yazi-plugin/src/external/rga.rs
vendored
Normal file
39
yazi-plugin/src/external/rga.rs
vendored
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
use std::process::Stdio;
|
||||
|
||||
use anyhow::Result;
|
||||
use tokio::{io::{AsyncBufReadExt, BufReader}, process::Command, sync::mpsc::{self, UnboundedReceiver}};
|
||||
use yazi_fs::File;
|
||||
use yazi_shared::url::Url;
|
||||
|
||||
pub struct RgaOpt {
|
||||
pub cwd: Url,
|
||||
pub hidden: bool,
|
||||
pub subject: String,
|
||||
pub args: Vec<String>,
|
||||
}
|
||||
|
||||
pub fn rga(opt: RgaOpt) -> Result<UnboundedReceiver<File>> {
|
||||
let mut child = Command::new("rga")
|
||||
.args(["--color=never", "--files-with-matches", "--smart-case"])
|
||||
.arg(if opt.hidden { "--hidden" } else { "--no-hidden" })
|
||||
.args(opt.args)
|
||||
.arg(opt.subject)
|
||||
.arg(&opt.cwd)
|
||||
.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)
|
||||
}
|
||||
|
|
@ -37,6 +37,7 @@ pub enum SearchOptVia {
|
|||
None,
|
||||
Rg,
|
||||
Fd,
|
||||
Rga,
|
||||
}
|
||||
|
||||
impl From<&str> for SearchOptVia {
|
||||
|
|
@ -44,6 +45,7 @@ impl From<&str> for SearchOptVia {
|
|||
match value {
|
||||
"rg" => Self::Rg,
|
||||
"fd" => Self::Fd,
|
||||
"rga" => Self::Rga,
|
||||
_ => Self::None,
|
||||
}
|
||||
}
|
||||
|
|
@ -54,6 +56,7 @@ impl Display for SearchOptVia {
|
|||
f.write_str(match self {
|
||||
Self::Rg => "rg",
|
||||
Self::Fd => "fd",
|
||||
Self::Rga => "rga",
|
||||
Self::None => "none",
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue