feat: display newlines in input as spaces to improve readability (#2932)

This commit is contained in:
三咲雅 misaki masa 2025-06-28 23:59:29 +08:00 committed by GitHub
parent a8a1b5625b
commit 8657e6b6f5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 39 additions and 24 deletions

View file

@ -156,7 +156,7 @@ impl Sendable {
for (k, v) in args { for (k, v) in args {
match k { match k {
DataKey::Integer(i) => tbl.raw_set(i + 1, Self::data_to_value(lua, v)?), 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()), _ => Err("invalid key in Data".into_lua_err()),
}?; }?;
} }
@ -170,7 +170,7 @@ impl Sendable {
match k { match k {
DataKey::Integer(i) => tbl.raw_set(i + 1, Self::data_to_value_ref(lua, v)?), DataKey::Integer(i) => tbl.raw_set(i + 1, Self::data_to_value_ref(lua, v)?),
DataKey::String(s) => { 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()), _ => Err("invalid key in Data".into_lua_err()),
}?; }?;

View file

@ -169,10 +169,7 @@ impl Partitions {
for (a, b) in for (a, b) in
[(r"\011", "\t"), (r"\012", "\n"), (r"\040", " "), (r"\043", "#"), (r"\134", r"\")] [(r"\011", "\t"), (r"\012", "\n"), (r"\040", " "), (r"\043", "#"), (r"\134", r"\")]
{ {
s = match replace_cow(&s, a, b) { s = replace_cow(s, a, b);
Cow::Borrowed(_) => s,
Cow::Owned(new) => Cow::Owned(new),
};
} }
s s
} }

View file

@ -33,35 +33,48 @@ pub fn strip_trailing_newline(mut s: String) -> String {
s s
} }
pub fn replace_cow<'a>(s: &'a str, from: &str, to: &str) -> Cow<'a, str> { pub fn replace_cow<'a, T>(s: T, from: &str, to: &str) -> Cow<'a, str>
replace_cow_impl(s, s.match_indices(from), to) where
T: Into<Cow<'a, str>>,
{
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> { pub fn replacen_cow<'a, T>(s: T, from: &str, to: &str, n: usize) -> Cow<'a, str>
replace_cow_impl(s, s.match_indices(from).take(n), to) where
T: Into<Cow<'a, str>>,
{
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>( fn replace_cow_impl<'a, T>(src: &'a str, mut indices: T, to: &str) -> Cow<'a, str>
src: &'s str, where
mut indices: impl Iterator<Item = (usize, &'s str)>, T: Iterator<Item = (usize, &'a str)>,
to: &str, {
) -> Cow<'s, str> {
let Some((first_idx, first_sub)) = indices.next() else { let Some((first_idx, first_sub)) = indices.next() else {
return Cow::Borrowed(src); 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 += unsafe { src.get_unchecked(..first_idx) };
result.to_mut().push_str(to); result += to;
let mut last = first_idx + first_sub.len(); let mut last = first_idx + first_sub.len();
for (idx, sub) in indices { for (idx, sub) in indices {
result += unsafe { src.get_unchecked(last..idx) }; result += unsafe { src.get_unchecked(last..idx) };
result.to_mut().push_str(to); result += to;
last = idx + sub.len(); 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]> { pub fn replace_vec_cow<'a>(v: &'a [u8], from: &[u8], to: &[u8]) -> Cow<'a, [u8]> {

View file

@ -1,5 +1,5 @@
use yazi_macro::render; use yazi_macro::render;
use yazi_shared::event::CmdCow; use yazi_shared::{event::CmdCow, replace_cow};
use crate::input::{Input, InputMode, op::InputOp}; use crate::input::{Input, InputMode, op::InputOp};
@ -15,6 +15,8 @@ impl Input {
} }
pub fn replace_str(&mut self, s: &str) { pub fn replace_str(&mut self, s: &str) {
let s = replace_cow(replace_cow(s, "\r", " "), "\n", " ");
let snap = self.snap_mut(); let snap = self.snap_mut();
snap.mode = InputMode::Normal; snap.mode = InputMode::Normal;
@ -22,8 +24,8 @@ impl Input {
let mut it = snap.value[start..].char_indices(); let mut it = snap.value[start..].char_indices();
match (it.next(), it.next()) { match (it.next(), it.next()) {
(None, _) => {} (None, _) => {}
(Some(_), None) => snap.value.replace_range(start..snap.len(), s), (Some(_), None) => snap.value.replace_range(start..snap.len(), &s),
(Some(_), Some((len, _))) => snap.value.replace_range(start..start + len, s), (Some(_), Some((len, _))) => snap.value.replace_range(start..start + len, &s),
} }
render!(); render!();

View file

@ -1,5 +1,6 @@
use yazi_config::keymap::Key; use yazi_config::keymap::Key;
use yazi_macro::render; use yazi_macro::render;
use yazi_shared::replace_cow;
use crate::input::{Input, InputMode}; use crate::input::{Input, InputMode};
@ -19,11 +20,13 @@ impl Input {
} }
pub fn type_str(&mut self, s: &str) { pub fn type_str(&mut self, s: &str) {
let s = replace_cow(replace_cow(s, "\r", " "), "\n", " ");
let snap = self.snap_mut(); let snap = self.snap_mut();
if snap.cursor < 1 { if snap.cursor < 1 {
snap.value.insert_str(0, s); snap.value.insert_str(0, &s);
} else { } 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); self.r#move(s.chars().count() as isize);