use std::fmt::Display;
use yazi_shared::event::Cmd;
pub struct SearchOpt {
pub via: SearchOptVia,
pub subject: String,
pub args: Vec<String>,
pub args_raw: String,
}
impl TryFrom<Cmd> for SearchOpt {
type Error = ();
fn try_from(mut c: Cmd) -> Result<Self, Self::Error> {
Ok(Self {
via: c.take_str("via").or_else(|| c.take_first_str()).unwrap_or_default().into(),
subject: c.take_first_str().unwrap_or_default(),
args: shell_words::split(c.str("args").unwrap_or_default()).map_err(|_| ())?,
args_raw: c.take_str("args").unwrap_or_default(),
})
}
}
#[derive(PartialEq, Eq)]
pub enum SearchOptVia {
None,
Rg,
Fd,
}
impl From<String> for SearchOptVia {
fn from(value: String) -> Self {
match value.as_str() {
"rg" => Self::Rg,
"fd" => Self::Fd,
_ => Self::None,
}
}
}
impl Display for SearchOptVia {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::Rg => "rg",
Self::Fd => "fd",
Self::None => "none",
})
}
}