From 8554e996b8db7066bee699b9c25a5daa12b6a1f8 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 7 Sep 2025 12:59:26 +0200 Subject: [PATCH] cli/diff: Replace manual pushing with Table::extend() Signed-off-by: Matthias Beyer --- crates/radicle-cli/src/git/pretty_diff.rs | 142 ++++++++++++---------- 1 file changed, 75 insertions(+), 67 deletions(-) diff --git a/crates/radicle-cli/src/git/pretty_diff.rs b/crates/radicle-cli/src/git/pretty_diff.rs index 22187398..dd5d77bc 100644 --- a/crates/radicle-cli/src/git/pretty_diff.rs +++ b/crates/radicle-cli/src/git/pretty_diff.rs @@ -444,78 +444,86 @@ impl ToPretty for Hunk { if let Ok(header) = HunkHeader::from_bytes(self.header.as_bytes()) { vstack.push(header.pretty(hi, &(), repo)); } - for line in &self.lines { - match line { - Modification::Addition(a) => { - table.push([ - term::Label::space() - .pad(5) - .bg(theme.color("positive")) - .to_line() - .filled(theme.color("positive")), - term::label(a.line_no.to_string()) - .pad(5) - .fg(theme.color("positive.light")) - .to_line() - .filled(theme.color("positive")), - term::label(" + ") - .fg(theme.color("positive.light")) - .to_line() - .filled(theme.color("positive.dark")), - line.pretty(hi, blobs, repo) - .filled(theme.color("positive.dark")), - term::Line::blank().filled(term::Color::default()), - ]); - } - Modification::Deletion(a) => { - table.push([ - term::label(a.line_no.to_string()) - .pad(5) - .fg(theme.color("negative.light")) - .to_line() - .filled(theme.color("negative")), - term::Label::space() - .pad(5) - .fg(theme.color("dim")) - .to_line() - .filled(theme.color("negative")), - term::label(" - ") - .fg(theme.color("negative.light")) - .to_line() - .filled(theme.color("negative.dark")), - line.pretty(hi, blobs, repo) - .filled(theme.color("negative.dark")), - term::Line::blank().filled(term::Color::default()), - ]); - } - Modification::Context { - line_no_old, - line_no_new, - .. - } => { - table.push([ - term::label(line_no_old.to_string()) - .pad(5) - .fg(theme.color("dim")) - .to_line() - .filled(theme.color("faint")), - term::label(line_no_new.to_string()) - .pad(5) - .fg(theme.color("dim")) - .to_line() - .filled(theme.color("faint")), - term::label(" ").to_line().filled(term::Color::default()), - line.pretty(hi, blobs, repo).filled(term::Color::default()), - term::Line::blank().filled(term::Color::default()), - ]); - } - } - } + + table.extend( + self.lines + .iter() + .map(|line| line_to_table_row(hi, blobs, repo, &theme, line)), + ); + vstack.push(table); vstack } } +fn line_to_table_row( + hi: &mut Highlighter, + blobs: &Blobs>, + repo: &R, + theme: &Theme, + line: &Modification, +) -> [radicle_term::Filled; 5] { + match line { + Modification::Addition(a) => [ + term::Label::space() + .pad(5) + .bg(theme.color("positive")) + .to_line() + .filled(theme.color("positive")), + term::label(a.line_no.to_string()) + .pad(5) + .fg(theme.color("positive.light")) + .to_line() + .filled(theme.color("positive")), + term::label(" + ") + .fg(theme.color("positive.light")) + .to_line() + .filled(theme.color("positive.dark")), + line.pretty(hi, blobs, repo) + .filled(theme.color("positive.dark")), + term::Line::blank().filled(term::Color::default()), + ], + Modification::Deletion(a) => [ + term::label(a.line_no.to_string()) + .pad(5) + .fg(theme.color("negative.light")) + .to_line() + .filled(theme.color("negative")), + term::Label::space() + .pad(5) + .fg(theme.color("dim")) + .to_line() + .filled(theme.color("negative")), + term::label(" - ") + .fg(theme.color("negative.light")) + .to_line() + .filled(theme.color("negative.dark")), + line.pretty(hi, blobs, repo) + .filled(theme.color("negative.dark")), + term::Line::blank().filled(term::Color::default()), + ], + Modification::Context { + line_no_old, + line_no_new, + .. + } => [ + term::label(line_no_old.to_string()) + .pad(5) + .fg(theme.color("dim")) + .to_line() + .filled(theme.color("faint")), + term::label(line_no_new.to_string()) + .pad(5) + .fg(theme.color("dim")) + .to_line() + .filled(theme.color("faint")), + term::label(" ").to_line().filled(term::Color::default()), + line.pretty(hi, blobs, repo).filled(term::Color::default()), + term::Line::blank().filled(term::Color::default()), + ], + } +} + impl ToPretty for Modification { type Output = term::Line; type Context = Blobs>;