likewise/examples/terminal-inline.rs
2021-01-31 18:22:37 +01:00

30 lines
1 KiB
Rust

use console::Style;
use similar::text::{ChangeTag, TextDiff};
fn main() {
let diff = TextDiff::from_lines(
"Hello World\nThis is the second line.\nThis is the third.",
"Hallo Welt\nThis is the second line.\nThis is life.\nMoar and more",
);
for op in diff.ops() {
for change in diff.iter_inline_changes(op) {
let (sign, style) = match change.tag() {
ChangeTag::Delete => ("-", Style::new().red()),
ChangeTag::Insert => ("+", Style::new().green()),
ChangeTag::Equal => (" ", Style::new()),
};
print!("{}", style.apply_to(sign).bold(),);
for &(emphasized, value) in change.values() {
if emphasized {
print!("{}", style.apply_to(value).underlined());
} else {
print!("{}", style.apply_to(value));
}
}
if change.is_missing_newline() {
println!();
}
}
}
}