nushell/crates/nu-plugin-test-support/src/diff.rs
Devyn Cairns efe1c99a3b
Fix #12280: replace difference crate with similar (#12282)
Fixes #12280.

# Description

This removes the dependency on the `difference` crate, which is
unmaintained, for `nu-plugin-test-support`. The `similar` crate
(Apache-2.0) is used instead, which is a bit larger and more complex,
but still suitable for a dev dep for tests. Also switched to use
`crossterm` for colors, since `similar` doesn't come with any terminal
pretty printing functionality.

# User-Facing Changes

None - output should be identical.

# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`
2024-03-25 21:13:12 -05:00

27 lines
733 B
Rust

use std::fmt::Write;
use nu_ansi_term::{Color, Style};
use similar::{ChangeTag, TextDiff};
/// Generate a stylized diff of different lines between two strings
pub(crate) fn diff_by_line(old: &str, new: &str) -> String {
let mut out = String::new();
let diff = TextDiff::from_lines(old, new);
for change in diff.iter_all_changes() {
let style = match change.tag() {
ChangeTag::Equal => Style::new(),
ChangeTag::Delete => Color::Red.into(),
ChangeTag::Insert => Color::Green.into(),
};
let _ = write!(
out,
"{}{}",
style.paint(change.tag().to_string()),
style.paint(change.value()),
);
}
out
}