From 95f1343ec1bcff245704c8b1d89ea44a4935afe4 Mon Sep 17 00:00:00 2001 From: hermes Date: Wed, 14 Oct 2026 20:08:12 +0000 Subject: [PATCH] tui: /diff wrap (w) and horizontal pan (h/l) --- crates/hx-tui/src/review.rs | 149 ++++++++++++++++++++++++++---------- 1 file changed, 108 insertions(+), 41 deletions(-) diff --git a/crates/hx-tui/src/review.rs b/crates/hx-tui/src/review.rs index c9765ea..aaebb8a 100644 --- a/crates/hx-tui/src/review.rs +++ b/crates/hx-tui/src/review.rs @@ -86,6 +86,9 @@ pub struct Review { nums: bool, words: bool, collapsed: bool, + wrap: bool, + /// Horizontal scroll in chars (only when not wrapping). + xoff: usize, top: Cell, } @@ -177,6 +180,8 @@ impl Review { nums: true, words: true, collapsed: false, + wrap: false, + xoff: 0, top: Cell::new(0), }) } @@ -308,6 +313,16 @@ impl Review { KeyCode::Char('G') | KeyCode::End => self.step(isize::MAX / 2), KeyCode::Char(']') => self.hunk(1), KeyCode::Char('[') => self.hunk(-1), + KeyCode::Char('w') => { + self.wrap = !self.wrap; + self.xoff = 0; + } + KeyCode::Char('h') | KeyCode::Left => self.xoff = self.xoff.saturating_sub(8), + KeyCode::Char('l') | KeyCode::Right if !self.wrap => { + let f = &self.files[self.sel]; + let len = f.rows[f.cur].text().chars().count(); + self.xoff = (self.xoff + 8).min(len.saturating_sub(1)); + } KeyCode::Char('L') => self.nums = !self.nums, KeyCode::Char('W') => self.words = !self.words, KeyCode::Char('v') => { @@ -351,7 +366,8 @@ impl Review { Act::None } - fn row_line(&self, r: &Row, width: usize, cur: bool) -> Line<'static> { + /// One screen line per row (cut with `«`/`»`), or several with `↪` when wrapping. + fn row_lines(&self, r: &Row, width: usize, cur: bool) -> Vec> { let th = theme::t(); let (base, em, sign) = match r.kind { Kind::Add => (th.add(), th.add_em(), '+'), @@ -359,49 +375,84 @@ impl Review { Kind::Hunk => (th.hunk(), th.hunk(), ' '), Kind::Ctx => (th.plain(), th.plain(), ' '), }; - let mut spans = vec![]; - let mut used = 0; + let mut gutter = String::new(); if self.nums && r.kind != Kind::Hunk { let n = |v: Option| v.map_or(" ".into(), |v| format!("{v:>4}")); - let g = format!("{} {} ", n(r.old), n(r.new)); - used += g.chars().count(); - spans.push(Span::styled(g, th.dim())); + gutter = format!("{} {} ", n(r.old), n(r.new)); } - if r.kind != Kind::Hunk { - spans.push(Span::styled(format!("{sign} "), base)); - used += 2; - } - let budget = width.saturating_sub(used); - let mut left = budget; - let mut cut = false; - for (e, t) in &r.segs { - let t = t.replace('\t', " "); - let c = t.chars().count(); - let take = if c > left { - cut = true; - left - } else { - c - }; - let part: String = t.chars().take(take).collect(); - left -= take; - spans.push(Span::styled(part, if *e && self.words { em } else { base })); - if cut { - break; - } - } - if cut && let Some(l) = spans.last_mut() { - let mut s: String = l.content.chars().collect(); - s.pop(); - s.push('»'); - l.content = s.into(); - } - let line = Line::from(spans); - if cur { - line.patch_style(th.sel()) + let sign = if r.kind == Kind::Hunk { + String::new() } else { - line + format!("{sign} ") + }; + let used = gutter.chars().count() + sign.chars().count(); + let budget = width.saturating_sub(used).max(1); + let cells: Vec<(char, bool)> = r + .segs + .iter() + .flat_map(|(e, t)| { + t.replace('\t', " ") + .chars() + .map(|c| (c, *e)) + .collect::>() + }) + .collect(); + let chunks: Vec<&[(char, bool)]> = if self.wrap { + if cells.is_empty() { + vec![&[]] + } else { + cells.chunks(budget - usize::from(budget > 1)).collect() + } + } else { + vec![&cells[self.xoff.min(cells.len())..]] + }; + let mut out = vec![]; + for (i, ch) in chunks.into_iter().enumerate() { + let mut spans = vec![]; + if i == 0 { + spans.push(Span::styled(gutter.clone(), th.dim())); + spans.push(Span::styled(sign.clone(), base)); + } else { + spans.push(Span::styled(format!("{:>w$}", "↪ ", w = used), th.dim())); + } + let (mut shown, cut) = if !self.wrap && ch.len() > budget { + (&ch[..budget], true) + } else { + (ch, false) + }; + let lead = !self.wrap && self.xoff > 0 && !cells.is_empty(); + let mut buf: Vec<(char, bool)> = shown.to_vec(); + if lead && let Some(f) = buf.first_mut() { + f.0 = '«'; + } + if cut && let Some(l) = buf.last_mut() { + l.0 = '»'; + } + shown = &buf; + let mut run = String::new(); + let mut run_em = shown.first().is_some_and(|c| c.1); + for &(c, e) in shown { + if e != run_em { + spans.push(Span::styled( + std::mem::take(&mut run), + if run_em && self.words { em } else { base }, + )); + run_em = e; + } + run.push(c); + } + spans.push(Span::styled( + run, + if run_em && self.words { em } else { base }, + )); + let line = Line::from(spans); + out.push(if cur { + line.patch_style(th.sel()) + } else { + line + }); } + out } pub fn draw(&self, f: &mut ratatui::Frame) { @@ -416,7 +467,7 @@ impl Review { let hint = if self.input.is_some() { " enter save note (empty deletes) · esc cancel ".to_string() } else { - " c note · s send notes · a accept · r revert · n/p file · j/k ]/[ move · L nums · W words · v collapse · esc close " + " c note · s send notes · a accept · r revert · n/p file · j/k ]/[ move · L nums · W words · w wrap · h/l pan · v collapse · esc close " .to_string() }; let block = Block::bordered() @@ -461,7 +512,7 @@ impl Review { if i == cur.cur { cur_line = lines.len(); } - lines.push(self.row_line(row, width, i == cur.cur)); + lines.extend(self.row_lines(row, width, i == cur.cur)); if let Some(n) = self.note_at(self.sel, row) { lines.push(Line::styled( format!(" ✎ {}", self.notes[n].text), @@ -583,6 +634,22 @@ mod tests { Act::Send(m) => assert!(m.contains("/x/a.rs:2 (added) `let x = 2;`: use 3"), "{m}"), _ => panic!("expected Send"), } + // long line: cut with », panned with «, wrapped with ↪ + let long = "x".repeat(100); + let mut r2 = Review::new(&[FileEdit { + path: "/y".into(), + old: None, + new: format!("{long}\n"), + }]) + .unwrap(); + let row = &r2.files[0].rows[1]; + let txt = |ls: Vec| ls.iter().map(|l| l.to_string()).collect::>(); + assert!(txt(r2.row_lines(row, 40, false))[0].ends_with('»')); + r2.key(key('l')); + assert!(txt(r2.row_lines(&r2.files[0].rows[1], 40, false))[0].contains('«')); + r2.key(key('w')); + let w = txt(r2.row_lines(&r2.files[0].rows[1], 40, false)); + assert!(w.len() > 2 && w[1].contains('↪'), "{w:?}"); r.key(key('v')); assert!(!r.shown(&r.files[0].rows[2])); }