tui: copy joins soft-wrapped rows without newline #184

Merged
hermes merged 1 commits from fix/copy-softwrap-pr into main 2026-11-14 10:15:41 +00:00
2 changed files with 50 additions and 16 deletions
Showing only changes of commit 25936457c9 - Show all commits

No files matched your search

+15 -6
View File
@@ -902,13 +902,21 @@ fn on_mouse(app: &mut App, m: crossterm::event::MouseEvent) {
}
/// The chat text as drawn, full height (for extracting a selection).
fn chat_buf(app: &App) -> Buffer {
/// Off-screen chat render plus, per row, whether it continues a soft-wrapped line.
fn chat_buf(app: &App) -> (Buffer, Vec<bool>) {
let w = app.areas.chat.width;
let para = Paragraph::new(app.t.lines(app.expanded, w)).wrap(Wrap { trim: false });
let lines = app.t.lines(app.expanded, w);
let mut cont = vec![];
for l in &lines {
let n = Paragraph::new(l.clone()).wrap(Wrap { trim: false }).line_count(w).max(1);
cont.push(false);
cont.extend(std::iter::repeat(true).take(n - 1));
}
let para = Paragraph::new(lines).wrap(Wrap { trim: false });
let r = Rect::new(0, 0, w, para.line_count(w) as u16);
let mut b = Buffer::empty(r);
Widget::render(para, r, &mut b);
b
(b, cont)
}
/// Put `t` on the clipboard (OSC 52) and say so in the footer.
@@ -973,13 +981,13 @@ fn select_mouse(app: &mut App, m: crossterm::event::MouseEvent) {
app.sel = Some(select::Sel { anchor: p, head: p });
app.drag = true;
} else {
let b = chat_buf(app);
let (b, cont) = chat_buf(app);
let s = if n == 2 {
select::word(&b, p.0, p.1)
} else {
select::line(&b, p.0)
};
let t = select::text(&b, &s);
let t = select::text(&b, &s, &cont);
app.sel = Some(s);
copy(app, &t);
}
@@ -993,7 +1001,8 @@ fn select_mouse(app: &mut App, m: crossterm::event::MouseEvent) {
app.drag = false;
match app.sel {
Some(s) if s.anchor != p => {
let t = select::text(&chat_buf(app), &s);
let (b, cont) = chat_buf(app);
let t = select::text(&b, &s, &cont);
copy(app, &t);
}
_ => app.sel = None,
+35 -10
View File
@@ -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]