From acb8b47eee00fe03fa24539e436549ce91e665ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Mon, 22 Jan 2024 14:25:07 +0800 Subject: [PATCH] feat: improve search input title (#559) --- yazi-config/preset/yazi.toml | 2 +- yazi-config/src/popup/options.rs | 4 +-- yazi-core/src/tab/commands/search.rs | 40 +++++++++++++++++++--------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/yazi-config/preset/yazi.toml b/yazi-config/preset/yazi.toml index a249d694..b4cfd9e6 100644 --- a/yazi-config/preset/yazi.toml +++ b/yazi-config/preset/yazi.toml @@ -156,7 +156,7 @@ find_origin = "top-center" find_offset = [ 0, 2, 50, 3 ] # search -search_title = "Search:" +search_title = "Search via {n}:" search_origin = "top-center" search_offset = [ 0, 2, 50, 3 ] diff --git a/yazi-config/src/popup/options.rs b/yazi-config/src/popup/options.rs index 21b2c108..2c7e72c6 100644 --- a/yazi-config/src/popup/options.rs +++ b/yazi-config/src/popup/options.rs @@ -89,9 +89,9 @@ impl InputCfg { } #[inline] - pub fn search() -> Self { + pub fn search(name: &str) -> Self { Self { - title: INPUT.search_title.to_owned(), + title: INPUT.search_title.replace("{n}", name), position: Position::new(INPUT.search_origin, INPUT.search_offset), ..Default::default() } diff --git a/yazi-core/src/tab/commands/search.rs b/yazi-core/src/tab/commands/search.rs index 8bdf0f3d..861b37f2 100644 --- a/yazi-core/src/tab/commands/search.rs +++ b/yazi-core/src/tab/commands/search.rs @@ -9,10 +9,6 @@ use yazi_shared::{event::Exec, fs::FilesOp, render}; use crate::{input::Input, manager::Manager, tab::Tab}; -pub struct Opt { - pub type_: OptType, -} - #[derive(PartialEq, Eq)] pub enum OptType { None, @@ -20,15 +16,34 @@ pub enum OptType { Fd, } +impl From<&str> for OptType { + fn from(value: &str) -> Self { + match value { + "rg" => Self::Rg, + "fd" => Self::Fd, + _ => Self::None, + } + } +} + +impl ToString for OptType { + fn to_string(&self) -> String { + match self { + Self::Rg => "rg", + Self::Fd => "fd", + Self::None => "none", + } + .to_owned() + } +} + +pub struct Opt { + pub type_: OptType, +} + impl From<&Exec> for Opt { fn from(e: &Exec) -> Self { - Self { - type_: match e.args.first().map(|s| s.as_str()) { - Some("fd") => OptType::Fd, - Some("rg") => OptType::Rg, - _ => OptType::None, - }, - } + Self { type_: e.args.first().map(|s| s.as_str()).unwrap_or_default().into() } } } @@ -47,7 +62,8 @@ impl Tab { let hidden = self.conf.show_hidden; self.search = Some(tokio::spawn(async move { - let Some(Ok(subject)) = Input::_show(InputCfg::search()).recv().await else { bail!("") }; + let mut input = Input::_show(InputCfg::search(&opt.type_.to_string())); + let Some(Ok(subject)) = input.recv().await else { bail!("") }; cwd = cwd.into_search(subject.clone()); let rx = if opt.type_ == OptType::Rg {