From 83b8c843cb4f1b44a79c816cf5a0a26a432046e1 Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Sun, 8 Aug 2021 11:26:17 -0700 Subject: [PATCH] Use .. for column numbers on blank lines used to align --- CHANGELOG.md | 6 ++++++ src/side_by_side.rs | 38 ++++++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1553688ce..07b7244dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ ## 0.7 (unreleased) +### Display + +Side-by-side display now uses "..." for column numbers when aligning +lines. This makes hunks more obvious, but hunks now also have two +blank lines between them to make it clearer. + ### Parsing Fixed handling of `->` in Rust. diff --git a/src/side_by_side.rs b/src/side_by_side.rs index 6f72ff769..e4593a6e3 100644 --- a/src/side_by_side.rs +++ b/src/side_by_side.rs @@ -76,6 +76,15 @@ fn format_line_num_padded(line_num: LineNumber, column_width: usize) -> String { format!("{:width$} ", line_num.0 + 1, width = column_width - 1) } +fn format_missing_line_num(prev_num: LineNumber, column_width: usize) -> String { + let num_digits = format!("{}", prev_num.0).len(); + format!( + "{:>width$} ", + ".".repeat(num_digits), + width = column_width - 1 + ) +} + fn apply_group( lhs_lines: &[&str], rhs_lines: &[&str], @@ -86,6 +95,8 @@ fn apply_group( rhs_column_width: usize, ) -> String { let mut result = String::new(); + let mut lhs_prev_line_num = LineNumber(0); + let mut rhs_prev_line_num = LineNumber(0); for (lhs_line_num, rhs_line_num) in aligned_lines(&group.lhs_lines(), &group.rhs_lines(), lhs_line_matches) @@ -94,9 +105,14 @@ fn apply_group( Some(lhs_line_num) => { result.push_str(&format_line_num_padded(lhs_line_num, lhs_column_width)); result.push_str(lhs_lines[lhs_line_num.0]); + + lhs_prev_line_num = lhs_line_num; } None => { - result.push_str(&" ".repeat(lhs_column_width)); + result.push_str(&format_missing_line_num( + lhs_prev_line_num, + lhs_column_width, + )); result.push_str(&" ".repeat(lhs_content_width)); } } @@ -106,8 +122,15 @@ fn apply_group( Some(rhs_line_num) => { result.push_str(&format_line_num_padded(rhs_line_num, rhs_column_width)); result.push_str(rhs_lines[rhs_line_num.0]); + + rhs_prev_line_num = rhs_line_num; + } + None => { + result.push_str(&format_missing_line_num( + rhs_prev_line_num, + rhs_column_width, + )); } - None => {} } result.push('\n'); @@ -124,7 +147,6 @@ fn apply_groups( groups: &[LineGroup], lhs_line_matches: &HashMap, lhs_content_width: usize, - rhs_content_width: usize, lhs_column_width: usize, rhs_column_width: usize, ) -> String { @@ -133,12 +155,6 @@ fn apply_groups( let mut result = String::new(); - let mut spacer = String::new(); - spacer.push_str(&" ".repeat(lhs_column_width)); - spacer.push_str(&"-".repeat(lhs_content_width)); - spacer.push_str(&" ".repeat(rhs_column_width + 2)); - spacer.push_str(&"-".repeat(rhs_content_width)); - for (i, group) in groups.iter().enumerate() { result.push_str(&apply_group( &lhs_lines, @@ -150,8 +166,7 @@ fn apply_groups( rhs_column_width, )); if i != groups.len() - 1 { - result.push_str(&spacer); - result.push('\n'); + result.push_str("\n\n"); } } @@ -195,7 +210,6 @@ pub fn display( groups, lhs_matched_lines, lhs_content_width, - rhs_content_width, lhs_column_width, rhs_column_width, )