Crate termdiff[][src]

Expand description

This library is for helping you create diff for displaying on the terminal

Examples

use termdiff::{arrows_theme, diff};
let old = "The quick brown fox and\njumps over the sleepy dog";
let new = "The quick red fox and\njumps over the lazy dog";
let mut buffer: Vec<u8> = Vec::new();
diff(&mut buffer, old, new, arrows_theme()).unwrap();
let actual: 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
"
);

Alternatively if you are dropping this into a format! or similar, you might want to use the displayable instead

use termdiff::{signs_theme, DrawDiff};
let old = "The quick brown fox and\njumps over the sleepy dog";
let new = "The quick red fox and\njumps over the lazy dog";
let actual = format!("{}", DrawDiff::new(old, new, signs_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
"
);

You can define your own theme if you like

use termdiff::DrawDiff;
use termdiff::Theme;
use crossterm::style::Stylize;

let my_theme = Theme {
header: format!("{}\n", "Header"),
highlight_insert: crossterm::style::Stylize::stylize,
highlight_delete: crossterm::style::Stylize::stylize,
equal_prefix: "=".to_string(),
equal_content: crossterm::style::Stylize::stylize,
delete_prefix: "!".to_string(),
delete_content: crossterm::style::Stylize::stylize,
insert_prefix: "|".to_string(),
insert_line: crossterm::style::Stylize::stylize,
line_end: "\n".into(),
};

let old = "The quick brown fox and\njumps over the sleepy dog";
let new = "The quick red fox and\njumps over the lazy dog";
let actual = 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
"
);

Structs

The struct that draws the diff

A Theme for the diff

Functions

A simple colorful theme using arrows

A simple colorless using arrows theme

Print a diff to a writer

A simple colorful theme using signs

A simple colorless using signs theme