From 14a6a252ddeee4120a436e72e8056b3b45bc6674 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Tue, 6 Feb 2024 22:48:03 +0800 Subject: [PATCH] Simplify the code --- yazi-core/src/input/commands/backspace.rs | 8 ++------ yazi-core/src/input/commands/kill.rs | 10 +++++----- yazi-core/src/tab/commands/search.rs | 4 ---- yazi-plugin/src/url/url.rs | 2 +- yazi-shared/src/fs/url.rs | 11 ++++++----- 5 files changed, 14 insertions(+), 21 deletions(-) diff --git a/yazi-core/src/input/commands/backspace.rs b/yazi-core/src/input/commands/backspace.rs index 2806af13..3796771f 100644 --- a/yazi-core/src/input/commands/backspace.rs +++ b/yazi-core/src/input/commands/backspace.rs @@ -15,16 +15,12 @@ impl From for Opt { impl Input { pub fn backspace(&mut self, opt: impl Into) { - let opt = opt.into() as Opt; let snap = self.snaps.current_mut(); - if snap.value.is_empty() { - self.ticket = self.ticket.wrapping_add(1); - self.visible = false; - render!(); - return; + return self.close(false); } + let opt = opt.into() as Opt; if !opt.under && snap.cursor < 1 { return; } else if opt.under && snap.cursor >= snap.value.len() { diff --git a/yazi-core/src/input/commands/kill.rs b/yazi-core/src/input/commands/kill.rs index 3d0ae780..eeac2245 100644 --- a/yazi-core/src/input/commands/kill.rs +++ b/yazi-core/src/input/commands/kill.rs @@ -69,21 +69,21 @@ impl Input { let opt = opt.into() as Opt; let snap = self.snap_mut(); - match opt.kind.as_bytes() { - b"bol" => { + match opt.kind.as_str() { + "bol" => { let end = snap.idx(snap.cursor).unwrap_or(snap.len()); self.kill_range(..end) } - b"eol" => { + "eol" => { let start = snap.idx(snap.cursor).unwrap_or(snap.len()); self.kill_range(start..) } - b"backward" => { + "backward" => { let end = snap.idx(snap.cursor).unwrap_or(snap.len()); let start = end - Self::find_word_boundary(snap.value[..end].chars().rev()); self.kill_range(start..end) } - b"forward" => { + "forward" => { let start = snap.idx(snap.cursor).unwrap_or(snap.len()); let end = start + Self::find_word_boundary(snap.value[start..].chars()); self.kill_range(start..end) diff --git a/yazi-core/src/tab/commands/search.rs b/yazi-core/src/tab/commands/search.rs index 22a5d9fa..ae8c4640 100644 --- a/yazi-core/src/tab/commands/search.rs +++ b/yazi-core/src/tab/commands/search.rs @@ -63,10 +63,6 @@ impl Tab { let mut input = Input::_show(InputCfg::search(&opt.type_.to_string())); let Some(Ok(subject)) = input.recv().await else { bail!("") }; - if subject.is_empty() { - return Ok(()); - } - cwd = cwd.into_search(subject.clone()); let rx = if opt.type_ == OptType::Rg { external::rg(external::RgOpt { cwd: cwd.clone(), hidden, subject }) diff --git a/yazi-plugin/src/url/url.rs b/yazi-plugin/src/url/url.rs index 36755cae..e6301f22 100644 --- a/yazi-plugin/src/url/url.rs +++ b/yazi-plugin/src/url/url.rs @@ -9,7 +9,7 @@ pub struct Url; impl Url { pub fn register(lua: &Lua) -> mlua::Result<()> { lua.register_userdata_type::(|reg| { - reg.add_field_method_get("frag", |_, me| Ok(me.frag().map(ToOwned::to_owned))); + reg.add_field_method_get("frag", |lua, me| lua.create_string(me.frag())); reg.add_field_method_get("is_regular", |_, me| Ok(me.is_regular())); reg.add_field_method_get("is_search", |_, me| Ok(me.is_search())); reg.add_field_method_get("is_archive", |_, me| Ok(me.is_archive())); diff --git a/yazi-shared/src/fs/url.rs b/yazi-shared/src/fs/url.rs index 46dea1ab..366a1001 100644 --- a/yazi-shared/src/fs/url.rs +++ b/yazi-shared/src/fs/url.rs @@ -8,7 +8,7 @@ const ENCODE_SET: &AsciiSet = &CONTROLS.add(b'#'); pub struct Url { scheme: UrlScheme, path: PathBuf, - frag: Option, + frag: String, } #[derive(Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] @@ -76,7 +76,7 @@ impl From<&str> for Url { } Some((a, b)) => { url.path = percent_decode_str(a).decode_utf8_lossy().into_owned().into(); - url.frag = Some(b.to_string()).filter(|s| !s.is_empty()); + url.frag = b.to_string(); } } url @@ -108,7 +108,8 @@ impl ToString for Url { }; let path = percent_encode(self.path.as_os_str().as_encoded_bytes(), ENCODE_SET); - let frag = self.frag.as_ref().map(|s| format!("#{s}")).unwrap_or_default(); + let frag = + Some(&self.frag).filter(|&s| !s.is_empty()).map(|s| format!("#{s}")).unwrap_or_default(); format!("{scheme}{path}{frag}") } } @@ -173,7 +174,7 @@ impl Url { #[inline] pub fn into_search(mut self, frag: String) -> Self { self.scheme = UrlScheme::Search; - self.frag = Some(frag); + self.frag = frag; self } @@ -195,7 +196,7 @@ impl Url { // --- Frag #[inline] - pub fn frag(&self) -> Option<&str> { self.frag.as_deref() } + pub fn frag(&self) -> &str { &self.frag } } impl From<&str> for UrlScheme {