cargo_test_support/
diff.rsuse std::fmt;
use std::io::Write;
#[derive(Debug, Eq, PartialEq)]
pub enum Change<T> {
Add(usize, T),
Remove(usize, T),
Keep(usize, usize, T),
}
pub fn render_colored_changes<T: fmt::Display>(changes: &[Change<T>]) -> String {
let red = anstyle::AnsiColor::Red.on_default().render();
let green = anstyle::AnsiColor::Green.on_default().render();
let dim = (anstyle::Style::new() | anstyle::Effects::DIMMED).render();
let bold = (anstyle::Style::new() | anstyle::Effects::BOLD).render();
let reset = anstyle::Reset.render();
let choice = if crate::is_ci() {
anstream::ColorChoice::Never
} else {
anstream::AutoStream::choice(&std::io::stdout())
};
let mut buffer = anstream::AutoStream::new(Vec::new(), choice);
for change in changes {
let (nums, sign, color, text) = match change {
Change::Add(i, s) => (format!(" {:<4} ", i), '+', green, s),
Change::Remove(i, s) => (format!("{:<4} ", i), '-', red, s),
Change::Keep(x, y, s) => (format!("{:<4}{:<4} ", x, y), ' ', dim, s),
};
writeln!(
buffer,
"{dim}{nums}{reset}{bold}{sign}{reset}{color}{text}{reset}"
)
.unwrap();
}
String::from_utf8(buffer.into_inner()).unwrap()
}