tui: copy selection joins soft-wrapped rows without newline
This commit is contained in:
1 parent
6ebf1c3180
commit
25936457c9
2 files changed
+50
-16
No files matched your search
+35
-10
@@ -55,13 +55,15 @@ pub fn paint(buf: &mut Buffer, area: Rect, top: u16, s: &Sel) {
|
||||
}
|
||||
|
||||
/// Selected text from a buffer rendered at origin (0, 0); trailing blanks per row are dropped.
|
||||
pub fn text(buf: &Buffer, s: &Sel) -> String {
|
||||
/// `cont[row]` = row is a soft-wrap continuation: joined to the previous row, not newline-split.
|
||||
pub fn text(buf: &Buffer, s: &Sel, cont: &[bool]) -> String {
|
||||
let (w, h) = (buf.area.width, buf.area.height);
|
||||
if w == 0 || h == 0 {
|
||||
return String::new();
|
||||
}
|
||||
let (a, b) = s.span();
|
||||
let mut rows = vec![];
|
||||
let mut out = String::new();
|
||||
let mut prev_full = false;
|
||||
for row in a.0..=b.0.min(h - 1) {
|
||||
let Some((lo, hi)) = s.cols(row, w) else {
|
||||
continue;
|
||||
@@ -72,9 +74,22 @@ pub fn text(buf: &Buffer, s: &Sel) -> String {
|
||||
line.push_str(sym);
|
||||
x += Span::raw(sym).width().max(1) as u16; // skip the trailing cell of wide chars
|
||||
}
|
||||
rows.push(line.trim_end().to_string());
|
||||
let line = line.trim_end();
|
||||
let full = Span::raw(line).width() as u16 >= w; // word broken mid-token
|
||||
if row > a.0 {
|
||||
if cont.get(row as usize) == Some(&true) {
|
||||
// ponytail: wrap at a space leaves no trace; assume one unless the row was full
|
||||
if !prev_full {
|
||||
out.push(' ');
|
||||
}
|
||||
} else {
|
||||
out.push('\n');
|
||||
}
|
||||
}
|
||||
out.push_str(line);
|
||||
prev_full = full;
|
||||
}
|
||||
rows.join("\n")
|
||||
out
|
||||
}
|
||||
|
||||
fn is_word(sym: &str) -> bool {
|
||||
@@ -151,28 +166,38 @@ mod tests {
|
||||
anchor: (0, 6),
|
||||
head: (1, 5),
|
||||
};
|
||||
assert_eq!(text(&b, &fwd), "world\nsecond");
|
||||
assert_eq!(text(&b, &fwd, &[]), "world\nsecond");
|
||||
let rev = Sel {
|
||||
anchor: (1, 5),
|
||||
head: (0, 6),
|
||||
};
|
||||
assert_eq!(text(&b, &rev), "world\nsecond");
|
||||
assert_eq!(text(&b, &rev, &[]), "world\nsecond");
|
||||
let wide = Sel {
|
||||
anchor: (1, 7),
|
||||
head: (1, 10),
|
||||
};
|
||||
assert_eq!(text(&b, &wide), "日本");
|
||||
assert_eq!(text(&b, &wide, &[]), "日本");
|
||||
let past = Sel {
|
||||
anchor: (2, 0),
|
||||
head: (9, 3),
|
||||
};
|
||||
assert_eq!(text(&b, &past), "third");
|
||||
assert_eq!(text(&b, &past, &[]), "third");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn soft_wrap_rows_join_without_newline() {
|
||||
let b = buf("hello\nworld\nnext", 10);
|
||||
let s = Sel {
|
||||
anchor: (0, 0),
|
||||
head: (2, 9),
|
||||
};
|
||||
assert_eq!(text(&b, &s, &[false, true, false]), "hello world\nnext");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn word_and_line_pick() {
|
||||
let b = buf("see src/main.rs now", 30);
|
||||
assert_eq!(text(&b, &word(&b, 0, 6)), "src/main.rs");
|
||||
assert_eq!(text(&b, &word(&b, 0, 6), &[]), "src/main.rs");
|
||||
assert_eq!(
|
||||
word(&b, 0, 3),
|
||||
Sel {
|
||||
@@ -180,7 +205,7 @@ mod tests {
|
||||
head: (0, 3)
|
||||
}
|
||||
);
|
||||
assert_eq!(text(&b, &line(&b, 0)), "see src/main.rs now");
|
||||
assert_eq!(text(&b, &line(&b, 0), &[]), "see src/main.rs now");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user