in crates/q_chat/src/tools/fs_write.rs [363:467]
fn print_diff(
updates: &mut impl Write,
old_str: &StylizedFile,
new_str: &StylizedFile,
start_line: usize,
) -> Result<()> {
let diff = similar::TextDiff::from_lines(&old_str.content, &new_str.content);
// First, get the gutter width required for both the old and new lines.
let (mut max_old_i, mut max_new_i) = (1, 1);
for change in diff.iter_all_changes() {
if let Some(i) = change.old_index() {
max_old_i = i + start_line;
}
if let Some(i) = change.new_index() {
max_new_i = i + start_line;
}
}
let old_line_num_width = terminal_width_required_for_line_count(max_old_i);
let new_line_num_width = terminal_width_required_for_line_count(max_new_i);
// Now, print
fn fmt_index(i: Option<usize>, start_line: usize) -> String {
match i {
Some(i) => (i + start_line).to_string(),
_ => " ".to_string(),
}
}
for change in diff.iter_all_changes() {
// Define the colors per line.
let (text_color, gutter_bg_color, line_bg_color) = match (change.tag(), new_str.truecolor) {
(similar::ChangeTag::Equal, true) => (style::Color::Reset, new_str.gutter_bg, new_str.line_bg),
(similar::ChangeTag::Delete, true) => (
style::Color::Reset,
style::Color::Rgb { r: 79, g: 40, b: 40 },
style::Color::Rgb { r: 36, g: 25, b: 28 },
),
(similar::ChangeTag::Insert, true) => (
style::Color::Reset,
style::Color::Rgb { r: 40, g: 67, b: 43 },
style::Color::Rgb { r: 24, g: 38, b: 30 },
),
(similar::ChangeTag::Equal, false) => (style::Color::Reset, new_str.gutter_bg, new_str.line_bg),
(similar::ChangeTag::Delete, false) => (style::Color::Red, new_str.gutter_bg, new_str.line_bg),
(similar::ChangeTag::Insert, false) => (style::Color::Green, new_str.gutter_bg, new_str.line_bg),
};
// Define the change tag character to print, if any.
let sign = match change.tag() {
similar::ChangeTag::Equal => " ",
similar::ChangeTag::Delete => "-",
similar::ChangeTag::Insert => "+",
};
let old_i_str = fmt_index(change.old_index(), start_line);
let new_i_str = fmt_index(change.new_index(), start_line);
// Print the gutter and line numbers.
queue!(updates, style::SetBackgroundColor(gutter_bg_color))?;
queue!(
updates,
style::SetForegroundColor(text_color),
style::Print(sign),
style::Print(" ")
)?;
queue!(
updates,
style::Print(format!(
"{:>old_line_num_width$}",
old_i_str,
old_line_num_width = old_line_num_width
))
)?;
if sign == " " {
queue!(updates, style::Print(", "))?;
} else {
queue!(updates, style::Print(" "))?;
}
queue!(
updates,
style::Print(format!(
"{:>new_line_num_width$}",
new_i_str,
new_line_num_width = new_line_num_width
))
)?;
// Print the line.
queue!(
updates,
style::SetForegroundColor(style::Color::Reset),
style::Print(":"),
style::SetForegroundColor(text_color),
style::SetBackgroundColor(line_bg_color),
style::Print(" "),
style::Print(change),
style::ResetColor,
)?;
}
queue!(
updates,
crossterm::terminal::Clear(crossterm::terminal::ClearType::UntilNewLine),
style::Print("\n"),
)?;
Ok(())
}