Highlight line numbers for lines with changes

pull/70/head
Wilfred Hughes 2021-10-28 00:21:45 +07:00
parent c23a36411d
commit 0f8a2d3672
3 changed files with 44 additions and 3 deletions

@ -10,6 +10,9 @@ Keywords in added/removed regions are now shown in bold, to give
modified regions basic syntax highlighting. Previously, all modified regions basic syntax highlighting. Previously, all
added/removed regions were bold. added/removed regions were bold.
Lines with changes are now shown in a different colour in side-by-side
display.
### Command Line Interface ### Command Line Interface
The difftastic binary is now named `difft`, to reduce typing during The difftastic binary is now named `difft`, to reduce typing during

@ -4,7 +4,7 @@ use atty::Stream;
use colored::{Color, Colorize}; use colored::{Color, Colorize};
use std::{ use std::{
cmp::{max, min}, cmp::{max, min},
collections::HashMap, collections::{HashMap, HashSet},
}; };
use crate::{ use crate::{
@ -92,6 +92,8 @@ fn apply_group<S: AsRef<str>>(
lhs_content_width: usize, lhs_content_width: usize,
lhs_column_width: usize, lhs_column_width: usize,
rhs_column_width: usize, rhs_column_width: usize,
lhs_lines_with_novel: &HashSet<LineNumber>,
rhs_lines_with_novel: &HashSet<LineNumber>,
) -> String { ) -> String {
let mut result = String::new(); let mut result = String::new();
@ -121,7 +123,15 @@ fn apply_group<S: AsRef<str>>(
if !lhs_empty { if !lhs_empty {
match lhs_line_num { match lhs_line_num {
Some(lhs_line_num) => { Some(lhs_line_num) => {
result.push_str(&format_line_num_padded(lhs_line_num, lhs_column_width)); if lhs_lines_with_novel.contains(&lhs_line_num) {
result.push_str(
&format_line_num_padded(lhs_line_num, lhs_column_width)
.bright_red()
.to_string(),
);
} else {
result.push_str(&format_line_num_padded(lhs_line_num, lhs_column_width));
}
result.push_str(lhs_lines[lhs_line_num.0].as_ref()); result.push_str(lhs_lines[lhs_line_num.0].as_ref());
lhs_prev_line_num = lhs_line_num; lhs_prev_line_num = lhs_line_num;
@ -139,7 +149,15 @@ fn apply_group<S: AsRef<str>>(
match rhs_line_num { match rhs_line_num {
Some(rhs_line_num) => { Some(rhs_line_num) => {
result.push_str(&format_line_num_padded(rhs_line_num, rhs_column_width)); if rhs_lines_with_novel.contains(&rhs_line_num) {
result.push_str(
&format_line_num_padded(rhs_line_num, rhs_column_width)
.bright_green()
.to_string(),
);
} else {
result.push_str(&format_line_num_padded(rhs_line_num, lhs_column_width));
}
result.push_str(rhs_lines[rhs_line_num.0].as_ref()); result.push_str(rhs_lines[rhs_line_num.0].as_ref());
rhs_prev_line_num = rhs_line_num; rhs_prev_line_num = rhs_line_num;
@ -170,6 +188,8 @@ fn apply_groups(
lhs_content_width: usize, lhs_content_width: usize,
lhs_column_width: usize, lhs_column_width: usize,
rhs_column_width: usize, rhs_column_width: usize,
lhs_lines_with_novel: &HashSet<LineNumber>,
rhs_lines_with_novel: &HashSet<LineNumber>,
) -> String { ) -> String {
let mut result = String::new(); let mut result = String::new();
@ -184,6 +204,8 @@ fn apply_groups(
lhs_content_width, lhs_content_width,
lhs_column_width, lhs_column_width,
rhs_column_width, rhs_column_width,
lhs_lines_with_novel,
rhs_lines_with_novel,
)); ));
if i != groups.len() - 1 { if i != groups.len() - 1 {
result.push('\n'); result.push('\n');
@ -262,6 +284,17 @@ pub fn display(
let lhs_colored = apply_colors(&lhs_src, true, lhs_positions); let lhs_colored = apply_colors(&lhs_src, true, lhs_positions);
let rhs_colored = apply_colors(&rhs_src, false, rhs_positions); let rhs_colored = apply_colors(&rhs_src, false, rhs_positions);
let lhs_lines_with_novel: HashSet<LineNumber> = lhs_positions
.iter()
.filter(|mp| mp.kind.is_novel())
.map(|mp| mp.pos.line)
.collect();
let rhs_lines_with_novel: HashSet<LineNumber> = rhs_positions
.iter()
.filter(|mp| mp.kind.is_novel())
.map(|mp| mp.pos.line)
.collect();
apply_groups( apply_groups(
display_path, display_path,
lang_name, lang_name,
@ -272,5 +305,7 @@ pub fn display(
lhs_content_width, lhs_content_width,
lhs_column_width, lhs_column_width,
rhs_column_width, rhs_column_width,
&lhs_lines_with_novel,
&rhs_lines_with_novel,
) )
} }

@ -514,6 +514,9 @@ impl MatchKind {
pub fn is_unchanged(&self) -> bool { pub fn is_unchanged(&self) -> bool {
matches!(self, MatchKind::Unchanged { .. }) matches!(self, MatchKind::Unchanged { .. })
} }
pub fn is_novel(&self) -> bool {
matches!(self, MatchKind::Novel { .. })
}
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]