https://github.com/purplebooth/termdiff
Diff a string for presentation to a user in the terminal
https://github.com/purplebooth/termdiff
diff terminal
Last synced: 2 months ago
JSON representation
Diff a string for presentation to a user in the terminal
- Host: GitHub
- URL: https://github.com/purplebooth/termdiff
- Owner: PurpleBooth
- License: cc0-1.0
- Created: 2021-10-21T22:21:30.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-08-07T22:42:23.000Z (almost 3 years ago)
- Last Synced: 2024-04-14T10:15:22.622Z (about 2 years ago)
- Topics: diff, terminal
- Language: Rust
- Homepage: https://crates.io/crates/termdiff
- Size: 216 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# termdiff
Diff a string for presentation to a user in the terminal.
## Usage
``` rust
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
``` rust
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 = 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 red fox and
>jumps over the lazy dog
"
);
```
Read more at [Docs.rs](https://docs.rs/termdiff/)
## Themes
We have a limited number of built in themes
### Arrows

### Signs

### Custom
``` rust
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
"
);
```