Diff a string for presentation to a user in the terminal
termdiff
Diff a string for presentation to a user in the terminal.
Usage
usetermdiff::{SignsTheme,DrawDiff};letold="The quick brown fox and\njumps over the sleepy dog";letnew="The quick red fox and\njumps over the lazy dog";lettheme=SignsTheme::default();letactual=format!("{}",DrawDiff::new(old,new,&theme));assert_eq!(actual,"--- remove | insert +++
-The quick brown fox and
-jumps over the sleepy dog
+The quick red fox and
+jumps over the lazy dog
");Alternatively you can use this interface
usetermdiff::{ArrowsTheme,diff};letold="The quick brown fox and\njumps over the sleepy dog";letnew="The quick red fox and\njumps over the lazy dog";lettheme=ArrowsTheme::default();letmutbuffer: Vec<u8>=Vec::new();diff(&mutbuffer,old,new,&theme).unwrap();letactual: String =String::from_utf8(buffer).expect("Not valid UTF-8");assert_eq!(actual,"< left / > right
<The quick brown fox and
<jumps over the sleepy dog
>The quick red fox and
>jumps over the lazy dog
");Read more at Docs.rs
Themes
We have a limited number of built in themes
Arrows
Signs
Custom
usetermdiff::DrawDiff;usetermdiff::Theme;usecrossterm::style::Stylize;usestd::borrow::Cow;#[derive(Debug)]struct MyTheme{}implThemeforMyTheme{fn highlight_insert<'this>(&self,input: &'this str)-> Cow<'this,str>{input.into()}fn highlight_delete<'this>(&self,input: &'this str)-> Cow<'this,str>{input.into()}fn equal_content<'this>(&self,input: &'this str)-> Cow<'this,str>{input.into()}fn delete_content<'this>(&self,input: &'this str)-> Cow<'this,str>{input.into()}fn equal_prefix<'this>(&self)-> Cow<'this,str>{"=".into()}fn delete_prefix<'this>(&self)-> Cow<'this,str>{"!".into()}fn insert_line<'this>(&self,input: &'this str)-> Cow<'this,str>{input.into()}fn insert_prefix<'this>(&self)-> Cow<'this,str>{"|".into()}fn line_end<'this>(&self)-> Cow<'this,str>{"\n".into()}fn header<'this>(&self)-> Cow<'this,str>{format!("{}\n","Header").into()}}letmy_theme=MyTheme{};letold="The quick brown fox and\njumps over the sleepy dog";letnew="The quick red fox and\njumps over the lazy dog";letactual=format!("{}",DrawDiff::new(old,new,&my_theme));assert_eq!(actual,"Header
!The quick brown fox and
!jumps over the sleepy dog
|The quick red fox and
|jumps over the lazy dog
");