diff --git a/yazi-dds/src/sendable.rs b/yazi-dds/src/sendable.rs index 1be46ea6..1a7f6328 100644 --- a/yazi-dds/src/sendable.rs +++ b/yazi-dds/src/sendable.rs @@ -156,7 +156,7 @@ impl Sendable { for (k, v) in args { match k { DataKey::Integer(i) => tbl.raw_set(i + 1, Self::data_to_value(lua, v)?), - DataKey::String(s) => tbl.raw_set(replace_cow(&s, "-", "_"), Self::data_to_value(lua, v)?), + DataKey::String(s) => tbl.raw_set(replace_cow(s, "-", "_"), Self::data_to_value(lua, v)?), _ => Err("invalid key in Data".into_lua_err()), }?; } @@ -170,7 +170,7 @@ impl Sendable { match k { DataKey::Integer(i) => tbl.raw_set(i + 1, Self::data_to_value_ref(lua, v)?), DataKey::String(s) => { - tbl.raw_set(replace_cow(s, "-", "_"), Self::data_to_value_ref(lua, v)?) + tbl.raw_set(replace_cow(s.as_ref(), "-", "_"), Self::data_to_value_ref(lua, v)?) } _ => Err("invalid key in Data".into_lua_err()), }?; diff --git a/yazi-fs/src/mounts/linux.rs b/yazi-fs/src/mounts/linux.rs index 980c81ed..d93c84ae 100644 --- a/yazi-fs/src/mounts/linux.rs +++ b/yazi-fs/src/mounts/linux.rs @@ -169,10 +169,7 @@ impl Partitions { for (a, b) in [(r"\011", "\t"), (r"\012", "\n"), (r"\040", " "), (r"\043", "#"), (r"\134", r"\")] { - s = match replace_cow(&s, a, b) { - Cow::Borrowed(_) => s, - Cow::Owned(new) => Cow::Owned(new), - }; + s = replace_cow(s, a, b); } s } diff --git a/yazi-shared/src/chars.rs b/yazi-shared/src/chars.rs index 0d7983eb..319ae580 100644 --- a/yazi-shared/src/chars.rs +++ b/yazi-shared/src/chars.rs @@ -33,35 +33,48 @@ pub fn strip_trailing_newline(mut s: String) -> String { s } -pub fn replace_cow<'a>(s: &'a str, from: &str, to: &str) -> Cow<'a, str> { - replace_cow_impl(s, s.match_indices(from), to) +pub fn replace_cow<'a, T>(s: T, from: &str, to: &str) -> Cow<'a, str> +where + T: Into>, +{ + let cow = s.into(); + match replace_cow_impl(&cow, cow.match_indices(from), to) { + Cow::Borrowed(_) => cow, + Cow::Owned(new) => Cow::Owned(new), + } } -pub fn replacen_cow<'a>(s: &'a str, from: &str, to: &str, n: usize) -> Cow<'a, str> { - replace_cow_impl(s, s.match_indices(from).take(n), to) +pub fn replacen_cow<'a, T>(s: T, from: &str, to: &str, n: usize) -> Cow<'a, str> +where + T: Into>, +{ + let cow = s.into(); + match replace_cow_impl(&cow, cow.match_indices(from).take(n), to) { + Cow::Borrowed(_) => cow, + Cow::Owned(now) => Cow::Owned(now), + } } -fn replace_cow_impl<'s>( - src: &'s str, - mut indices: impl Iterator, - to: &str, -) -> Cow<'s, str> { +fn replace_cow_impl<'a, T>(src: &'a str, mut indices: T, to: &str) -> Cow<'a, str> +where + T: Iterator, +{ let Some((first_idx, first_sub)) = indices.next() else { return Cow::Borrowed(src); }; - let mut result = Cow::Owned(String::with_capacity(src.len())); + let mut result = String::with_capacity(src.len()); result += unsafe { src.get_unchecked(..first_idx) }; - result.to_mut().push_str(to); + result += to; let mut last = first_idx + first_sub.len(); for (idx, sub) in indices { result += unsafe { src.get_unchecked(last..idx) }; - result.to_mut().push_str(to); + result += to; last = idx + sub.len(); } - result + unsafe { src.get_unchecked(last..) } + Cow::Owned(result + unsafe { src.get_unchecked(last..) }) } pub fn replace_vec_cow<'a>(v: &'a [u8], from: &[u8], to: &[u8]) -> Cow<'a, [u8]> { diff --git a/yazi-widgets/src/input/commands/replace.rs b/yazi-widgets/src/input/commands/replace.rs index ab7046f3..6aadfd95 100644 --- a/yazi-widgets/src/input/commands/replace.rs +++ b/yazi-widgets/src/input/commands/replace.rs @@ -1,5 +1,5 @@ use yazi_macro::render; -use yazi_shared::event::CmdCow; +use yazi_shared::{event::CmdCow, replace_cow}; use crate::input::{Input, InputMode, op::InputOp}; @@ -15,6 +15,8 @@ impl Input { } pub fn replace_str(&mut self, s: &str) { + let s = replace_cow(replace_cow(s, "\r", " "), "\n", " "); + let snap = self.snap_mut(); snap.mode = InputMode::Normal; @@ -22,8 +24,8 @@ impl Input { let mut it = snap.value[start..].char_indices(); match (it.next(), it.next()) { (None, _) => {} - (Some(_), None) => snap.value.replace_range(start..snap.len(), s), - (Some(_), Some((len, _))) => snap.value.replace_range(start..start + len, s), + (Some(_), None) => snap.value.replace_range(start..snap.len(), &s), + (Some(_), Some((len, _))) => snap.value.replace_range(start..start + len, &s), } render!(); diff --git a/yazi-widgets/src/input/commands/type.rs b/yazi-widgets/src/input/commands/type.rs index f3c9caa5..9e32323a 100644 --- a/yazi-widgets/src/input/commands/type.rs +++ b/yazi-widgets/src/input/commands/type.rs @@ -1,5 +1,6 @@ use yazi_config::keymap::Key; use yazi_macro::render; +use yazi_shared::replace_cow; use crate::input::{Input, InputMode}; @@ -19,11 +20,13 @@ impl Input { } pub fn type_str(&mut self, s: &str) { + let s = replace_cow(replace_cow(s, "\r", " "), "\n", " "); + let snap = self.snap_mut(); if snap.cursor < 1 { - snap.value.insert_str(0, s); + snap.value.insert_str(0, &s); } else { - snap.value.insert_str(snap.idx(snap.cursor).unwrap(), s); + snap.value.insert_str(snap.idx(snap.cursor).unwrap(), &s); } self.r#move(s.chars().count() as isize);