in src/main.rs [457:503]
fn diffs_to_print<'a>(&self, orig: &'a str, edit: &'a str) -> Vec<DiffResult<&'a str>> {
let mut diffs = diff::lines(orig, edit);
fn is_same(x: &DiffResult<&str>) -> bool {
match x {
DiffResult::Both(..) => true,
_ => false,
}
}
let lines_to_print = match terminal::size() {
Some((_w, h)) => h,
None => 25,
} - 20;
let num_prefix_lines = diffs.iter().take_while(|diff| is_same(diff)).count();
let num_suffix_lines = diffs.iter().rev().take_while(|diff| is_same(diff)).count();
// If the prefix is the length of the diff then the file matched <regex>
// but applying <subst> didn't result in any changes, there are no diffs
// to print so we return an empty Vec.
if diffs.len() == num_prefix_lines {
return vec![];
}
let size_of_diff = diffs.len() - num_prefix_lines - num_suffix_lines;
let size_of_context = lines_to_print.saturating_sub(size_of_diff);
let size_of_up_context = size_of_context / 2;
let size_of_down_context = size_of_context / 2 + size_of_context % 2;
let start_offset = num_prefix_lines.saturating_sub(size_of_up_context);
let end_offset = min(
diffs.len(),
num_prefix_lines + size_of_diff + size_of_down_context,
);
diffs.truncate(end_offset);
diffs.splice(..start_offset, iter::empty());
assert!(
diffs.len() <= max(lines_to_print, size_of_diff),
"changeset too long: {} > max({}, {})",
diffs.len(),
lines_to_print,
size_of_diff
);
diffs
}