#terminal #text #diff

termdiff

Write a diff with color codes to a string

17 stable releases (4 major)

new 4.1.0 May 5, 2025
3.1.5 Apr 23, 2025
3.1.4 Aug 19, 2024
3.1.3 Jul 27, 2024
0.1.0 Oct 21, 2021

#230 in Text processing

Download history 22/week @ 2025-01-15 37/week @ 2025-01-22 68/week @ 2025-01-29 189/week @ 2025-02-05 151/week @ 2025-02-12 86/week @ 2025-02-19 131/week @ 2025-02-26 36/week @ 2025-03-05 32/week @ 2025-03-12 44/week @ 2025-03-19 46/week @ 2025-03-26 22/week @ 2025-04-02 18/week @ 2025-04-09 14/week @ 2025-04-16 217/week @ 2025-04-23 277/week @ 2025-04-30

527 downloads per month
Used in 2 crates

CC0 license

115KB
2K SLoC

termdiff

Diff a string for presentation to a user in the terminal.

Usage

use termdiff::{SignsTheme, 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 theme = SignsTheme::default();
let actual = 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

use termdiff::{ArrowsTheme, 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 theme = ArrowsTheme::default();
let mut buffer: Vec<u8> = Vec::new();
diff(&mut buffer, old, new, &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
"
);

Read more at Docs.rs

Themes

We have a limited number of built in themes

Arrows

Demo of the arrows format

Signs

Demo of the signs format

Custom

use termdiff::DrawDiff;
use termdiff::Theme;
use crossterm::style::Stylize;
use std::borrow::Cow;

#[derive(Debug)]
struct MyTheme {}
impl Theme for MyTheme {
    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()
    }
}

let my_theme = MyTheme {};

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
"
);

Dependencies

~0.2–8.5MB
~93K SLoC