mirror of
https://github.com/nushell/nushell
synced 2025-01-15 06:34:15 +00:00
efe1c99a3b
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`
27 lines
733 B
Rust
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
|
|
}
|