StyleDiff

Struct StyleDiff 

Source
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§

Source§

impl<'a> StyleDiff<'a>

Source

pub fn new(lhs: StyledStr<'a>, rhs: StyledStr<'a>) -> Self

Computes style difference between two styled strings.

§Panics

Panics if lhs and rhs have differing lengths.

Source

pub fn is_empty(&self) -> bool

Checks whether this difference is empty.

Trait Implementations§

Source§

impl<'a> Debug for StyleDiff<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for StyleDiff<'_>

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.