Print file name and hunk number on every hunk

pull/70/head
Wilfred Hughes 2021-10-23 16:51:37 +07:00
parent 5e9a0eec5d
commit b3106e1382
4 changed files with 29 additions and 7 deletions

@ -1,5 +1,11 @@
## 0.12 (unreleased)
### Display
Every hunk is now shown with the file name and a hunk number. This
makes it easier to see which file you're looking at when there are
many changes.
### Command Line Interface
The difftastic binary is now named `difft`, to reduce typing during

@ -209,7 +209,7 @@ fn diff_file(display_path: &str, lhs_path: &str, rhs_path: &str) {
let lhs_binary = is_probably_binary(&lhs_bytes);
let rhs_binary = is_probably_binary(&rhs_bytes);
if lhs_binary || rhs_binary {
println!("{}", style::header(display_path, "binary"));
print!("{}", style::header(display_path, 1, 1, "binary"));
return;
}
@ -239,8 +239,6 @@ fn diff_file(display_path: &str, lhs_path: &str, rhs_path: &str) {
),
};
println!("{}", style::header(display_path, lang_name));
init_info(&lhs, &rhs);
mark_syntax(lhs.get(0).copied(), rhs.get(0).copied());
@ -265,6 +263,8 @@ fn diff_file(display_path: &str, lhs_path: &str, rhs_path: &str) {
groups = join_overlapping(groups);
if env::var("INLINE").is_ok() {
print!("{}", style::header(display_path, 1, 1, lang_name));
print!(
"{}",
inline::display(&lhs_src, &rhs_src, &lhs_positions, &rhs_positions, &groups)
@ -273,6 +273,8 @@ fn diff_file(display_path: &str, lhs_path: &str, rhs_path: &str) {
print!(
"{}",
side_by_side::display(
display_path,
lang_name,
&lhs_src,
&rhs_src,
&lhs_positions,

@ -12,7 +12,7 @@ use crate::{
codepoint_len, enforce_exact_length, enforce_max_length, format_line_num, LineGroup,
LineNumber,
},
style::apply_colors,
style::{apply_colors, header},
syntax::{aligned_lines, MatchedPos},
};
@ -161,6 +161,8 @@ fn apply_group<S: AsRef<str>>(
/// Display all the lines in `lhs` and `rhs` that are mentioned in
/// `groups`, horizontally concatenating the matched lines.
fn apply_groups(
display_path: &str,
lang_name: &str,
lhs: &str,
rhs: &str,
groups: &[LineGroup],
@ -172,6 +174,8 @@ fn apply_groups(
let mut result = String::new();
for (i, group) in groups.iter().enumerate() {
result.push_str(&header(display_path, i + 1, groups.len(), lang_name));
result.push_str(&apply_group(
&split_lines_nonempty(lhs),
&split_lines_nonempty(rhs),
@ -182,7 +186,7 @@ fn apply_groups(
rhs_column_width,
));
if i != groups.len() - 1 {
result.push_str("\n\n");
result.push('\n');
}
}
@ -220,6 +224,8 @@ fn display_single_column(src: &str, color: Color) -> String {
/// Display `lhs_src` and `rhs_src` in a side-by-side view with
/// changed lines shown and highlighted.
pub fn display(
display_path: &str,
lang_name: &str,
lhs_src: &str,
rhs_src: &str,
lhs_positions: &[MatchedPos],
@ -257,6 +263,8 @@ pub fn display(
let rhs_colored = apply_colors(&rhs_src, false, rhs_positions);
apply_groups(
display_path,
lang_name,
&lhs_colored,
&rhs_colored,
groups,

@ -133,6 +133,12 @@ pub fn apply_colors(s: &str, is_lhs: bool, positions: &[MatchedPos]) -> String {
apply(s, &styles)
}
pub fn header(file_name: &str, language_name: &str) -> String {
format!("{} -- {}", file_name.yellow().bold(), language_name)
pub fn header(file_name: &str, hunk_num: usize, hunk_total: usize, language_name: &str) -> String {
format!(
"{} --- {}/{} --- {}\n",
file_name.yellow().bold(),
hunk_num,
hunk_total,
language_name
)
}