1
0
Fork
You've already forked termdiff
0
Diff a string for presentation to a user in the terminal
Rust 98.8%
Just 1.2%
Solace System Renovate Fox 033015f169
Some checks failed
ci/woodpecker/push/lint/3 Pipeline was successful
ci/woodpecker/push/lint/1 Pipeline was successful
ci/woodpecker/push/lint/2 Pipeline was successful
ci/woodpecker/push/fix unknown status
ci/woodpecker/push/test/1 Pipeline was successful
ci/woodpecker/push/test/2 Pipeline was successful
ci/woodpecker/push/test/3 Pipeline was successful
ci/woodpecker/push/release Pipeline was successful
Merge pull request 'chore(deps): update rust docker digest to 087fe68' ( #46 ) from renovate/rust into main
2025年11月06日 00:14:32 +01:00
.cargo chore: add mutants.toml configuration for test mutation exclusions 2025年05月05日 15:53:40 +02:00
.woodpecker chore(deps): update rust docker digest to 087fe68 2025年11月05日 21:04:49 +00:00
benches refactor(src): move tests to dedicated directory and add benchmarking 2025年05月04日 20:50:06 +02:00
src chore: Aktualisiere similar-Bibliothek und entferne pedantic Clippy-Lint 2025年08月27日 08:20:25 +02:00
.fastconventional.yaml ci: refactor workflow and configuration files for improved readability 2025年10月05日 09:36:46 +02:00
.gitignore refactor(src): move tests to dedicated directory and add benchmarking 2025年05月04日 20:50:06 +02:00
Cargo.toml chore(version): v4.1.1 2025年10月08日 06:23:21 +00:00
CHANGELOG.md chore(version): v4.1.1 2025年10月08日 06:23:21 +00:00
CODE_OF_CONDUCT.md refactor: Markdown formatting 2023年01月20日 21:53:32 +01:00
cog.toml chore: remove post-bump git push hooks from cog.toml 2025年10月07日 09:46:47 +02:00
CONVENTIONS.md docs: Add project conventions and update diff rendering style 2025年05月05日 15:53:40 +02:00
demo_arrows.png docs: Update the images 2021年10月24日 01:09:16 +02:00
demo_signs.png docs: Update the images 2021年10月24日 01:09:16 +02:00
Justfile ci: add Concourse pipeline configuration command 2025年10月05日 09:25:27 +02:00
LICENSE.md feat: Initial Commit 2021年10月22日 00:26:16 +02:00
README.md docs(src): Format the examples a little nicer in the README 2022年03月06日 22:42:25 +01:00
renovate.json refactor: update Renovate configuration to use library preset 2025年05月15日 22:38:10 +02:00

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

Demo of the arrows format

Signs

Demo of the signs format

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