pub struct StyleDiff<'a> { /* private fields */ }Expand description
Difference in styles between two styled strings that can be output in detailed
human-readable format via Display.
The Display implementation supports two output formats:
- With the default / non-alternate format (e.g.,
{}), the diff will be output as the LHS line-by-line, highlighting differences in styles on each differing line. - With the alternate format (
{:#}), the diff will be output as a table of all differing spans.
§Examples
use styled_str::{styled, StyleDiff, StyledString};
let lhs = styled!("[[red on white]]Hello,[[/]] [[bold green]]world!");
let rhs = styled!("[[red on white!]]Hello,[[/]] [[bold green]]world[[/]]!");
let diff = StyleDiff::new(lhs, rhs);
assert!(!diff.is_empty());
let diff_str = StyledString::from_ansi(&format!("{diff}"))?;
assert_eq!(
diff_str.text(),
"> Hello, world!\n\
> ^^^^^^ ^\n"
);
let diff_str = StyledString::from_ansi(&format!("{diff:#}"))?;
let expected =
"|Positions Left style Right style |
|========== ========================= =========================|
| 0..6 red on white red on white! |
| 12..13 bold green (none) |";
let expected: String = expected
.lines()
.flat_map(|line| [line.trim().trim_matches('|'), "\n"])
.collect();
assert_eq!(diff_str.text(), expected);Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for StyleDiff<'a>
impl<'a> RefUnwindSafe for StyleDiff<'a>
impl<'a> Send for StyleDiff<'a>
impl<'a> Sync for StyleDiff<'a>
impl<'a> Unpin for StyleDiff<'a>
impl<'a> UnwindSafe for StyleDiff<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more